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

Add Telemetry metrics framework #10241

Merged
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add instrumentation in Inbound Handler. ([#100143](https://github.com/opensearch-project/OpenSearch/pull/10143))
- Enable remote segment upload backpressure by default ([#10356](https://github.com/opensearch-project/OpenSearch/pull/10356))
- [Remote Store] Add support to reload repository metadata inplace ([#9569](https://github.com/opensearch-project/OpenSearch/pull/9569))
- [Metrics Framework] Add Metrics framework. ([#10241](https://github.com/opensearch-project/OpenSearch/pull/10241))

### Deprecated

Expand All @@ -147,4 +148,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Security

[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.11...2.x
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.11...2.x
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 {
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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.
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved
* @opensearch.experimental
*/
@ExperimentalApi
public interface MetricsRegistry extends Closeable {
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've introduced a new MetricsRegistry which individual classes may need to inject along with the previously created Tracer interface.

Is it possible to merge the needed interfaces to emit any tracing/metrics, etc. into a single wrapper which can help to avoid a lot of changes if a component decides to have only tracing today, but metrics in future or vice versa.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to merge the needed interfaces to emit any tracing/metrics, etc. into a single wrapper which can help to avoid a lot of changes if a component decides to have only tracing today, but metrics in future or vice versa.

Thanks @mgodwan we have not done metrics injection yet, but I think injecting Telemetry or TelemetryService instead would make sense (and cover your concerns).


/**
* 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 {
reta marked this conversation as resolved.
Show resolved Hide resolved

}
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 {
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 {
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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;
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved

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;
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved

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