Skip to content

Commit 037fab7

Browse files
authored
Minor cleanup (#35)
* fix the test by overriding the context storage * Some minor cleanup & extraction of the context storage wrapper for testing re-use.
1 parent 28f0aa3 commit 037fab7

File tree

11 files changed

+210
-167
lines changed

11 files changed

+210
-167
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.regex.Pattern;
2020

2121
import io.opentelemetry.api.OpenTelemetry;
22+
import io.opentelemetry.context.ContextStorage;
2223
import io.opentelemetry.context.propagation.ContextPropagators;
2324

2425
import org.springframework.beans.factory.ObjectProvider;
@@ -43,6 +44,7 @@
4344
import org.springframework.cloud.sleuth.instrument.web.HttpServerRequestParser;
4445
import org.springframework.cloud.sleuth.instrument.web.HttpServerResponseParser;
4546
import org.springframework.cloud.sleuth.instrument.web.SkipPatternProvider;
47+
import org.springframework.cloud.sleuth.otel.bridge.EventPublishingContextWrapper;
4648
import org.springframework.cloud.sleuth.otel.bridge.OtelBaggageManager;
4749
import org.springframework.cloud.sleuth.otel.bridge.OtelCurrentTraceContext;
4850
import org.springframework.cloud.sleuth.otel.bridge.OtelHttpClientHandler;
@@ -78,11 +80,12 @@ Tracer otelTracerBridge(io.opentelemetry.api.trace.Tracer tracer, ApplicationEve
7880
sleuthBaggageProperties.getRemoteFields(), sleuthBaggageProperties.getTagFields(), publisher));
7981
}
8082

81-
// Both CurrentTraceContext & ContextStorage wrapper
83+
// Both CurrentTraceContext & application of a ContextStorage wrapper
8284
@Bean
8385
@ConditionalOnMissingBean
8486
OtelCurrentTraceContext otelCurrentTraceContext(ApplicationEventPublisher publisher) {
85-
return new OtelCurrentTraceContext(publisher);
87+
ContextStorage.addWrapper(new EventPublishingContextWrapper(publisher));
88+
return new OtelCurrentTraceContext();
8689
}
8790

8891
@Bean
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2013-2021 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.function.Function;
20+
21+
import io.opentelemetry.api.baggage.Baggage;
22+
import io.opentelemetry.api.trace.Span;
23+
import io.opentelemetry.context.Context;
24+
import io.opentelemetry.context.ContextStorage;
25+
26+
import org.springframework.context.ApplicationEvent;
27+
import org.springframework.context.ApplicationEventPublisher;
28+
import org.springframework.lang.Nullable;
29+
30+
public final class EventPublishingContextWrapper implements Function<ContextStorage, ContextStorage> {
31+
32+
private final ApplicationEventPublisher publisher;
33+
34+
public EventPublishingContextWrapper(ApplicationEventPublisher publisher) {
35+
this.publisher = publisher;
36+
}
37+
38+
@Override
39+
public ContextStorage apply(ContextStorage contextStorage) {
40+
return new ContextStorage() {
41+
@Override
42+
public io.opentelemetry.context.Scope attach(Context context) {
43+
Context currentContext = Context.current();
44+
io.opentelemetry.context.Scope scope = contextStorage.attach(context);
45+
if (scope == io.opentelemetry.context.Scope.noop()) {
46+
return scope;
47+
}
48+
publisher.publishEvent(new ScopeAttachedEvent(this, context));
49+
return () -> {
50+
scope.close();
51+
publisher.publishEvent(new ScopeClosedEvent(this));
52+
publisher.publishEvent(new ScopeRestoredEvent(this, currentContext));
53+
};
54+
}
55+
56+
@Override
57+
public Context current() {
58+
return contextStorage.current();
59+
}
60+
};
61+
}
62+
63+
public static class ScopeAttachedEvent extends ApplicationEvent {
64+
65+
/**
66+
* Context corresponding to the attached scope. Might be {@code null}.
67+
*/
68+
final Context context;
69+
70+
/**
71+
* Create a new {@code ApplicationEvent}.
72+
* @param source the object on which the event initially occurred or with which
73+
* the event is associated (never {@code null})
74+
* @param context corresponding otel context
75+
*/
76+
public ScopeAttachedEvent(Object source, @Nullable Context context) {
77+
super(source);
78+
this.context = context;
79+
}
80+
81+
Span getSpan() {
82+
return Span.fromContextOrNull(context);
83+
}
84+
85+
Baggage getBaggage() {
86+
return Baggage.fromContextOrNull(context);
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return "ScopeAttached{context: [span: " + getSpan() + "] [baggage: " + getBaggage() + "]}";
92+
}
93+
94+
}
95+
96+
public static class ScopeClosedEvent extends ApplicationEvent {
97+
98+
/**
99+
* Create a new {@code ApplicationEvent}.
100+
* @param source the object on which the event initially occurred or with which
101+
* the event is associated (never {@code null})
102+
*/
103+
public ScopeClosedEvent(Object source) {
104+
super(source);
105+
}
106+
107+
}
108+
109+
public static class ScopeRestoredEvent extends ApplicationEvent {
110+
111+
/**
112+
* {@link Context} corresponding to the scope being restored. Might be
113+
* {@code null}.
114+
*/
115+
final Context context;
116+
117+
/**
118+
* Create a new {@code ApplicationEvent}.
119+
* @param source the object on which the event initially occurred or with which
120+
* the event is associated (never {@code null})
121+
* @param context corresponding otel context
122+
*/
123+
public ScopeRestoredEvent(Object source, @Nullable Context context) {
124+
super(source);
125+
this.context = context;
126+
}
127+
128+
Span getSpan() {
129+
return Span.fromContextOrNull(context);
130+
}
131+
132+
Baggage getBaggage() {
133+
return Baggage.fromContextOrNull(context);
134+
}
135+
136+
@Override
137+
public String toString() {
138+
return "ScopeRestored{context: [span: " + getSpan() + "] [baggage: " + getBaggage() + "]}";
139+
}
140+
141+
}
142+
143+
}

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

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525
import io.opentelemetry.api.trace.Span;
2626
import io.opentelemetry.api.trace.SpanContext;
2727
import io.opentelemetry.context.Context;
28-
import io.opentelemetry.context.ContextStorage;
2928

