Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] [2.x] - Add Telemetry metrics framework #10393

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Pass parent filter to inner query in nested query ([#10246](https://github.com/opensearch-project/OpenSearch/pull/10246))
- Disable concurrent segment search when terminate_after is used ([#10200](https://github.com/opensearch-project/OpenSearch/pull/10200))
- Enable remote segment upload backpressure by default ([#10356](https://github.com/opensearch-project/OpenSearch/pull/10356))
- [Metrics Framework] Add Metrics framework. ([#10241](https://github.com/opensearch-project/OpenSearch/pull/10241))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.telemetry.metrics.tags.Tags;

/**
* Counter adds the value to the existing metric.
* {@opensearch.experimental}
*/
@ExperimentalApi
public interface Counter {

/**
* add value.
* @param value value to be added.
*/
void add(double value);

/**
* add value along with the attributes.
*
* @param value value to be added.
* @param tags attributes/dimensions of the metric.
*/
void add(double value, Tags tags);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import java.io.IOException;

/**
* Default implementation for {@link MetricsRegistry}
*/
class DefaultMetricsRegistry implements MetricsRegistry {
private final MetricsTelemetry metricsTelemetry;

/**
* Constructor
* @param metricsTelemetry metrics telemetry.
*/
public DefaultMetricsRegistry(MetricsTelemetry metricsTelemetry) {
this.metricsTelemetry = metricsTelemetry;
}

@Override
public Counter createCounter(String name, String description, String unit) {
return metricsTelemetry.createCounter(name, description, unit);
}

@Override
public Counter createUpDownCounter(String name, String description, String unit) {
return metricsTelemetry.createUpDownCounter(name, description, unit);
}

@Override
public void close() throws IOException {
metricsTelemetry.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics;

import org.opensearch.common.annotation.ExperimentalApi;

import java.io.Closeable;

/**
* MetricsRegistry helps in creating the metric instruments.
* @opensearch.experimental
*/
@ExperimentalApi
public interface MetricsRegistry extends Closeable {

/**
* Creates the counter.
* @param name name of the counter.
* @param description any description about the metric.
* @param unit unit of the metric.
* @return counter.
*/
Counter createCounter(String name, String description, String unit);

/**
* Creates the upDown counter.
* @param name name of the upDown counter.
* @param description any description about the metric.
* @param unit unit of the metric.
* @return counter.
*/
Counter createUpDownCounter(String name, String description, String unit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

import org.opensearch.common.annotation.ExperimentalApi;

import java.io.Closeable;

/**
* Interface for metrics telemetry providers
*
* @opensearch.experimental
*/
@ExperimentalApi
public interface MetricsTelemetry {
public interface MetricsTelemetry extends MetricsRegistry, Closeable {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics.noop;

import org.opensearch.common.annotation.InternalApi;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.tags.Tags;

/**
* No-op {@link Counter}
* {@opensearch.internal}
*/
@InternalApi
public class NoopCounter implements Counter {

/**
* No-op Counter instance
*/
public final static NoopCounter INSTANCE = new NoopCounter();

private NoopCounter() {}

@Override
public void add(double value) {

}

Check warning on line 32 in libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/noop/NoopCounter.java

View check run for this annotation

Codecov / codecov/patch

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/noop/NoopCounter.java#L32

Added line #L32 was not covered by tests

@Override
public void add(double value, Tags tags) {

}

Check warning on line 37 in libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/noop/NoopCounter.java

View check run for this annotation

Codecov / codecov/patch

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/noop/NoopCounter.java#L37

Added line #L37 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics.noop;

import org.opensearch.common.annotation.InternalApi;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.MetricsRegistry;

import java.io.IOException;

/**
*No-op {@link MetricsRegistry}
* {@opensearch.internal}
*/
@InternalApi
public class NoopMetricsRegistry implements MetricsRegistry {

/**
* No-op Meter instance
*/
public final static NoopMetricsRegistry INSTANCE = new NoopMetricsRegistry();

private NoopMetricsRegistry() {}

@Override
public Counter createCounter(String name, String description, String unit) {
return NoopCounter.INSTANCE;
}

@Override
public Counter createUpDownCounter(String name, String description, String unit) {
return NoopCounter.INSTANCE;
}

@Override
public void close() throws IOException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/**
* Contains metrics related classes
* {@opensearch.internal}
*/
@InternalApi
package org.opensearch.telemetry.metrics.noop;

import org.opensearch.common.annotation.InternalApi;
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.metrics.tags;

import org.opensearch.common.annotation.ExperimentalApi;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Class to create tags for a meter.
*
* @opensearch.experimental
*/
@ExperimentalApi
public class Tags {
private final Map<String, Object> tagsMap;
/**
* Empty value.
*/
public final static Tags EMPTY = new Tags(Collections.emptyMap());

/**
* Factory method.
* @return tags.
*/
public static Tags create() {
return new Tags(new HashMap<>());
}

/**
* Constructor.
*/
private Tags(Map<String, Object> tagsMap) {
this.tagsMap = tagsMap;
}

/**
* Add String attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, String value) {
Objects.requireNonNull(value, "value cannot be null");
tagsMap.put(key, value);
return this;
}

/**
* Add long attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, long value) {
tagsMap.put(key, value);
return this;
};

/**
* Add double attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, double value) {
tagsMap.put(key, value);
return this;
};

/**
* Add boolean attribute.
* @param key key
* @param value value
* @return Same instance.
*/
public Tags addTag(String key, boolean value) {
tagsMap.put(key, value);
return this;
};

/**
* Returns the attribute map.
* @return tags map
*/
public Map<String, ?> getTagsMap() {
return Collections.unmodifiableMap(tagsMap);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/**
* Contains metrics related classes
* @opensearch.experimental
*/
@ExperimentalApi
package org.opensearch.telemetry.metrics.tags;

import org.opensearch.common.annotation.ExperimentalApi;
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,4 @@ public interface TracingTelemetry extends Closeable {
*/
TracingContextPropagator getContextPropagator();

/**
* closes the resource
*/
void close();

}
Loading
Loading