Skip to content

Commit 6d2c7fb

Browse files
committed
**Trace**: Block internal opentelemetry implementation
1 parent a2b2824 commit 6d2c7fb

File tree

16 files changed

+122
-278
lines changed

16 files changed

+122
-278
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9-
## [0.1.1] - 2025-11-11
9+
## [0.1.2] - 2026-02-04
10+
### Changed
11+
- **Trace**: Block internal opentelemetry implementation
12+
13+
## [0.1.1] - 2026-02-04
1014
### Changed
1115
- **Cozeloop Client**: Support get workspace id and token from env
1216

cozeloop-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.coze</groupId>
1010
<artifactId>cozeloop-java</artifactId>
11-
<version>0.1.1</version>
11+
<version>0.1.2</version>
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414

cozeloop-core/src/main/java/com/coze/loop/client/CozeLoopClient.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import com.coze.loop.entity.Prompt;
1010
import com.coze.loop.prompt.GetPromptParam;
1111
import com.coze.loop.stream.StreamReader;
12+
import com.coze.loop.trace.CozeLoopContext;
1213
import com.coze.loop.trace.CozeLoopSpan;
1314

14-
import io.opentelemetry.api.trace.Tracer;
15-
import io.opentelemetry.context.Context;
16-
1715
/** Main interface for CozeLoop SDK client. Provides trace and prompt functionality. */
1816
public interface CozeLoopClient extends AutoCloseable {
1917

@@ -58,24 +56,17 @@ public interface CozeLoopClient extends AutoCloseable {
5856
* @param parentContext the parent context
5957
* @return the span wrapper
6058
*/
61-
CozeLoopSpan startSpan(String name, String spanType, Context parentContext);
59+
CozeLoopSpan startSpan(String name, String spanType, CozeLoopContext parentContext);
6260

63-
CozeLoopSpan startSpan(String name, String spanType, Context parentContext, String scene);
61+
CozeLoopSpan startSpan(String name, String spanType, CozeLoopContext parentContext, String scene);
6462

6563
/**
6664
* Extract context from headers for cross-service propagation.
6765
*
6866
* @param headers the map of headers (e.g. from an incoming HTTP request)
6967
* @return the extracted context
7068
*/
71-
Context extractContext(Map<String, String> headers);
72-
73-
/**
74-
* Get the underlying OpenTelemetry Tracer.
75-
*
76-
* @return tracer instance
77-
*/
78-
Tracer getTracer();
69+
CozeLoopContext extractContext(Map<String, String> headers);
7970

8071
// ========== Prompt Operations ==========
8172

cozeloop-core/src/main/java/com/coze/loop/client/CozeLoopClientImpl.java

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.coze.loop.prompt.PromptProvider;
2020
import com.coze.loop.spec.tracespec.SpanKeys;
2121
import com.coze.loop.stream.StreamReader;
22+
import com.coze.loop.trace.CozeLoopContext;
2223
import com.coze.loop.trace.CozeLoopSpan;
2324
import com.coze.loop.trace.CozeLoopTracerProvider;
2425

@@ -84,10 +85,7 @@ public CozeLoopSpan startSpan(String name, String spanType, String scene) {
8485
public CozeLoopSpan startSpan(String name, String spanType, CozeLoopSpan parentSpan) {
8586
checkNotClosed();
8687

87-
Context parentContext =
88-
parentSpan != null && parentSpan.getSpan() != null
89-
? Context.current().with(parentSpan.getSpan())
90-
: null;
88+
CozeLoopContext parentContext = parentSpan != null ? parentSpan.getContext() : null;
9189

9290
return startSpan(name, spanType, parentContext);
9391
}
@@ -97,23 +95,21 @@ public CozeLoopSpan startSpan(
9795
String name, String spanType, CozeLoopSpan parentSpan, String scene) {
9896
checkNotClosed();
9997

100-
Context parentContext =
101-
parentSpan != null && parentSpan.getSpan() != null
102-
? Context.current().with(parentSpan.getSpan())
103-
: null;
98+
CozeLoopContext parentContext = parentSpan != null ? parentSpan.getContext() : null;
10499

105100
return startSpan(name, spanType, parentContext, scene);
106101
}
107102

108103
@Override
109-
public CozeLoopSpan startSpan(String name, String spanType, Context parentContext) {
104+
public CozeLoopSpan startSpan(String name, String spanType, CozeLoopContext parentContext) {
110105
checkNotClosed();
111106

112107
return startSpan(name, spanType, parentContext, "");
113108
}
114109

115110
@Override
116-
public CozeLoopSpan startSpan(String name, String spanType, Context parentContext, String scene) {
111+
public CozeLoopSpan startSpan(
112+
String name, String spanType, CozeLoopContext parentContext, String scene) {
117113
checkNotClosed();
118114

119115
SpanBuilder spanBuilder = tracer.spanBuilder(name);
@@ -140,39 +136,35 @@ public CozeLoopSpan startSpan(String name, String spanType, Context parentContex
140136
Context finalContext = contextToUse.with(span);
141137
Scope scope = finalContext.makeCurrent();
142138

143-
return new CozeLoopSpan(span, scope, tracerProvider.getPropagators(), finalContext, scene);
139+
return new CozeLoopSpan(
140+
span, scope, tracerProvider.getPropagators(), new CozeLoopContext(finalContext), scene);
144141
}
145142

146143
@Override
147-
public Context extractContext(Map<String, String> headers) {
144+
public CozeLoopContext extractContext(Map<String, String> headers) {
148145
checkNotClosed();
149146
if (headers == null || headers.isEmpty()) {
150-
return Context.current();
147+
return CozeLoopContext.current();
151148
}
152149

153-
return tracerProvider
154-
.getPropagators()
155-
.getTextMapPropagator()
156-
.extract(
157-
Context.current(),
158-
headers,
159-
new TextMapGetter<Map<String, String>>() {
160-
@Override
161-
public Iterable<String> keys(Map<String, String> carrier) {
162-
return carrier.keySet();
163-
}
164-
165-
@Override
166-
public String get(Map<String, String> carrier, String key) {
167-
return carrier.get(key);
168-
}
169-
});
170-
}
171-
172-
@Override
173-
public Tracer getTracer() {
174-
checkNotClosed();
175-
return tracer;
150+
return new CozeLoopContext(
151+
tracerProvider
152+
.getPropagators()
153+
.getTextMapPropagator()
154+
.extract(
155+
Context.current(),
156+
headers,
157+
new TextMapGetter<Map<String, String>>() {
158+
@Override
159+
public Iterable<String> keys(Map<String, String> carrier) {
160+
return carrier.keySet();
161+
}
162+
163+
@Override
164+
public String get(Map<String, String> carrier, String key) {
165+
return carrier.get(key);
166+
}
167+
}));
176168
}
177169

178170
// ========== Prompt Operations ==========

cozeloop-core/src/main/java/com/coze/loop/entity/Message.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import com.fasterxml.jackson.annotation.JsonInclude;
67
import com.fasterxml.jackson.annotation.JsonProperty;
78

89
/** Message entity. */
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
911
public class Message {
1012
@JsonProperty("role")
1113
private Role role;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.coze.loop.internal;
22

33
public class Constants {
4-
public static final String Version = "0.1.0";
4+
public static final String Version = "0.1.2";
55
public static final String TraceContextHeaderParent = "X-Cozeloop-Traceparent";
66
public static final String TraceContextHeaderBaggage = "X-Cozeloop-Tracestate";
77
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.coze.loop.trace;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import io.opentelemetry.context.Context;
6+
import io.opentelemetry.context.ContextKey;
7+
8+
/**
9+
* Wrapper for OpenTelemetry Context to shield internal implementation.
10+
*
11+
* <p>This class provides a way to work with trace context without directly exposing OpenTelemetry
12+
* APIs.
13+
*/
14+
public class CozeLoopContext implements Context {
15+
private final Context context;
16+
17+
/**
18+
* Create a new CozeLoopContext wrapping an OpenTelemetry Context.
19+
*
20+
* @param context the underlying OpenTelemetry context
21+
*/
22+
public CozeLoopContext(Context context) {
23+
this.context = context;
24+
}
25+
26+
/**
27+
* Get the current context from thread-local storage.
28+
*
29+
* @return the current context
30+
*/
31+
public static CozeLoopContext current() {
32+
return new CozeLoopContext(Context.current());
33+
}
34+
35+
@Nullable
36+
@Override
37+
public <V> V get(ContextKey<V> contextKey) {
38+
return context.get(contextKey);
39+
}
40+
41+
@Override
42+
public <V> Context with(ContextKey<V> contextKey, V v) {
43+
return context.with(contextKey, v);
44+
}
45+
}

0 commit comments

Comments
 (0)