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.
Adds mock implementation for TelemetryPlugin (opensearch-project#8357)
--------- Signed-off-by: Gagan Juneja <[email protected]> Signed-off-by: Gagan Juneja <[email protected]> Co-authored-by: Gagan Juneja <[email protected]>
- Loading branch information
1 parent
39bd704
commit 11840bd
Showing
15 changed files
with
504 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
42 changes: 42 additions & 0 deletions
42
test/framework/src/main/java/org/opensearch/test/telemetry/MockTelemetry.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,42 @@ | ||
/* | ||
* 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.test.telemetry; | ||
|
||
import org.opensearch.telemetry.Telemetry; | ||
import org.opensearch.telemetry.TelemetrySettings; | ||
import org.opensearch.telemetry.metrics.MetricsTelemetry; | ||
import org.opensearch.test.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() { | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
test/framework/src/main/java/org/opensearch/test/telemetry/MockTelemetryPlugin.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,39 @@ | ||
/* | ||
* 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.test.telemetry; | ||
|
||
import java.util.Optional; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.plugins.TelemetryPlugin; | ||
import org.opensearch.telemetry.Telemetry; | ||
import org.opensearch.telemetry.TelemetrySettings; | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
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,25 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
apply plugin: 'opensearch.build' | ||
apply plugin: 'opensearch.publish' | ||
|
||
dependencies { | ||
api project(":libs:opensearch-common") | ||
api project(":libs:opensearch-telemetry") | ||
} | ||
|
||
tasks.named('forbiddenApisMain').configure { | ||
//package does not depend on core, so only jdk signatures should be checked | ||
replaceSignatureFiles 'jdk-signatures' | ||
} | ||
|
||
test.enabled = false |
159 changes: 159 additions & 0 deletions
159
test/telemetry/src/main/java/org/opensearch/test/telemetry/tracing/MockSpan.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,159 @@ | ||
/* | ||
* 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.test.telemetry.tracing; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.function.Supplier; | ||
import org.opensearch.telemetry.tracing.AbstractSpan; | ||
import org.opensearch.telemetry.tracing.Span; | ||
|
||
/** | ||
* 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(); | ||
|
||
private static final Supplier<Random> randomSupplier = ThreadLocalRandom::current; | ||
|
||
/** | ||
* 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 = randomSupplier.get().nextLong(); | ||
return Long.toHexString(id); | ||
} | ||
|
||
private static String generateTraceId() { | ||
long idHi = randomSupplier.get().nextLong(); | ||
long idLo = randomSupplier.get().nextLong(); | ||
long result = idLo | (idHi << 32); | ||
return Long.toHexString(result); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.