Skip to content

Commit

Permalink
Merge branch 'main' into brave-5.17.1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak authored Jan 11, 2024
2 parents d11d803 + c7db577 commit 5bbc418
Show file tree
Hide file tree
Showing 30 changed files with 906 additions and 251 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ nexusPublishing {
}

wrapper {
gradleVersion = '8.4'
gradleVersion = '8.5'
}

defaultTasks 'build'
1 change: 1 addition & 0 deletions docs/antora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ asciidoc:
chomp: 'all'
include-java: 'example$docs-src/test/java/io/micrometer/docs'
include-bridges-java: 'example$bridges-src'
include-integration-tests: 'example$/integration-src'
include-resources: 'example$docs-src/test/resources'
2 changes: 2 additions & 0 deletions docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies {
testImplementation libs.mockitoCore
testImplementation libs.assertj
testImplementation libs.springContext
testImplementation 'io.projectreactor:reactor-core'
testImplementation 'io.projectreactor:reactor-core-micrometer'
}

antora {
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/examples/integration-src
17 changes: 16 additions & 1 deletion docs/modules/ROOT/pages/configuring.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,22 @@ In order to make https://micrometer.io/docs/contextPropagation[Context Propagati

[source,java,subs=+attributes]
-----
include::{include-java}/tracing/TracingConfiguringTests.java[tags=span_thread_local_accessor,indent=0]
include::{include-java}/tracing/TracingConfiguringTests.java[tags=thread_local_accessors,indent=0]
-----

The `ObservationAwareSpanThreadLocalAccessor` is required to propagate manually created spans (not the ones that are governed by Observations). The `ObservationAwareBaggageThreadLocalAccessor` is required to propagate baggage created by the user.

With Project Reactor one should set the values of `Observation`, `Span` or `BaggageToPropagate` in the Reactor Context as presented below.

[source,java,subs=+attributes]
-----
// Setup example
include::{include-integration-tests}/src/test/java/io/micrometer/tracing/test/contextpropagation/AbstractObservationAwareSpanThreadLocalAccessorTests.java[tags=setup,indent=0]
include::{include-integration-tests}/src/test/java/io/micrometer/tracing/test/contextpropagation/AbstractObservationAwareSpanThreadLocalAccessorTests.java[tags=setup_accessors,indent=0]
// Usage example
include::{include-integration-tests}/src/test/java/io/micrometer/tracing/test/contextpropagation/AbstractObservationAwareSpanThreadLocalAccessorTests.java[tags=docs,indent=0]
-----

=== Exemplars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.contextpropagation.ObservationAwareBaggageThreadLocalAccessor;
import io.micrometer.tracing.contextpropagation.ObservationAwareSpanThreadLocalAccessor;
import io.micrometer.tracing.handler.DefaultTracingObservationHandler;
import io.micrometer.tracing.handler.PropagatingReceiverTracingObservationHandler;
Expand Down Expand Up @@ -107,12 +108,16 @@ void yourCodeToMeasure() {

void example_of_setting_ObservationAwareSpanThreadLocalAccessor() {
Tracer tracer = null;
ObservationRegistry registry = ObservationRegistry.create();

// tag::span_thread_local_accessor[]
// tag::thread_local_accessors[]
ContextRegistry.getInstance().registerThreadLocalAccessor(new ObservationAwareSpanThreadLocalAccessor(tracer));
// end::span_thread_local_accessor[]
ContextRegistry.getInstance()
.registerThreadLocalAccessor(new ObservationAwareBaggageThreadLocalAccessor(registry, tracer));
// end::thread_local_accessors[]

ContextRegistry.getInstance().removeThreadLocalAccessor(ObservationAwareSpanThreadLocalAccessor.KEY);
ContextRegistry.getInstance().removeThreadLocalAccessor(ObservationAwareBaggageThreadLocalAccessor.KEY);
}

void example_of_exemplars() {
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
micrometer = "1.12.1"
micrometer = "1.12.2"
micrometerContextPropagation="1.1.0"
junit = "5.10.1"
javaxServlet="4.0.1"
Expand All @@ -9,10 +9,10 @@ otelInstrumentation="1.32.0-alpha"
aopalliance="1.0"
# TODO: Update to 1.0.0 after https://github.com/open-telemetry/opentelemetry-java/pull/6129
zipkinAws="0.24.0"
otelBom="1.33.0"
otelBom="1.34.0"
logback = "1.4.14"
wavefrontSdk="3.4.3"
otelBomAlpha="1.33.0-alpha"
otelBomAlpha="1.34.0-alpha"
log4j = "2.22.1"
wavefrontReporter="1.7.16"
slf4j = "1.7.36"
Expand All @@ -27,9 +27,9 @@ braveBom = "6.0.0"
# When present, use the overload of AsyncZipkinSpanHandler.build, allowing custom formats like
# OTLP, Wavefront or Stackdriver.
zipkinReporterBom = "2.17.2"
reactorBom = "2022.0.14"
reactorBom = "2022.0.15"
asmForPlugins = "7.3.1"
javaFormatForPlugins = "0.0.40"
javaFormatForPlugins = "0.0.41"
jsr305 = "3.0.2"
aspectjweaver = "1.9.21"
spring = "5.3.31"
Expand Down Expand Up @@ -77,7 +77,7 @@ plugin-nebulaProject = { module = "com.netflix.nebula:nebula-project-plugin", ve
plugin-nebulaInfo = { module = "com.netflix.nebula:gradle-info-plugin", version = "12.1.6" }
plugin-noHttp = { module = "io.spring.nohttp:nohttp-gradle", version = "0.0.11" }
plugin-nexusPublish = { module = "io.github.gradle-nexus:publish-plugin", version = "1.3.0" }
plugin-javaformat = { module = "io.spring.javaformat:spring-javaformat-gradle-plugin", version = "0.0.40" }
plugin-javaformat = { module = "io.spring.javaformat:spring-javaformat-gradle-plugin", version = "0.0.41" }
plugin-jmh = { module = "me.champeau.jmh:jmh-gradle-plugin", version = "0.7.1" }
plugin-spring-antora = { module = "io.spring.gradle.antora:spring-antora-plugin", version = "0.0.1" }
plugin-antora = { module = "org.antora:gradle-antora-plugin", version = "1.0.0" }
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
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.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class BraveBaggageInScope implements Baggage, BaggageInScope {

private final List<String> tagFields;

// Null TC would happen pretty much in exceptional cases (there was no span in scope)
// but someone wanted to set baggage
@Nullable
private brave.propagation.TraceContext traceContext;

Expand All @@ -53,7 +55,7 @@ class BraveBaggageInScope implements Baggage, BaggageInScope {
@Nullable Span span, List<String> tagFields) {
this.delegate = delegate;
this.traceContext = traceContext;
this.previousBaggage = delegate.getValue();
this.previousBaggage = traceContext != null ? delegate.getValue(traceContext) : delegate.getValue();
this.tagFields = tagFields;
this.span = span;
}
Expand All @@ -77,10 +79,16 @@ public String get(TraceContext traceContext) {
@Deprecated
public Baggage set(String value) {
if (this.traceContext != null) {
this.delegate.updateValue(this.traceContext, value);
boolean success = this.delegate.updateValue(this.traceContext, value);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on set [" + success + "]. Provided value [" + value + "]");
}
}
else {
this.delegate.updateValue(value);
boolean success = this.delegate.updateValue(value);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on set [" + success + "]. Provided value [" + value + "]");
}
}
tagSpanIfOnTagList();
return this;
Expand All @@ -99,7 +107,11 @@ private void tagSpanIfOnTagList() {
@Deprecated
public Baggage set(TraceContext traceContext, String value) {
brave.propagation.TraceContext braveContext = updateBraveTraceContext(traceContext);
this.delegate.updateValue(braveContext, value);
boolean success = this.delegate.updateValue(braveContext, value);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on set [" + success + "]. Provided value [" + value
+ "], trace context [" + traceContext + "]");
}
tagSpanIfOnTagList();
return this;
}
Expand All @@ -123,10 +135,16 @@ public BaggageInScope makeCurrent() {
@Override
public BaggageInScope makeCurrent(String value) {
if (this.traceContext != null) {
this.delegate.updateValue(this.traceContext, value);
boolean success = this.delegate.updateValue(this.traceContext, value);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on make current [" + success + "]");
}
}
else {
this.delegate.updateValue(value);
boolean success = this.delegate.updateValue(value);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on make current [" + success + "]");
}
}
tagSpanIfOnTagList();
return makeCurrent();
Expand All @@ -135,16 +153,29 @@ public BaggageInScope makeCurrent(String value) {
@Override
public BaggageInScope makeCurrent(TraceContext traceContext, String value) {
brave.propagation.TraceContext braveContext = updateBraveTraceContext(traceContext);
this.delegate.updateValue(braveContext, value);
boolean success = this.delegate.updateValue(braveContext, value);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on close [" + success + "]. Provided value [" + value
+ "], trace context [" + traceContext + "]");
}
tagSpanIfOnTagList();
return makeCurrent();
}

@Override
public void close() {
if (this.traceContext != null) {
this.delegate.updateValue(this.traceContext, this.previousBaggage);
boolean success = this.delegate.updateValue(this.traceContext, this.previousBaggage);
if (logger.isTraceEnabled()) {
logger.trace("Managed to update the baggage on close [" + success + "]");
}
}
}

@Override
public String toString() {
return "BraveBaggageInScope{" + "delegate=" + delegate + ", previousBaggage='" + previousBaggage + '\''
+ ", tagFields=" + tagFields + ", traceContext=" + traceContext + ", span=" + span + '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import brave.Span;
import brave.Tracing;
import brave.baggage.BaggageField;
import io.micrometer.tracing.Baggage;
import io.micrometer.tracing.BaggageInScope;
import io.micrometer.tracing.BaggageManager;
import io.micrometer.tracing.TraceContext;
import io.micrometer.common.lang.Nullable;
import io.micrometer.tracing.*;

import java.io.Closeable;
import java.util.Collections;
Expand All @@ -38,6 +36,9 @@ public class BraveBaggageManager implements Closeable, BaggageManager {

private final List<String> tagFields;

@Nullable
private Tracer tracer;

/**
* Create an instance of {@link BraveBaggageManager}.
* @param tagFields fields of baggage keys that should become tags on a span
Expand Down Expand Up @@ -101,7 +102,11 @@ private BraveBaggageInScope baggage(String name) {
}

// Taken from BraveField
private static Span currentSpan() {
private Span currentSpan() {
if (tracer != null) {
io.micrometer.tracing.Span span = tracer.currentSpan();
return BraveSpan.toBrave(span);
}
Tracing tracing = Tracing.current();
return tracing != null ? tracing.tracer().currentSpan() : null;
}
Expand All @@ -127,4 +132,8 @@ public void close() {
// We used to cache baggage fields
}

void setTracer(Tracer tracer) {
this.tracer = tracer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public BraveTracer(brave.Tracer tracer, CurrentTraceContext context, BaggageMana
this.tracer = tracer;
this.braveBaggageManager = braveBaggageManager;
this.currentTraceContext = context;
if (braveBaggageManager instanceof BraveBaggageManager) {
((BraveBaggageManager) braveBaggageManager).setTracer(this);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public Propagation<String> get() {
return this;
}

@Override
public Propagation<String> get() {
return this;
}

@Override
public List<String> keys() {
return FIELDS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,9 @@ public void close() {
}
}

@Override
public String toString() {
return "OtelBaggageInScope{" + "tagFields=" + tagFields + ", entry=" + entry + '}';
}

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

import java.util.Map;

import io.micrometer.tracing.Baggage;
import io.micrometer.tracing.BaggageInScope;
import io.micrometer.tracing.BaggageManager;
import io.micrometer.tracing.CurrentTraceContext;
import io.micrometer.tracing.ScopedSpan;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.SpanCustomizer;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.*;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;

import java.util.Map;

/**
* OpenTelemetry implementation of a {@link Tracer}.
*
Expand Down Expand Up @@ -124,6 +116,9 @@ public SpanCustomizer currentSpanCustomizer() {
public Span currentSpan() {
OtelTraceContext context = (OtelTraceContext) this.otelCurrentTraceContext.context();
if (context != null && context.span != null) {
if (io.opentelemetry.api.trace.Span.getInvalid().equals(context.span)) {
return null;
}
return new OtelSpan(context);
}
io.opentelemetry.api.trace.Span currentSpan = io.opentelemetry.api.trace.Span.current();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
testImplementation libs.wiremock
testImplementation libs.awaitility
testImplementation libs.logback
testImplementation 'io.projectreactor:reactor-core'
testImplementation 'org.testcontainers:testcontainers'
testImplementation 'org.testcontainers:junit-jupiter'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2023 VMware, Inc.
*
* 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
*
* https://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.micrometer.tracing.contextpropagation;

import java.util.Map;

public class TestBaggageThreadLocalAccessor {

public static Map<Thread, ? extends Object> baggageInScope(ObservationAwareBaggageThreadLocalAccessor accessor) {
return accessor.baggageInScope;
}

}
Loading

0 comments on commit 5bbc418

Please sign in to comment.