Skip to content

Commit 28f0aa3

Browse files
authored
Streamline the interactions with the MDC and the OTel Context. (#34)
* Streamline the interactions with the MDC and the OTel Context. Resolves #28 * remove an errant println * formatting
1 parent 348b8ef commit 28f0aa3

File tree

9 files changed

+154
-241
lines changed

9 files changed

+154
-241
lines changed

spring-cloud-sleuth-otel-autoconfigure/src/main/java/org/springframework/cloud/sleuth/autoconfig/otel/OtelLogConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
2020
import org.slf4j.MDC;
2121

22-
import org.springframework.beans.factory.BeanFactory;
2322
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2423
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2524
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2625
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2726
import org.springframework.cloud.sleuth.autoconfig.SleuthBaggageProperties;
2827
import org.springframework.cloud.sleuth.otel.bridge.Slf4jApplicationListener;
29-
import org.springframework.cloud.sleuth.otel.bridge.Slf4jBaggageSpanProcessor;
28+
import org.springframework.cloud.sleuth.otel.bridge.Slf4jBaggageApplicationListener;
3029
import org.springframework.context.annotation.Bean;
3130
import org.springframework.context.annotation.Configuration;
3231

@@ -55,9 +54,8 @@ Slf4jApplicationListener otelSlf4jApplicationListener() {
5554

5655
@Bean
5756
@ConditionalOnMissingBean
58-
Slf4jBaggageSpanProcessor otelSlf4jBaggageSpanProcessor(BeanFactory beanFactory,
59-
SleuthBaggageProperties baggageProperties) {
60-
return new Slf4jBaggageSpanProcessor(baggageProperties.getCorrelationFields(), beanFactory);
57+
Slf4jBaggageApplicationListener otelSlf4jBaggageApplicationListener(SleuthBaggageProperties baggageProperties) {
58+
return new Slf4jBaggageApplicationListener(baggageProperties.getCorrelationFields());
6159
}
6260

6361
}

spring-cloud-sleuth-otel/src/main/java/org/springframework/cloud/sleuth/otel/bridge/OtelBaggageInScope.java

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import org.springframework.cloud.sleuth.BaggageInScope;
2929
import org.springframework.cloud.sleuth.CurrentTraceContext;
3030
import org.springframework.cloud.sleuth.TraceContext;
31-
import org.springframework.context.ApplicationEvent;
32-
import org.springframework.context.ApplicationEventPublisher;
3331

3432
/**
3533
* OpenTelemetry implementation of a {@link BaggageInScope}.
@@ -43,19 +41,16 @@ class OtelBaggageInScope implements BaggageInScope {
4341

4442
private final CurrentTraceContext currentTraceContext;
4543

46-
private final ApplicationEventPublisher publisher;
47-
4844
private final List<String> tagFields;
4945

5046
private final AtomicReference<Entry> entry = new AtomicReference<>();
5147

5248
private final AtomicReference<Scope> scope = new AtomicReference<>();
5349

5450
OtelBaggageInScope(OtelBaggageManager otelBaggageManager, CurrentTraceContext currentTraceContext,
55-
ApplicationEventPublisher publisher, List<String> tagFields, Entry entry) {
51+
List<String> tagFields, Entry entry) {
5652
this.otelBaggageManager = otelBaggageManager;
5753
this.currentTraceContext = currentTraceContext;
58-
this.publisher = publisher;
5954
this.tagFields = tagFields;
6055
this.entry.set(entry);
6156
}
@@ -109,7 +104,6 @@ private BaggageInScope doSet(TraceContext context, String value) {
109104
if (this.tagFields.stream().map(String::toLowerCase).anyMatch(s -> s.equals(entry().getKey()))) {
110105
currentSpan.setAttribute(entry().getKey(), value);
111106
}
112-
this.publisher.publishEvent(new BaggageChanged(this, baggage, entry().getKey(), value));
113107
Entry previous = entry();
114108
this.entry.set(new Entry(previous.getKey(), value, previous.getMetadata()));
115109
return this;
@@ -143,35 +137,4 @@ public void close() {
143137
}
144138
}
145139

146-
static class BaggageChanged extends ApplicationEvent {
147-
148-
/**
149-
* Baggage with the new entry.
150-
*/
151-
Baggage baggage;
152-
153-
/**
154-
* Baggage entry name.
155-
*/
156-
String name;
157-
158-
/**
159-
* Baggage entry value.
160-
*/
161-
String value;
162-
163-
BaggageChanged(OtelBaggageInScope source, Baggage baggage, String name, String value) {
164-
super(source);
165-
this.baggage = baggage;
166-
this.name = name;
167-
this.value = value;
168-
}
169-
170-
@Override
171-
public String toString() {
172-
return "BaggageChanged{" + "name='" + name + '\'' + ", value='" + value + '\'' + '}';
173-
}
174-
175-
}
176-
177140
}

