Skip to content

Commit

Permalink
Merge branch '1.0.x' into 1.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Jul 21, 2023
2 parents ff3eb5c + 091dbec commit b0e1e08
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 86 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ task deleteLockFiles(type: Delete) {
}

wrapper {
gradleVersion = '8.2'
gradleVersion = '8.2.1'
}

defaultTasks 'build'
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public TraceContext context() {
public Scope newScope(TraceContext context) {
OtelTraceContext otelTraceContext = (OtelTraceContext) context;
if (otelTraceContext == null) {
return io.opentelemetry.context.Scope::noop;
return new WrappedScope(io.opentelemetry.context.Scope.noop());
}
Context current = Context.current();
Context old = otelTraceContext.context();
Expand All @@ -76,24 +76,20 @@ public Scope newScope(TraceContext context) {
Baggage oldBaggage = Baggage.fromContext(old);
boolean sameBaggage = sameBaggage(currentBaggage, oldBaggage);
if (sameSpan && sameBaggage) {
return io.opentelemetry.context.Scope::noop;
return new WrappedScope(io.opentelemetry.context.Scope.noop());
}
Baggage updatedBaggage = mergeBaggage(currentBaggage, oldBaggage);
Context newContext = old.with(fromContext).with(updatedBaggage);
io.opentelemetry.context.Scope attach = newContext.makeCurrent();
otelTraceContext.updateContext(newContext);
return () -> {
otelTraceContext.updateContext(old);
attach.close();
};
return new WrappedScope(attach, otelTraceContext, old);
}

private static Baggage mergeBaggage(Baggage currentBaggage, Baggage oldBaggage) {
BaggageBuilder baggageBuilder = currentBaggage.toBuilder();
oldBaggage.forEach(
(key, baggageEntry) -> baggageBuilder.put(key, baggageEntry.getValue(), baggageEntry.getMetadata()));
Baggage updatedBaggage = baggageBuilder.build();
return updatedBaggage;
return baggageBuilder.build();
}

private boolean sameBaggage(Baggage currentBaggage, Baggage oldBaggage) {
Expand All @@ -104,7 +100,7 @@ private boolean sameBaggage(Baggage currentBaggage, Baggage oldBaggage) {
public Scope maybeScope(TraceContext context) {
if (context == null) {
io.opentelemetry.context.Scope scope = Context.root().makeCurrent();
return scope::close;
return new WrappedScope(scope);
}
return newScope(context);
}
Expand All @@ -129,4 +125,33 @@ public ExecutorService wrap(ExecutorService delegate) {
return Context.current().wrap(delegate);
}

static class WrappedScope implements Scope {

final io.opentelemetry.context.Scope scope;

final OtelTraceContext currentOtelTraceContext;

final Context oldContext;

WrappedScope(io.opentelemetry.context.Scope scope) {
this(scope, null, null);
}

WrappedScope(io.opentelemetry.context.Scope scope, OtelTraceContext currentOtelTraceContext,
Context oldContext) {
this.scope = scope;
this.currentOtelTraceContext = currentOtelTraceContext;
this.oldContext = oldContext;
}

@Override
public void close() {
if (this.currentOtelTraceContext != null) {
currentOtelTraceContext.updateContext(oldContext);
}
this.scope.close();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ public Span start() {
@Override
public Span name(String name) {
this.delegate.updateName(name);
return new OtelSpan(this.delegate);
return this;
}

@Override
public Span event(String value) {
this.delegate.addEvent(value);
return new OtelSpan(this.delegate);
return this;
}

@Override
Expand All @@ -104,7 +104,7 @@ public Span event(String value, long time, TimeUnit timeUnit) {
@Override
public Span tag(String key, String value) {
this.delegate.setAttribute(key, value);
return new OtelSpan(this.delegate);
return this;
}

@Override
Expand Down Expand Up @@ -141,7 +141,7 @@ public Span remoteIpAndPort(String ip, int port) {
public Span error(Throwable throwable) {
this.delegate.recordException(throwable);
this.delegate.setStatus(StatusCode.ERROR, throwable.getMessage());
return new OtelSpan(this.delegate);
return this;
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public Span nextSpan(Span parent) {
@Override
public Tracer.SpanInScope withSpan(Span span) {
io.opentelemetry.api.trace.Span delegate = delegate(span);
return new OtelSpanInScope((OtelSpan) span, delegate);
CurrentTraceContext.Scope scope = this.otelCurrentTraceContext
.maybeScope(OtelSpan.fromOtel(delegate).context());
return new WrappedSpanInScope(scope);
}

private io.opentelemetry.api.trace.Span delegate(Span span) {
Expand Down Expand Up @@ -195,4 +197,19 @@ public interface EventPublisher {

}

static class WrappedSpanInScope implements SpanInScope {

final CurrentTraceContext.Scope scope;

WrappedSpanInScope(CurrentTraceContext.Scope scope) {
this.scope = scope;
}

@Override
public void close() {
this.scope.close();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,10 @@
*/
package io.micrometer.tracing.otel.bridge;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.micrometer.tracing.Baggage;
import io.micrometer.tracing.BaggageInScope;
import io.micrometer.tracing.Link;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.*;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
Expand All @@ -39,6 +29,13 @@
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn;
import static org.assertj.core.api.BDDAssertions.then;

Expand Down Expand Up @@ -365,4 +362,50 @@ void should_add_links() throws InterruptedException {
then(linkData.getAttributes().asMap()).containsEntry(AttributeKey.stringKey("tag"), "value");
}

@Test
void should_work_with_with_scope_for_tracer_and_current_trace_context() {
Span span = tracer.nextSpan();

then(io.opentelemetry.api.trace.Span.current()).isSameAs(io.opentelemetry.api.trace.Span.getInvalid());
then(tracer.currentSpan()).isNull();

try (Tracer.SpanInScope ws = tracer.withSpan(span.start())) {
OtelCurrentTraceContext.WrappedScope wrappedScope = getScopeFromTracerScope(
(OtelTracer.WrappedSpanInScope) ws);
then(wrappedScope.scope).isNotSameAs(Scope.noop());
then(io.opentelemetry.api.trace.Span.current()).isSameAs(OtelSpan.toOtel(span));
then(tracer.currentSpan()).isEqualTo(span);
try (Tracer.SpanInScope ws2 = tracer.withSpan(span)) {
OtelCurrentTraceContext.WrappedScope wrappedScope2 = getScopeFromTracerScope(
(OtelTracer.WrappedSpanInScope) ws2);
then(wrappedScope2.scope).isSameAs(Scope.noop());
then(io.opentelemetry.api.trace.Span.current()).isSameAs(OtelSpan.toOtel(span));
then(tracer.currentSpan()).isEqualTo(span);
}
}

then(io.opentelemetry.api.trace.Span.current()).isSameAs(io.opentelemetry.api.trace.Span.getInvalid());
then(tracer.currentSpan()).isNull();

try (CurrentTraceContext.Scope ws = otelCurrentTraceContext.maybeScope(span.context())) {
Scope scope = ((OtelCurrentTraceContext.WrappedScope) ws).scope;
then(scope).isNotSameAs(Scope.noop());
then(io.opentelemetry.api.trace.Span.current()).isSameAs(OtelSpan.toOtel(span));
then(tracer.currentSpan()).isEqualTo(span);
try (CurrentTraceContext.Scope ws2 = otelCurrentTraceContext.maybeScope(span.context())) {
Scope scope2 = ((OtelCurrentTraceContext.WrappedScope) ws2).scope;
then(scope2).isSameAs(Scope.noop());
then(io.opentelemetry.api.trace.Span.current()).isSameAs(OtelSpan.toOtel(span));
then(tracer.currentSpan()).isEqualTo(span);
}
}

then(io.opentelemetry.api.trace.Span.current()).isSameAs(io.opentelemetry.api.trace.Span.getInvalid());
then(tracer.currentSpan()).isNull();
}

private static OtelCurrentTraceContext.WrappedScope getScopeFromTracerScope(OtelTracer.WrappedSpanInScope ws) {
return (OtelCurrentTraceContext.WrappedScope) ws.scope;
}

}

0 comments on commit b0e1e08

Please sign in to comment.