Skip to content

Commit

Permalink
Update Gauge meter to add tags with value
Browse files Browse the repository at this point in the history
Signed-off-by: Gagan Juneja <[email protected]>
  • Loading branch information
Gagan Juneja committed Jun 5, 2024
1 parent 39ae32a commit 7573b6f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package org.opensearch.telemetry.metrics;

import org.opensearch.telemetry.metrics.tags.Tags;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.Supplier;
Expand Down Expand Up @@ -44,8 +42,8 @@ public Histogram createHistogram(String name, String description, String unit) {
}

@Override
public Closeable createGauge(String name, String description, String unit, Supplier<Double> valueProvider, Tags tags) {
return metricsTelemetry.createGauge(name, description, unit, valueProvider, tags);
public Closeable createGauge(String name, String description, String unit, Supplier<ObservableMeasurement> valueProvider) {
return metricsTelemetry.createGauge(name, description, unit, valueProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.opensearch.telemetry.metrics;

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

import java.io.Closeable;
import java.util.function.Supplier;
Expand Down Expand Up @@ -54,13 +53,12 @@ public interface MetricsRegistry extends Closeable {
* Creates the Observable Gauge type of Metric. Where the value provider will be called at a certain frequency
* to capture the value.
*
* @param name name of the observable gauge.
* @param description any description about the metric.
* @param unit unit of the metric.
* @param valueProvider value provider.
* @param tags attributes/dimensions of the metric.
* @param name name of the observable gauge.
* @param description any description about the metric.
* @param unit unit of the metric.
* @param value value provider.
* @return closeable to dispose/close the Gauge metric.
*/
Closeable createGauge(String name, String description, String unit, Supplier<Double> valueProvider, Tags tags);
Closeable createGauge(String name, String description, String unit, Supplier<ObservableMeasurement> value);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

/**
* Observable Measurement for the Asynchronous instruments.
* @opensearch.experimental
*/
@ExperimentalApi
public class ObservableMeasurement {
private final Double value;
private final Tags tags;

/**
* Factory method to create the {@link ObservableMeasurement} object.
* @param value value.
* @param tags tags to be added per value.
* @return ObservableMeasurement
*/
public static ObservableMeasurement create(double value, Tags tags) {
return new ObservableMeasurement(value, tags);

Check warning on line 30 in libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java

View check run for this annotation

Codecov / codecov/patch

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java#L30

Added line #L30 was not covered by tests
}

private ObservableMeasurement(double value, Tags tags) {
this.value = value;
this.tags = tags;
}

Check warning on line 36 in libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java

View check run for this annotation

Codecov / codecov/patch

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java#L33-L36

Added lines #L33 - L36 were not covered by tests

/**
* Returns the value.
* @return value
*/
public Double getValue() {
return value;

Check warning on line 43 in libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java

View check run for this annotation

Codecov / codecov/patch

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java#L43

Added line #L43 was not covered by tests
}

/**
* Returns the tags.
* @return tags
*/
public Tags getTags() {
return tags;

Check warning on line 51 in libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java

View check run for this annotation

Codecov / codecov/patch

libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/ObservableMeasurement.java#L51

Added line #L51 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.Histogram;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.telemetry.metrics.tags.Tags;
import org.opensearch.telemetry.metrics.ObservableMeasurement;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -48,7 +48,7 @@ public Histogram createHistogram(String name, String description, String unit) {
}

@Override
public Closeable createGauge(String name, String description, String unit, Supplier<Double> valueProvider, Tags tags) {
public Closeable createGauge(String name, String description, String unit, Supplier<ObservableMeasurement> valueProvider) {
return () -> {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ public void testHistogram() {
@SuppressWarnings("unchecked")
public void testGauge() {
Closeable mockCloseable = mock(Closeable.class);
when(
defaultMeterRegistry.createGauge(any(String.class), any(String.class), any(String.class), any(Supplier.class), any(Tags.class))
).thenReturn(mockCloseable);
when(defaultMeterRegistry.createGauge(any(String.class), any(String.class), any(String.class), any(Supplier.class))).thenReturn(
mockCloseable
);
Closeable closeable = defaultMeterRegistry.createGauge(
"org.opensearch.telemetry.metrics.DefaultMeterRegistryTests.testObservableGauge",
"test observable gauge",
"ms",
() -> 1.0,
Tags.EMPTY
() -> ObservableMeasurement.create(1.0, Tags.EMPTY)
);
assertSame(mockCloseable, closeable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableExponentialHistogramPointData;
Expand Down Expand Up @@ -130,8 +133,10 @@ public void testGauge() throws Exception {
InMemorySingletonMetricsExporter.INSTANCE.reset();
Tags tags = Tags.create().addTag("test", "integ-test");
final AtomicInteger testValue = new AtomicInteger(0);
Supplier<Double> valueProvider = () -> { return Double.valueOf(testValue.incrementAndGet()); };
Closeable gaugeCloseable = metricsRegistry.createGauge(metricName, "test", "ms", valueProvider, tags);
Supplier<ObservableMeasurement> valueProvider = () -> {
return ObservableMeasurement.create(Double.valueOf(testValue.incrementAndGet()), tags);
};
Closeable gaugeCloseable = metricsRegistry.createGauge(metricName, "test", "ms", valueProvider);
// Sleep for about 2.2s to wait for metrics to be published.
Thread.sleep(2200);

Expand All @@ -140,6 +145,9 @@ public void testGauge() throws Exception {
assertTrue(getMaxObservableGaugeValue(exporter, metricName) >= 2.0);
gaugeCloseable.close();
double observableGaugeValueAfterStop = getMaxObservableGaugeValue(exporter, metricName);
Map<AttributeKey<?>, Object> attributes = getMetricAttributes(exporter, metricName);

assertEquals("integ-test", attributes.get(AttributeKey.stringKey("test")));

// Sleep for about 1.2s to wait for metrics to see that closed observableGauge shouldn't execute the callable.
Thread.sleep(1200);
Expand All @@ -154,11 +162,21 @@ private static double getMaxObservableGaugeValue(InMemorySingletonMetricsExporte
.collect(Collectors.toList());
double totalValue = 0;
for (MetricData metricData : dataPoints) {
Attributes attributes = metricData.getDoubleGaugeData().getPoints().stream().findAny().get().getAttributes();
totalValue = Math.max(totalValue, ((DoublePointData) metricData.getDoubleGaugeData().getPoints().toArray()[0]).getValue());
}
return totalValue;
}

private static Map<AttributeKey<?>, Object> getMetricAttributes(InMemorySingletonMetricsExporter exporter, String metricName) {
List<MetricData> dataPoints = exporter.getFinishedMetricItems()
.stream()
.filter(a -> a.getName().contains(metricName))
.collect(Collectors.toList());
Attributes attributes = dataPoints.get(0).getDoubleGaugeData().getPoints().stream().findAny().get().getAttributes();
return attributes.asMap();
}

@After
public void reset() {
InMemorySingletonMetricsExporter.INSTANCE.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.opensearch.common.concurrent.RefCountedReleasable;
import org.opensearch.telemetry.OTelAttributesConverter;
import org.opensearch.telemetry.OTelTelemetryPlugin;
import org.opensearch.telemetry.metrics.tags.Tags;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -91,12 +90,14 @@ public Histogram createHistogram(String name, String description, String unit) {
}

@Override
public Closeable createGauge(String name, String description, String unit, Supplier<Double> valueProvider, Tags tags) {
public Closeable createGauge(String name, String description, String unit, Supplier<ObservableMeasurement> valueProvider) {
ObservableDoubleGauge doubleObservableGauge = AccessController.doPrivileged(
(PrivilegedAction<ObservableDoubleGauge>) () -> otelMeter.gaugeBuilder(name)
.setUnit(unit)
.setDescription(description)
.buildWithCallback(record -> record.record(valueProvider.get(), OTelAttributesConverter.convert(tags)))
.buildWithCallback(
record -> record.record(valueProvider.get().getValue(), OTelAttributesConverter.convert(valueProvider.get().getTags()))

Check warning on line 99 in plugins/telemetry-otel/src/main/java/org/opensearch/telemetry/metrics/OTelMetricsTelemetry.java

View check run for this annotation

Codecov / codecov/patch

plugins/telemetry-otel/src/main/java/org/opensearch/telemetry/metrics/OTelMetricsTelemetry.java#L99

Added line #L99 was not covered by tests
)
);
return () -> doubleObservableGauge.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ public void testGauge() throws Exception {
when(mockOTelDoubleGaugeBuilder.setUnit(unit)).thenReturn(mockOTelDoubleGaugeBuilder);
when(mockOTelDoubleGaugeBuilder.buildWithCallback(any(Consumer.class))).thenReturn(observableDoubleGauge);

Closeable closeable = metricsTelemetry.createGauge(observableGaugeName, description, unit, () -> 1.0, Tags.EMPTY);
Closeable closeable = metricsTelemetry.createGauge(
observableGaugeName,
description,
unit,
() -> ObservableMeasurement.create(1.0, Tags.EMPTY)
);
closeable.close();
verify(observableDoubleGauge).close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.Histogram;
import org.opensearch.telemetry.metrics.MetricsTelemetry;
import org.opensearch.telemetry.metrics.ObservableMeasurement;
import org.opensearch.telemetry.metrics.noop.NoopCounter;
import org.opensearch.telemetry.metrics.noop.NoopHistogram;
import org.opensearch.telemetry.metrics.tags.Tags;
import org.opensearch.telemetry.tracing.TracingTelemetry;
import org.opensearch.test.telemetry.tracing.MockTracingTelemetry;

Expand Down Expand Up @@ -58,7 +58,7 @@ public Histogram createHistogram(String name, String description, String unit) {
}

@Override
public Closeable createGauge(String name, String description, String unit, Supplier<Double> valueProvider, Tags tags) {
public Closeable createGauge(String name, String description, String unit, Supplier<ObservableMeasurement> valueProvider) {
return () -> {};
}

Expand Down

0 comments on commit 7573b6f

Please sign in to comment.