Skip to content

Commit

Permalink
Add otel current context detection
Browse files Browse the repository at this point in the history
  • Loading branch information
cindy-peng committed May 5, 2024
1 parent e59fd6b commit 4ff6738
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
8 changes: 4 additions & 4 deletions google-cloud-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.opentelemetry</groupId>-->
<!-- <artifactId>opentelemetry-context</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.Iterables;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -204,10 +205,12 @@ public Builder loadW3CTraceParentContext(String traceParent) {
*/
@CanIgnoreReturnValue
public Builder loadOpenTelemetryContext() {
if (Span.current().getSpanContext() != null && Span.current().getSpanContext().isValid()) {
setTraceId(Span.current().getSpanContext().getTraceId());
setSpanId(Span.current().getSpanContext().getSpanId());
setTraceSampled(Span.current().getSpanContext().isSampled());
io.opentelemetry.context.Context currentContext = io.opentelemetry.context.Context.current();
SpanContext spanContext = Span.fromContext(currentContext).getSpanContext();
if (spanContext != null && spanContext.isValid()) {
setTraceId(spanContext.getTraceId());
setSpanId(spanContext.getSpanId());
setTraceSampled(spanContext.isSampled());
}
return this;
}
Expand Down
21 changes: 6 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@
<artifactId>opentelemetry-api</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.opentelemetry</groupId>-->
<!-- <artifactId>opentelemetry-context</artifactId>-->
<!-- <version>${opentelemetry.version}</version>-->
<!-- <optional>false</optional>-->
<!-- </dependency>-->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<!-- END OpenTelemetry -->
<dependency>
<groupId>com.google.api.grpc</groupId>
Expand Down Expand Up @@ -179,15 +178,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>org.objenesis:objenesis</ignoredUnusedDeclaredDependency>
<!-- <ignoredUnusedDeclaredDependency>io.opentelemetry:opentelemetry-context</ignoredUnusedDeclaredDependency>-->
</ignoredUnusedDeclaredDependencies>
<!-- <ignoredUnusedDeclaredDependencies>io.opentelemetry:opentelemetry-context</ignoredUnusedDeclaredDependencies>-->
<!-- ignore annotations for "non-test scoped" warnings -->
<!-- <ignoredNonTestScopedDependencies>-->
<!-- <ignoredNonTestScopedDependency>io.opentelemetry:opentelemetry-context</ignoredNonTestScopedDependency>-->
<!-- </ignoredNonTestScopedDependencies>-->
<ignoredUnusedDeclaredDependencies>org.objenesis:objenesis</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
</plugins>
Expand Down

0 comments on commit 4ff6738

Please sign in to comment.