Skip to content

Commit

Permalink
Add a small artifact that joins TracerSdk and InMemorySpanExporter. (#…
Browse files Browse the repository at this point in the history
…494)

* Add a small artifact that joins TracerSdk and InMemorySpanExporter.

Used for OpenTelemetry integration testing.

* Re-organize the inmemory artifact.

* Put it under contrib.
* Use package io.opentelemetry.sdk.contrib.trace.export

* Move inmemory_export to sdk_contrib.
  • Loading branch information
carlosalberto authored and bogdandrutu committed Aug 27, 2019
1 parent 75811d4 commit 925cf41
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sdk_contrib/inmemory_export/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description = 'OpenTelemetry InMemory Export'

dependencies {
api project(':opentelemetry-sdk')

testImplementation "com.google.protobuf:protobuf-java:${protobufVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed 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.
*/

package io.opentelemetry.sdk.contrib.trace.export;

import io.opentelemetry.internal.Utils;
import io.opentelemetry.proto.trace.v1.Span;
import io.opentelemetry.sdk.trace.TracerSdk;
import io.opentelemetry.sdk.trace.export.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.export.SimpleSampledSpansProcessor;
import io.opentelemetry.trace.Tracer;
import java.util.List;

/**
* InMemoryTracing is an utility class that uses a {@code TracerSdk} to create {@code Span}s and
* later expose the finished items in memory. Can be used to test OpenTelemetry integration.
*
* <p>Example usage:
*
* <pre><code>
* {@literal @}Test
* public void testCondition() {
* InMemoryTracing tracing = new InMemoryTracing();
* Tracer tracer = tracing.getTracer();
* tracer.spanBuilder("span").startSpan().end();
*
* {@code List<io.opentelemetry.proto.trace.v1.Span>} spans = tracing.getFinishedSpanItems();
* assertThat(spans.size()).isEqualTo(1);
* assertThat(spans.get(0).getName()).isEqualTo("span");
* }
* </code></pre>
*
* @since 0.1.0
*/
public final class InMemoryTracing {
private final TracerSdk tracer;
private final InMemorySpanExporter exporter;

/**
* Creates a new {@code InMemoryTracing} with a new {@code TracerSdk}.
*
* @since 0.1.0
*/
public InMemoryTracing() {
this(new TracerSdk());
}

/**
* Creates a new {@code InMemoryTracing} with the specified {@code TracerSdk}.
*
* @param tracer the {@code TracerSdk} to be used.
* @throws NullPointerException if {@code tracer} is {@code null}.
* @since 0.1.0
*/
public InMemoryTracing(TracerSdk tracer) {
Utils.checkNotNull(tracer, "tracer");

this.tracer = tracer;
this.exporter = InMemorySpanExporter.create();
tracer.addSpanProcessor(SimpleSampledSpansProcessor.newBuilder(exporter).build());
}

/**
* Returns a {@code Tracer} that can be used to create {@code Span}s and later recovered through
* {@link #getFinishedSpanItems()}.
*
* @return the {@code Tracer} to be used to create {@code Span}s.
* @since 0.1.0
*/
public Tracer getTracer() {
return tracer;
}

/**
* Clears the internal {@code List} of finished {@code Span}s.
*
* @since 0.1.0
*/
public void reset() {
exporter.reset();
}

/**
* Returns a copy {@code List} of the finished {@code Span}s, represented by {@code
* io.opentelemetry.proto.trace.v1.Span}.
*
* @return a {@code List} of the finished {@code Span}s.
* @since 0.1.0
*/
public List<Span> getFinishedSpanItems() {
return exporter.getFinishedSpanItems();
}

/**
* Attemps to stop all activity for the underlying tracer by calling {@code TracerSdk.shutdown()}.
*
* @since 0.1.0
*/
public void shutdown() {
tracer.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed 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.
*/

package io.opentelemetry.sdk.contrib.trace.export;

import static com.google.common.truth.Truth.assertThat;

import io.opentelemetry.proto.trace.v1.Span;
import io.opentelemetry.sdk.trace.TracerSdk;
import io.opentelemetry.trace.util.Samplers;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link InMemoryTracing}. */
@RunWith(JUnit4.class)
public class InMemoryTracingTest {
InMemoryTracing tracing = new InMemoryTracing();

@Rule public final ExpectedException thrown = ExpectedException.none();

@Test
public void defaultTracer() {
assertThat(tracing.getTracer()).isInstanceOf(TracerSdk.class);
}

@Test
public void defaultFinishedSpanItems() {
assertThat(tracing.getFinishedSpanItems().size()).isEqualTo(0);
}

@Test
public void ctor_tracer() {
TracerSdk tracer = new TracerSdk();
InMemoryTracing tracing = new InMemoryTracing(tracer);
assertThat(tracing.getTracer()).isSameInstanceAs(tracer);
}

@Test
public void ctor_nullTracer() {
thrown.expect(NullPointerException.class);
new InMemoryTracing(null);
}

@Test
public void getFinishedSpanItems() {
tracing.getTracer().spanBuilder("A").startSpan().end();
tracing.getTracer().spanBuilder("B").startSpan().end();

List<Span> finishedSpanItems = tracing.getFinishedSpanItems();
assertThat(finishedSpanItems.get(0).getName()).isEqualTo("A");
assertThat(finishedSpanItems.get(1).getName()).isEqualTo("B");
}

@Test
public void getFinishedSpanItems_sampled() {
tracing.getTracer().spanBuilder("A").startSpan().end();
tracing.getTracer().spanBuilder("B").setSampler(Samplers.neverSample()).startSpan().end();

List<Span> finishedSpanItems = tracing.getFinishedSpanItems();
assertThat(finishedSpanItems.size()).isEqualTo(1);
assertThat(finishedSpanItems.get(0).getName()).isEqualTo("A");
}

@Test
public void reset() {
tracing.getTracer().spanBuilder("A").startSpan();
tracing.getTracer().spanBuilder("B").startSpan();
tracing.reset();
assertThat(tracing.getFinishedSpanItems().size()).isEqualTo(0);

tracing.reset();
assertThat(tracing.getFinishedSpanItems().size()).isEqualTo(0);
}
}
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include ":opentelemetry-opentracing-shim"
include ":opentelemetry-proto"
include ":opentelemetry-sdk"
include ":opentelemetry-sdk-contrib-async-processor"
include ":opentelemetry-sdk-contrib-inmemory-export"
include ":opentelemetry-sdk-contrib-testbed"

project(':opentelemetry-all').projectDir = "$rootDir/all" as File
Expand All @@ -22,4 +23,6 @@ project(':opentelemetry-opentracing-shim').projectDir = "$rootDir/opentracing_sh
project(':opentelemetry-sdk').projectDir = "$rootDir/sdk" as File
project(':opentelemetry-sdk-contrib-async-processor').projectDir =
"$rootDir/sdk_contrib/async_processor" as File
project(':opentelemetry-sdk-contrib-inmemory-export').projectDir =
"$rootDir/sdk_contrib/inmemory_export" as File
project(':opentelemetry-sdk-contrib-testbed').projectDir = "$rootDir/sdk_contrib/testbed" as File

0 comments on commit 925cf41

Please sign in to comment.