3029
import org.springframework.cloud.sleuth.CurrentTraceContext;
3130
import org.springframework.cloud.sleuth.TraceContext;
32-
import org.springframework.context.ApplicationEvent;
33-
import org.springframework.context.ApplicationEventPublisher;
34-
import org.springframework.lang.Nullable;
3531

3632
/**
3733
* OpenTelemetry implementation of a {@link CurrentTraceContext}.
@@ -42,30 +38,6 @@
4238
*/
4339
public class OtelCurrentTraceContext implements CurrentTraceContext {
4440

45-
public OtelCurrentTraceContext(ApplicationEventPublisher publisher) {
46-
ContextStorage.addWrapper(contextStorage -> new ContextStorage() {
47-
@Override
48-
public io.opentelemetry.context.Scope attach(Context context) {
49-
Context currentContext = Context.current();
50-
io.opentelemetry.context.Scope scope = contextStorage.attach(context);
51-
if (scope == io.opentelemetry.context.Scope.noop()) {
52-
return scope;
53-
}
54-
publisher.publishEvent(new ScopeAttached(this, context));
55-
return () -> {
56-
scope.close();
57-
publisher.publishEvent(new ScopeClosed(this));
58-
publisher.publishEvent(new ScopeRestored(this, currentContext));
59-
};
60-
}
61-
62-
@Override
63-
public Context current() {
64-
return contextStorage.current();
65-
}
66-
});
67-
}
68-
6941
@Override
7042
public TraceContext context() {
7143
Span currentSpan = Span.current();
@@ -141,83 +113,4 @@ public ExecutorService wrap(ExecutorService delegate) {
141113
return Context.current().wrap(delegate);
142114
}
143115

144-
static class ScopeAttached extends ApplicationEvent {
145-
146-
/**
147-
* Span corresponding to the attached scope. Might be {@code null}.
148-
*/
149-
final Context context;
150-
151-
/**
152-
* Create a new {@code ApplicationEvent}.
153-
* @param source the object on which the event initially occurred or with which
154-
* the event is associated (never {@code null})
155-
* @param context corresponding trace context
156-
*/
157-
ScopeAttached(Object source, @Nullable Context context) {
158-
super(source);
159-
this.context = context;
160-
}
161-
162-
Span getSpan() {
163-
return Span.fromContextOrNull(context);
164-
}
165-
166-
Baggage getBaggage() {
167-
return Baggage.fromContextOrNull(context);
168-
}
169-
170-
@Override
171-
public String toString() {
172-
return "ScopeAttached{context: [span: " + getSpan() + "] [baggage: " + getBaggage() + "]}";
173-
}
174-
175-
}
176-
177-
static class ScopeRestored extends ApplicationEvent {
178-
179-
/**
180-
* Span corresponding to the scope being restored. Might be {@code null}.
181-
*/
182-
final Context context;
183-
184-
/**
185-
* Create a new {@code ApplicationEvent}.
186-
* @param source the object on which the event initially occurred or with which
187-
* the event is associated (never {@code null})
188-
* @param context corresponding trace context
189-
*/
190-
ScopeRestored(Object source, @Nullable Context context) {
191-
super(source);
192-
this.context = context;
193-
}
194-
195-
Span getSpan() {
196-
return Span.fromContextOrNull(context);
197-
}
198-
199-
Baggage getBaggage() {
200-
return Baggage.fromContextOrNull(context);
201-
}
202-
203-
@Override
204-
public String toString() {
205-
return "ScopeRestored{context: [span: " + getSpan() + "] [baggage: " + getBaggage() + "]}";
206-
}
207-
208-
}
209-
210-
static class ScopeClosed extends ApplicationEvent {
211-
212-
/**
213-
* Create a new {@code ApplicationEvent}.
214-
* @param source the object on which the event initially occurred or with which
215-
* the event is associated (never {@code null})
216-
*/
217-
ScopeClosed(Object source) {
218-
super(source);
219-
}
220-
221-
}
222-
223116
}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,12 @@ public Span handleSend(HttpClientRequest request, TraceContext parent) {
9797

9898
private Span span(HttpClientRequest request, Context context) {
9999
try (Scope scope = context.makeCurrent()) {
100-
io.opentelemetry.api.trace.Span span = io.opentelemetry.api.trace.Span.fromContext(context);
101-
if (span.isRecording()) {
102-
String remoteIp = request.remoteIp();
103-
if (StringUtils.hasText(remoteIp)) {
104-
span.setAttribute(SemanticAttributes.NET_PEER_IP, remoteIp);
105-
}
106-
span.setAttribute(SemanticAttributes.NET_PEER_PORT, request.remotePort());
100+
io.opentelemetry.api.trace.Span span = io.opentelemetry.api.trace.Span.current();
101+
String remoteIp = request.remoteIp();
102+
if (StringUtils.hasText(remoteIp)) {
103+
span.setAttribute(SemanticAttributes.NET_PEER_IP, remoteIp);
107104
}
105+
span.setAttribute(SemanticAttributes.NET_PEER_PORT, request.remotePort());
108106
return OtelSpan.fromOtel(span);
109107
}
110108
}
@@ -133,24 +131,24 @@ protected void onResponse(io.opentelemetry.api.trace.Span span, HttpClientRespon
133131

134132
@Override
135133
public void handleReceive(HttpClientResponse response, Span span) {
136-
if (OtelSpan.toOtel(span).equals(io.opentelemetry.api.trace.Span.getInvalid())) {
134+
io.opentelemetry.api.trace.Span otelSpan = OtelSpan.toOtel(span);
135+
if (otelSpan.equals(io.opentelemetry.api.trace.Span.getInvalid())) {
137136
if (log.isDebugEnabled()) {
138-
log.debug("Not doing anything cause the span is invalid");
137+
log.debug("Not doing anything because the span is invalid");
139138
}
140139
return;
141140
}
142-
io.opentelemetry.api.trace.Span otel = OtelSpan.toOtel(span);
143141
if (response.error() != null) {
144142
if (log.isDebugEnabled()) {
145-
log.debug("There was an error, will finish span [" + otel + "] exceptionally");
143+
log.debug("There was an error, will finish span [" + otelSpan + "] exceptionally");
146144
}
147-
endExceptionally(otel.storeInContext(Context.current()), response, response.error());
145+
endExceptionally(Context.current().with(otelSpan), response, response.error());
148146
}
149147
else {
150148
if (log.isDebugEnabled()) {
151-
log.debug("There was no error, will finish span [" + otel + "] in a standard way");
149+
log.debug("There was no error, will finish span [" + otelSpan + "] in a standard way");
152150
}
153-
end(otel.storeInContext(Context.current()), response);
151+
end(Context.current().with(otelSpan), response);
154152
}
155153
}
156154

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private io.opentelemetry.api.trace.Span delegate(Span span) {
7272
if (span == null) {
7373
// remove any existing span/baggage data from the current state of anything
7474
// that might be holding on to it.
75-
this.publisher.publishEvent(new OtelCurrentTraceContext.ScopeClosed(this));
75+
this.publisher.publishEvent(new EventPublishingContextWrapper.ScopeClosedEvent(this));
7676
return io.opentelemetry.api.trace.Span.getInvalid();
7777
}
7878
return ((OtelSpan) span).delegate;

0 commit comments

Comments
 (0)