spring-cloud-sleuth-otel/src/main/java/org/springframework/cloud/sleuth/otel/bridge/OtelBaggageManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private Entry entryForName(String name, io.opentelemetry.api.baggage.Baggage bag
137137
}
138138

139139
private BaggageInScope otelBaggage(Entry entry) {
140-
return new OtelBaggageInScope(this, this.currentTraceContext, this.publisher, this.tagFields, entry);
140+
return new OtelBaggageInScope(this, this.currentTraceContext, this.tagFields, entry);
141141
}
142142

143143
@Override
@@ -157,7 +157,7 @@ private BaggageInScope baggageWithValue(String name, String value) {
157157
.anyMatch(s -> s.equals(name.toLowerCase()));
158158
BaggageEntryMetadata entryMetadata = BaggageEntryMetadata.create(propagationString(remoteField));
159159
Entry entry = new Entry(name, value, entryMetadata);
160-
return new OtelBaggageInScope(this, this.currentTraceContext, this.publisher, this.tagFields, entry);
160+
return new OtelBaggageInScope(this, this.currentTraceContext, this.tagFields, entry);
161161
}
162162

163163
private String propagationString(boolean remoteField) {

spring-cloud-sleuth-otel/src/main/java/org/springframework/cloud/sleuth/otel/bridge/OtelCurrentTraceContext.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,16 @@ public OtelCurrentTraceContext(ApplicationEventPublisher publisher) {
4646
ContextStorage.addWrapper(contextStorage -> new ContextStorage() {
4747
@Override
4848
public io.opentelemetry.context.Scope attach(Context context) {
49-
Span currentSpan = Span.fromContextOrNull(Context.current());
49+
Context currentContext = Context.current();
5050
io.opentelemetry.context.Scope scope = contextStorage.attach(context);
5151
if (scope == io.opentelemetry.context.Scope.noop()) {
5252
return scope;
5353
}
54-
Span attachingSpan = Span.fromContext(context);
55-
publisher.publishEvent(new ScopeAttached(this, attachingSpan));
54+
publisher.publishEvent(new ScopeAttached(this, context));
5655
return () -> {
5756
scope.close();
5857
publisher.publishEvent(new ScopeClosed(this));
59-
publisher.publishEvent(new ScopeRestored(this, currentSpan));
58+
publisher.publishEvent(new ScopeRestored(this, currentContext));
6059
};
6160
}
6261

@@ -147,22 +146,30 @@ static class ScopeAttached extends ApplicationEvent {
147146
/**
148147
* Span corresponding to the attached scope. Might be {@code null}.
149148
*/
150-
final Span span;
149+
final Context context;
151150

152151
/**
153152
* Create a new {@code ApplicationEvent}.
154153
* @param source the object on which the event initially occurred or with which
155154
* the event is associated (never {@code null})
156-
* @param span corresponding trace context
155+
* @param context corresponding trace context
157156
*/
158-
ScopeAttached(Object source, @Nullable Span span) {
157+
ScopeAttached(Object source, @Nullable Context context) {
159158
super(source);
160-
this.span = span;
159+
this.context = context;
160+
}
161+
162+
Span getSpan() {
163+
return Span.fromContextOrNull(context);
164+
}
165+
166+
Baggage getBaggage() {
167+
return Baggage.fromContextOrNull(context);
161168
}
162169

163170
@Override
164171
public String toString() {
165-
return "ScopeAttached{span=" + span + "}";
172+
return "ScopeAttached{context: [span: " + getSpan() + "] [baggage: " + getBaggage() + "]}";
166173
}
167174

168175
}
@@ -172,22 +179,30 @@ static class ScopeRestored extends ApplicationEvent {
172179
/**
173180
* Span corresponding to the scope being restored. Might be {@code null}.
174181
*/
175-
final Span span;
182+
final Context context;
176183

177184
/**
178185
* Create a new {@code ApplicationEvent}.
179186
* @param source the object on which the event initially occurred or with which
180187
* the event is associated (never {@code null})
181-
* @param span corresponding trace context
188+
* @param context corresponding trace context
182189
*/
183-
ScopeRestored(Object source, @Nullable Span span) {
190+
ScopeRestored(Object source, @Nullable Context context) {
184191
super(source);
185-
this.span = span;
192+
this.context = context;
193+
}
194+
195+
Span getSpan() {
196+
return Span.fromContextOrNull(context);
197+
}
198+
199+
Baggage getBaggage() {
200+
return Baggage.fromContextOrNull(context);
186201
}
187202

188203
@Override
189204
public String toString() {
190-
return "ScopeRestored{span=" + span + "}";
205+
return "ScopeRestored{context: [span: " + getSpan() + "] [baggage: " + getBaggage() + "]}";
191206
}
192207

193208
}

spring-cloud-sleuth-otel/src/main/java/org/springframework/cloud/sleuth/otel/bridge/Slf4jApplicationListener.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.sleuth.otel.bridge;
1818

19+
import io.opentelemetry.api.trace.Span;
1920
import org.apache.commons.logging.Log;
2021
import org.apache.commons.logging.LogFactory;
2122
import org.slf4j.MDC;
@@ -31,19 +32,21 @@ private void onScopeAttached(OtelCurrentTraceContext.ScopeAttached event) {
3132
if (log.isTraceEnabled()) {
3233
log.trace("Got scope changed event [" + event + "]");
3334
}
34-
if (event.span != null) {
35-
MDC.put("traceId", event.span.getSpanContext().getTraceId());
36-
MDC.put("spanId", event.span.getSpanContext().getSpanId());
35+
Span span = event.getSpan();
36+
if (span != null) {
37+
MDC.put("traceId", span.getSpanContext().getTraceId());
38+
MDC.put("spanId", span.getSpanContext().getSpanId());
3739
}
3840
}
3941

4042
private void onScopeRestored(OtelCurrentTraceContext.ScopeRestored event) {
4143
if (log.isTraceEnabled()) {
42-
log.trace("Got scope changed event [" + event + "]");
44+
log.trace("Got scope restored event [" + event + "]");
4345
}
44-
if (event.span != null) {
45-
MDC.put("traceId", event.span.getSpanContext().getTraceId());
46-
MDC.put("spanId", event.span.getSpanContext().getSpanId());
46+
Span span = event.getSpan();
47+
if (span != null) {
48+
MDC.put("traceId", span.getSpanContext().getTraceId());
49+
MDC.put("spanId", span.getSpanContext().getSpanId());
4750
}
4851
}
4952

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.otel.bridge;
18+
19+
import java.util.List;
20+
import java.util.stream.Collectors;
21+
22+
import io.opentelemetry.api.baggage.Baggage;
23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
25+
import org.slf4j.MDC;
26+
27+
import org.springframework.context.ApplicationEvent;
28+
import org.springframework.context.ApplicationListener;
29+
30+
public class Slf4jBaggageApplicationListener implements ApplicationListener<ApplicationEvent> {
31+
32+
private static final Log log = LogFactory.getLog(Slf4jBaggageApplicationListener.class);
33+
34+
private final List<String> lowerCaseCorrelationFields;
35+
36+
private final List<String> correlationFields;
37+
38+
public Slf4jBaggageApplicationListener(List<String> correlationFields) {
39+
this.lowerCaseCorrelationFields = correlationFields.stream().map(String::toLowerCase)
40+
.collect(Collectors.toList());
41+
this.correlationFields = correlationFields;
42+
}
43+
44+
private void onScopeAttached(OtelCurrentTraceContext.ScopeAttached event) {
45+
if (log.isTraceEnabled()) {
46+
log.trace("Got scope attached event [" + event + "]");
47+
}
48+
if (event.getBaggage() != null) {
49+
putEntriesIntoMdc(event.getBaggage());
50+
}
51+
}
52+
53+
private void onScopeRestored(OtelCurrentTraceContext.ScopeRestored event) {
54+
if (log.isTraceEnabled()) {
55+
log.trace("Got scope restored event [" + event + "]");
56+
}
57+
if (event.getBaggage() != null) {
58+
putEntriesIntoMdc(event.getBaggage());
59+
}
60+
}
61+
62+
private void putEntriesIntoMdc(Baggage baggage) {
63+
baggage.forEach((key, baggageEntry) -> {
64+
if (lowerCaseCorrelationFields.contains(key.toLowerCase())) {
65+
MDC.put(key, baggageEntry.getValue());
66+
}
67+
});
68+
}
69+
70+
private void onScopeClosed(OtelCurrentTraceContext.ScopeClosed event) {
71+
if (log.isTraceEnabled()) {
72+
log.trace("Got scope closed event [" + event + "]");
73+
}
74+
correlationFields.forEach(MDC::remove);
75+
}
76+
77+
@Override
78+
public void onApplicationEvent(ApplicationEvent event) {
79+
if (event instanceof OtelCurrentTraceContext.ScopeAttached) {
80+
onScopeAttached((OtelCurrentTraceContext.ScopeAttached) event);
81+
}
82+
else if (event instanceof OtelCurrentTraceContext.ScopeClosed) {
83+
onScopeClosed((OtelCurrentTraceContext.ScopeClosed) event);
84+
}
85+
else if (event instanceof OtelCurrentTraceContext.ScopeRestored) {
86+
onScopeRestored((OtelCurrentTraceContext.ScopeRestored) event);
87+
}
88+
}
89+
90+
}

0 commit comments

Comments
 (0)