Skip to content

Commit

Permalink
Add telemetry tracer/metric enable flag and integ test
Browse files Browse the repository at this point in the history
Signed-off-by: Gagan Juneja <[email protected]>
  • Loading branch information
Gagan Juneja committed Oct 5, 2023
1 parent 211578c commit bb41efe
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

import org.opensearch.common.annotation.ExperimentalApi;

import java.io.Closeable;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
* compatible open source license.
*/

package org.opensearch.telemetry.tracing;
package org.opensearch.telemetry;

import org.opensearch.common.settings.Settings;
import org.opensearch.telemetry.OTelTelemetryPlugin;
import org.opensearch.telemetry.Telemetry;
import org.opensearch.telemetry.TelemetrySettings;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.util.Collection;
import java.util.List;

import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter;

public class InMemorySingletonMetricsExporter implements MetricExporter {

public static final InMemorySingletonMetricsExporter INSTANCE = new InMemorySingletonMetricsExporter(InMemoryMetricExporter.create());

private static InMemoryMetricExporter delegate;

public static InMemorySingletonMetricsExporter create() {
return INSTANCE;
}

private InMemorySingletonMetricsExporter(InMemoryMetricExporter delegate) {
InMemorySingletonMetricsExporter.delegate = delegate;
}

@Override
public CompletableResultCode export(Collection<MetricData> metrics) {
return delegate.export(metrics);
}

@Override
public CompletableResultCode flush() {
return delegate.flush();
}

@Override
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

public List<MetricData> getFinishedMetricItems() {
return delegate.getFinishedMetricItems();
}

/**
* Clears the state.
*/
public void reset() {
delegate.reset();
}

@Override
public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) {
return delegate.getAggregationTemporality(instrumentType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugins.Plugin;
import org.opensearch.telemetry.IntegrationTestOTelTelemetryPlugin;
import org.opensearch.telemetry.OTelTelemetrySettings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.telemetry.metrics.noop.NoopCounter;
import org.opensearch.telemetry.metrics.noop.NoopMetricsRegistry;
import org.opensearch.test.OpenSearchSingleNodeTestCase;

import java.util.Arrays;
import java.util.Collection;

public class TelemetryMetricsDisabledSanityIT extends OpenSearchSingleNodeTestCase {

@Override
protected Settings nodeSettings() {
return Settings.builder()
.put(super.nodeSettings())
.put(TelemetrySettings.METRICS_FEATURE_ENABLED_SETTING.getKey(), false)
.put(
OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(),
"org.opensearch.telemetry.metrics.InMemorySingletonMetricsExporter"
)
.put(TelemetrySettings.METRICS_PUBLISH_INTERVAL_SETTING.getKey(), TimeValue.timeValueSeconds(1))
.build();
}

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return Arrays.asList(IntegrationTestOTelTelemetryPlugin.class);
}

@Override
protected boolean addMockTelemetryPlugin() {
return false;
}

public void testSanityChecksWhenMetricsEnabled() throws Exception {
MetricsRegistry metricsRegistry = node().injector().getInstance(MetricsRegistry.class);

Counter counter = metricsRegistry.createCounter("test-counter", "test", "1");
counter.add(1.0);

assertTrue(metricsRegistry instanceof NoopMetricsRegistry);
assertTrue(counter instanceof NoopCounter);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugins.Plugin;
import org.opensearch.telemetry.IntegrationTestOTelTelemetryPlugin;
import org.opensearch.telemetry.OTelTelemetrySettings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.test.OpenSearchSingleNodeTestCase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import io.opentelemetry.sdk.metrics.data.DoublePointData;

public class TelemetryMetricsEnabledCounterIT extends OpenSearchSingleNodeTestCase {

@Override
protected Settings nodeSettings() {
return Settings.builder()
.put(super.nodeSettings())
.put(TelemetrySettings.METRICS_FEATURE_ENABLED_SETTING.getKey(), true)
.put(
OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(),
"org.opensearch.telemetry.metrics.InMemorySingletonMetricsExporter"
)
.put(TelemetrySettings.METRICS_PUBLISH_INTERVAL_SETTING.getKey(), TimeValue.timeValueSeconds(1))
.build();
}

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return Arrays.asList(IntegrationTestOTelTelemetryPlugin.class);
}

@Override
protected boolean addMockTelemetryPlugin() {
return false;
}

public void testCounter() throws Exception {
MetricsRegistry metricsRegistry = node().injector().getInstance(MetricsRegistry.class);

Counter counter = metricsRegistry.createCounter("test-counter", "test", "1");
counter.add(1.0);
// Sleep for about 2s to wait for metrics to be published.
Thread.sleep(2000);

InMemorySingletonMetricsExporter exporter = InMemorySingletonMetricsExporter.INSTANCE;
double value = ((DoublePointData) ((ArrayList) exporter.getFinishedMetricItems().get(0).getDoubleSumData().getPoints()).get(0))
.getValue();
assertEquals(1.0, value, 0.0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugins.Plugin;
import org.opensearch.telemetry.IntegrationTestOTelTelemetryPlugin;
import org.opensearch.telemetry.OTelTelemetrySettings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.test.OpenSearchSingleNodeTestCase;
import org.junit.After;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import io.opentelemetry.sdk.metrics.data.DoublePointData;

public class TelemetryMetricsEnabledUpDownCounterIT extends OpenSearchSingleNodeTestCase {

@Override
protected Settings nodeSettings() {
return Settings.builder()
.put(super.nodeSettings())
.put(TelemetrySettings.METRICS_FEATURE_ENABLED_SETTING.getKey(), true)
.put(
OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(),
"org.opensearch.telemetry.metrics.InMemorySingletonMetricsExporter"
)
.put(TelemetrySettings.METRICS_PUBLISH_INTERVAL_SETTING.getKey(), TimeValue.timeValueSeconds(1))
.build();
}

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return Arrays.asList(IntegrationTestOTelTelemetryPlugin.class);
}

@Override
protected boolean addMockTelemetryPlugin() {
return false;
}

public void testUpDownCounter() throws Exception {

MetricsRegistry metricsRegistry = node().injector().getInstance(MetricsRegistry.class);

Counter counter = metricsRegistry.createUpDownCounter("test-up-down-counter", "test", "1");
counter.add(1.0);
counter.add(-2.0);
// Sleep for about 2s to wait for metrics to be published.
Thread.sleep(2000);

InMemorySingletonMetricsExporter exporter = InMemorySingletonMetricsExporter.INSTANCE;
double value = ((DoublePointData) ((ArrayList) exporter.getFinishedMetricItems().get(0).getDoubleSumData().getPoints()).get(0))
.getValue();
assertEquals(-1.0, value, 0.0);
}

@After
public void reset() {
InMemorySingletonMetricsExporter.INSTANCE.reset();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugins.Plugin;
import org.opensearch.telemetry.IntegrationTestOTelTelemetryPlugin;
import org.opensearch.telemetry.OTelTelemetrySettings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.test.OpenSearchIntegTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugins.Plugin;
import org.opensearch.telemetry.IntegrationTestOTelTelemetryPlugin;
import org.opensearch.telemetry.OTelTelemetrySettings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.telemetry.tracing.attributes.Attributes;
Expand Down Expand Up @@ -88,9 +89,7 @@ public void testSanityChecksWhenTracingEnabled() throws Exception {
);

InMemorySingletonSpanExporter exporter = InMemorySingletonSpanExporter.INSTANCE;
if (!exporter.getFinishedSpanItems().isEmpty()) {
validators.validate(exporter.getFinishedSpanItems(), 6);
}
validators.validate(exporter.getFinishedSpanItems(), 6);
}

private static void updateTelemetrySetting(Client client, boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.TelemetryPlugin;
import org.opensearch.telemetry.metrics.MetricsTelemetry;
import org.opensearch.telemetry.metrics.OTelMetricsTelemetry;
import org.opensearch.telemetry.tracing.OTelResourceProvider;
import org.opensearch.telemetry.tracing.OTelTelemetry;
import org.opensearch.telemetry.tracing.OTelTracingTelemetry;
import org.opensearch.telemetry.tracing.TracingTelemetry;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -58,20 +60,39 @@ public List<Setting<?>> getSettings() {

@Override
public Optional<Telemetry> getTelemetry(TelemetrySettings telemetrySettings) {
return Optional.of(telemetry(telemetrySettings));
return telemetry(telemetrySettings);
}

@Override
public String getName() {
return OTEL_TRACER_NAME;
}

private Telemetry telemetry(TelemetrySettings telemetrySettings) {
final OpenTelemetrySdk openTelemetry = OTelResourceProvider.get(telemetrySettings, settings);
return new OTelTelemetry(
new OTelTracingTelemetry<>(openTelemetry, openTelemetry.getSdkTracerProvider()),
new OTelMetricsTelemetry<>(openTelemetry.getSdkMeterProvider())
);
private Optional<Telemetry> telemetry(TelemetrySettings telemetrySettings) {
final Optional<OpenTelemetrySdk> openTelemetry = OTelResourceProvider.get(telemetrySettings, settings);
if (openTelemetry.isPresent()) {
return Optional.of(
new OTelTelemetry(getTracingTelemetry(settings, openTelemetry.get()), getMetricsTelemetry(settings, openTelemetry.get()))
);
} else {
return Optional.empty();
}
}

private MetricsTelemetry getMetricsTelemetry(Settings settings, OpenTelemetrySdk openTelemetry) {
if (TelemetrySettings.METRICS_FEATURE_ENABLED_SETTING.get(settings)) {
return new OTelMetricsTelemetry(openTelemetry.getSdkMeterProvider());
} else {
return null;
}
}

private TracingTelemetry getTracingTelemetry(Settings settings, OpenTelemetrySdk openTelemetry) {
if (TelemetrySettings.TRACER_FEATURE_ENABLED_SETTING.get(settings)) {
return new OTelTracingTelemetry(openTelemetry, openTelemetry.getTracerProvider());
} else {
return null;
}
}

}
Loading

0 comments on commit bb41efe

Please sign in to comment.