forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Gagan Juneja <[email protected]>
- Loading branch information
Gagan Juneja
committed
Oct 2, 2023
1 parent
3314fca
commit 7837dba
Showing
4 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...alClusterTest/java/org/opensearch/telemetry/metrics/InMemorySingletonMetricsExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...rnalClusterTest/java/org/opensearch/telemetry/metrics/TelemetryTracerEnabledSanityIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters