Skip to content

Commit

Permalink
Add unit tests
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 2, 2023
1 parent 3314fca commit 7837dba
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
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 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;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.opensearch.test.telemetry.tracing.MockSpanData;

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,91 @@
/*
* 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 io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugins.Plugin;
import org.opensearch.search.SearchService;
import org.opensearch.telemetry.OTelTelemetrySettings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.telemetry.tracing.InMemorySingletonSpanExporter;
import org.opensearch.telemetry.tracing.IntegrationTestOTelTelemetryPlugin;
import org.opensearch.telemetry.tracing.attributes.Attributes;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.OpenSearchSingleNodeTestCase;
import org.opensearch.test.telemetry.tracing.TelemetryValidators;
import org.opensearch.test.telemetry.tracing.validators.AllSpansAreEndedProperly;
import org.opensearch.test.telemetry.tracing.validators.AllSpansHaveUniqueId;
import org.opensearch.test.telemetry.tracing.validators.NumberOfTraceIDsEqualToRequests;
import org.opensearch.test.telemetry.tracing.validators.TotalRootSpansEqualToRequests;

import static org.opensearch.index.query.QueryBuilders.queryStringQuery;

public class TelemetryTracerEnabledSanityIT extends OpenSearchSingleNodeTestCase {

@Override
protected Settings nodeSettings() {
return Settings.builder()
.put(super.nodeSettings())
.put(
OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(),
"org.opensearch.telemetry.metrics.InMemorySingletonMetricsExporter"
)
.put(OTelTelemetrySettings.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 {
updateTelemetrySetting(client(), true);
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();
assertTrue(1.0 == value);
cleanupSettings(client());
}

private static void updateTelemetrySetting(Client client, boolean value) {
client.admin()
.cluster()
.prepareUpdateSettings()
.setTransientSettings(Settings.builder().put(TelemetrySettings.METRICS_ENABLED_SETTING.getKey(), value))
.get();
}

private void cleanupSettings(Client client){
client().admin()
.cluster()
.prepareUpdateSettings()
.setTransientSettings(Settings.builder().putNull(TelemetrySettings.METRICS_ENABLED_SETTING.getKey()))
.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.opensearch.common.settings.Settings;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.telemetry.metrics.exporter.OTelMetricsExporterFactory;
import org.opensearch.telemetry.tracing.exporter.OTelSpanExporterFactory;
import org.opensearch.telemetry.tracing.sampler.ProbabilisticSampler;
import org.opensearch.telemetry.tracing.sampler.RequestSampler;
Expand Down Expand Up @@ -85,7 +86,7 @@ private static SdkMeterProvider createSdkMetricProvider(Settings settings, Resou
return SdkMeterProvider.builder()
.setResource(resource)
.registerMetricReader(
PeriodicMetricReader.builder(LoggingMetricExporter.create(AggregationTemporality.DELTA))
PeriodicMetricReader.builder(OTelMetricsExporterFactory.create(settings))
.setInterval(METRICS_PUBLISH_INTERVAL_SETTING.get(settings).getSeconds(), TimeUnit.SECONDS)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ protected boolean addMockHttpTransport() {
return true;
}

protected boolean addMockTelemetryPlugin() {
return true;
}

private Node newNode() {
final Path tempDir = createTempDir();
final String nodeName = nodeSettings().get(Node.NODE_NAME_SETTING.getKey(), "node_s_0");
Expand Down Expand Up @@ -271,7 +275,9 @@ private Node newNode() {
plugins.add(MockHttpTransport.TestPlugin.class);
}
plugins.add(MockScriptService.TestPlugin.class);
plugins.add(MockTelemetryPlugin.class);
if(addMockTelemetryPlugin()) {
plugins.add(MockTelemetryPlugin.class);
}
Node node = new MockNode(settingsBuilder.build(), plugins, forbidPrivateIndexSettings());
try {
node.start();
Expand Down

0 comments on commit 7837dba

Please sign in to comment.