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

Adds mock implementation for TelemetryPlugin #8357

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Make remote cluster connection setup in async ([#8038](https://github.com/opensearch-project/OpenSearch/pull/8038))
- Add API to initialize extensions ([#8029]()https://github.com/opensearch-project/OpenSearch/pull/8029)
- Add distributed tracing framework ([#7543](https://github.com/opensearch-project/OpenSearch/issues/7543))
- Adds mock implementation for TelemetryPlugin ([#7545](https://github.com/opensearch-project/OpenSearch/issues/7545))

### Dependencies
- Bump `com.azure:azure-storage-common` from 12.21.0 to 12.21.1 (#7566, #7814)
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/org/opensearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ public synchronized void close() throws IOException {
toClose.add(injector.getInstance(NodeEnvironment.class));
toClose.add(stopWatch::stop);
if (FeatureFlags.isEnabled(TELEMETRY)) {
toClose.add(() -> injector.getInstance(TracerFactory.class));
toClose.add(injector.getInstance(TracerFactory.class));
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved
}

if (logger.isTraceEnabled()) {
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ List projects = [
'test:fixtures:minio-fixture',
'test:fixtures:old-elasticsearch',
'test:fixtures:s3-fixture',
'test:logger-usage'
'test:logger-usage',
'test:telemetry'
]

/**
Expand Down
1 change: 1 addition & 0 deletions test/framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
api project(':libs:opensearch-nio')
api project(":server")
api project(":libs:opensearch-cli")
api project(":test:telemetry")
api "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
api "junit:junit:${versions.junit}"
api "org.hamcrest:hamcrest:${versions.hamcrest}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import org.opensearch.common.unit.ByteSizeUnit;
import org.opensearch.common.unit.ByteSizeValue;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -152,6 +153,7 @@
import org.opensearch.test.disruption.ServiceDisruptionScheme;
import org.opensearch.test.store.MockFSIndexStore;
import org.opensearch.test.transport.MockTransportService;
import org.opensearch.telemetry.MockTelemetryPlugin;
import org.opensearch.transport.TransportInterceptor;
import org.opensearch.transport.TransportRequest;
import org.opensearch.transport.TransportRequestHandler;
Expand Down Expand Up @@ -776,6 +778,7 @@ protected Settings featureFlagSettings() {
for (Setting builtInFlag : FeatureFlagSettings.BUILT_IN_FEATURE_FLAGS) {
featureSettings.put(builtInFlag.getKey(), builtInFlag.getDefaultRaw(Settings.EMPTY));
}
featureSettings.put(FeatureFlags.TELEMETRY_SETTING.getKey(), true);
return featureSettings.build();
}

Expand Down Expand Up @@ -2101,6 +2104,7 @@ protected Collection<Class<? extends Plugin>> getMockPlugins() {
if (addMockGeoShapeFieldMapper()) {
mocks.add(TestGeoShapeFieldMapperPlugin.class);
}
mocks.add(MockTelemetryPlugin.class);

return Collections.unmodifiableList(mocks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.common.util.concurrent.OpenSearchExecutors;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.core.common.Strings;
Expand All @@ -66,6 +67,8 @@
import org.opensearch.plugins.Plugin;
import org.opensearch.script.MockScriptService;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.telemetry.TelemetrySettings;
import org.opensearch.telemetry.MockTelemetryPlugin;
import org.opensearch.transport.TransportSettings;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -242,6 +245,8 @@ private Node newNode() {
.put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false)
.putList(DISCOVERY_SEED_HOSTS_SETTING.getKey()) // empty list disables a port scan for other nodes
.putList(INITIAL_CLUSTER_MANAGER_NODES_SETTING.getKey(), nodeName)
.put(FeatureFlags.TELEMETRY_SETTING.getKey(), true)
.put(TelemetrySettings.TRACER_ENABLED_SETTING.getKey(), true)
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved
.put(nodeSettings()) // allow test cases to provide their own settings or override these
.build();

Expand All @@ -254,6 +259,7 @@ private Node newNode() {
plugins.add(MockHttpTransport.TestPlugin.class);
}
plugins.add(MockScriptService.TestPlugin.class);
plugins.add(MockTelemetryPlugin.class);
Node node = new MockNode(settings, plugins, forbidPrivateIndexSettings());
try {
node.start();
Expand Down
40 changes: 40 additions & 0 deletions test/telemetry/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

apply plugin: 'opensearch.build'
apply plugin: 'opensearch.publish'

dependencies {
api project(":libs:opensearch-common")
api project(":libs:opensearch-telemetry")
api project(":server")
Gaganjuneja marked this conversation as resolved.
Show resolved Hide resolved
}

test.enabled = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;

import org.opensearch.telemetry.metrics.MetricsTelemetry;
import org.opensearch.telemetry.tracing.MockTracingTelemetry;
import org.opensearch.telemetry.tracing.TracingTelemetry;

/**
* Mock {@link Telemetry} implementation for testing.
*/
public class MockTelemetry implements Telemetry {

private final TelemetrySettings settings;

/**
* Constructor with settings.
* @param settings telemetry settings.
*/
public MockTelemetry(TelemetrySettings settings) {
this.settings = settings;
}

@Override
public TracingTelemetry getTracingTelemetry() {
return new MockTracingTelemetry();
}

@Override
public MetricsTelemetry getMetricsTelemetry() {
return new MetricsTelemetry() {
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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;

import java.util.Optional;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.TelemetryPlugin;

/**
* Mock {@link TelemetryPlugin} implementation for testing.
*/
public class MockTelemetryPlugin extends Plugin implements TelemetryPlugin {
private static final String MOCK_TRACER_NAME = "mock";

/**
* Base constructor.
*/
public MockTelemetryPlugin() {

}

@Override
public Optional<Telemetry> getTelemetry(TelemetrySettings settings) {
return Optional.of(new MockTelemetry(settings));
}

@Override
public String getName() {
return MOCK_TRACER_NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Base opensearch package. */
package org.opensearch.telemetry;
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.tracing;

import java.util.HashMap;
import java.util.Map;
import org.opensearch.common.Randomness;

/**
* MockSpan for testing and strict check validations. Not to be used for production cases.
*/
public class MockSpan extends AbstractSpan {
private final SpanProcessor spanProcessor;
private final Map<String, Object> metadata;
private final String traceId;
private final String spanId;
private boolean hasEnded;
private final Long startTime;
private Long endTime;

private final Object lock = new Object();

/**
* Base Constructor.
* @param spanName span name
* @param parentSpan parent span
* @param spanProcessor span processor
*/
public MockSpan(String spanName, Span parentSpan, SpanProcessor spanProcessor) {
this(
spanName,
parentSpan,
parentSpan != null ? parentSpan.getTraceId() : IdGenerator.generateTraceId(),
IdGenerator.generateSpanId(),
spanProcessor
);
}

/**
* Constructor with traceId and SpanIds
* @param spanName Span Name
* @param parentSpan Parent Span
* @param traceId Trace ID
* @param spanId Span ID
* @param spanProcessor Span Processor
*/
public MockSpan(String spanName, Span parentSpan, String traceId, String spanId, SpanProcessor spanProcessor) {
super(spanName, parentSpan);
this.spanProcessor = spanProcessor;
this.metadata = new HashMap<>();
this.traceId = traceId;
this.spanId = spanId;
this.startTime = System.nanoTime();
}

@Override
public void endSpan() {
synchronized (lock) {
if (hasEnded) {
return;
}
endTime = System.nanoTime();
hasEnded = true;
}
spanProcessor.onEnd(this);
}

@Override
public void addAttribute(String key, String value) {
putMetadata(key, value);
}

@Override
public void addAttribute(String key, Long value) {
putMetadata(key, value);
}

@Override
public void addAttribute(String key, Double value) {
putMetadata(key, value);
}

@Override
public void addAttribute(String key, Boolean value) {
putMetadata(key, value);
}

@Override
public void addEvent(String event) {
putMetadata(event, null);
}

private void putMetadata(String key, Object value) {
metadata.put(key, value);
}

@Override
public String getTraceId() {
return traceId;
}

@Override
public String getSpanId() {
return spanId;
}

/**
* Returns whether the span is ended or not.
* @return span end status.
*/
public boolean hasEnded() {
synchronized (lock) {
return hasEnded;
}
}

/**
* Returns the start time of the span.
* @return start time of the span.
*/
public Long getStartTime() {
return startTime;
}

/**
* Returns the start time of the span.
* @return end time of the span.
*/
public Long getEndTime() {
return endTime;
}

private static class IdGenerator {
private static String generateSpanId() {
long id = Randomness.get().nextLong();
return Long.toHexString(id);
}

private static String generateTraceId() {
long idHi = Randomness.get().nextLong();
long idLo = Randomness.get().nextLong();
long result = idLo | (idHi << 32);
return Long.toHexString(result);
}

}
}
Loading