public class CozeLoopSpan extends Object implements AutoCloseable
This class wraps OpenTelemetry's Span and provides:
The span is automatically made current in the OpenTelemetry context when created, allowing child spans to automatically inherit the parent context.
Example usage:
try (CozeLoopSpan span = client.startSpan("operation", "custom")) {
span.setInput("input data");
span.setOutput("output data");
span.addEvent("important-event");
}
For advanced OpenTelemetry features, you can access the underlying Span:
CozeLoopSpan cozeSpan = client.startSpan("operation", "custom");
Span otelSpan = cozeSpan.getSpan();
// Use OpenTelemetry APIs directly
| Constructor and Description |
|---|
CozeLoopSpan(io.opentelemetry.api.trace.Span span,
io.opentelemetry.context.Scope scope,
io.opentelemetry.context.propagation.ContextPropagators propagators,
CozeLoopContext context,
String scene,
FinishEventProcessor finishEventProcessor)
Create a new CozeLoopSpan wrapper.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
End the span and close the scope.
|
void |
finish()
End the span and close the scope.
|
Map<String,String> |
getBaggage()
Get all baggage items.
|
CozeLoopContext |
getContext()
Get the CozeLoop Context associated with this span.
|
String |
getSpanID()
Get the span ID for this span.
|
String |
getTraceID()
Get the trace ID for this span.
|
void |
setBaggage(Map<String,String> values)
Set multiple baggage items.
|
void |
setBaggage(String key,
String value)
Set baggage for this span and future child spans.
|
void |
setDeploymentEnv(String env)
Set deployment environment (e.g., "prod", "staging").
|
void |
setError(Throwable error)
Set the error for this span.
|
void |
setInput(Object input)
Set the input for this span.
|
void |
setInputTokens(long tokens)
Set input tokens.
|
void |
setLogID(String logID)
Set log ID for this span.
|
void |
setMessageID(String messageId)
Set message_id for this span.
|
void |
setMessageIDBaggage(String messageId)
Set message_id baggage for this span and future child spans.
|
void |
setModelFrequencyPenalty(Double frequencyPenalty)
Set model frequency_penalty.
|
void |
setModelMaxTokens(Integer maxTokens)
Set model max_tokens.
|
void |
setModelName(String model)
Set model name.
|
void |
setModelPresencePenalty(Double presencePenalty)
Set model presence_penalty.
|
void |
setModelProvider(String provider)
Set model provider (e.g., "openai", "anthropic").
|
void |
setModelStopSequences(List<String> stopSequences)
Set model stop_sequences.
|
void |
setModelTemperature(Double temperature)
Set model temperature.
|
void |
setModelTopK(Integer topK)
Set model top_k.
|
void |
setModelTopP(Double topP)
Set model top_p.
|
void |
setOutput(Object output)
Set the output for this span.
|
void |
setOutputTokens(long tokens)
Set output tokens.
|
void |
setRuntime(Runtime runt)
Set runtime data.
|
void |
setServiceName(String serviceName)
Set service name for this span.
|
void |
setStartTimeFirstResp(long timestampUs)
Set start time (Not duration!)
|
void |
setStatusCode(int statusCode)
Set status code (0=OK, other=ERROR).
|
void |
setTag(String key,
boolean value)
Set custom attribute (boolean).
|
void |
setTag(String key,
double value)
Set custom attribute (double).
|
void |
setTag(String key,
long value)
Set custom attribute (long).
|
void |
setTag(String key,
String value)
Set custom attribute (string).
|
void |
setTags(Map<String,String> tags)
Set multiple tags/attributes at once.
|
void |
setThreadID(String threadId)
Set thread_id for this span.
|
void |
setThreadIDBaggage(String threadId)
Set thread_id baggage for this span and future child spans.
|
void |
setUserID(String userId)
Set user_id for this span.
|
void |
setUserIDBaggage(String userId)
Set user_id baggage for this span and future child spans.
|
Map<String,String> |
toHeader()
Get headers for cross-service propagation.
|
public CozeLoopSpan(io.opentelemetry.api.trace.Span span,
io.opentelemetry.context.Scope scope,
io.opentelemetry.context.propagation.ContextPropagators propagators,
CozeLoopContext context,
String scene,
FinishEventProcessor finishEventProcessor)
The span is automatically made current in the OpenTelemetry context, which enables automatic context propagation to child spans.
span - the underlying OpenTelemetry Spanscope - the scope that makes this span current in the contextpropagators - the context propagators for header injectioncontext - the context this span was created inscene - the scene identifier for this spanfinishEventProcessor - the processor to handle span finish eventspublic String getTraceID()
public String getSpanID()
public Map<String,String> getBaggage()
public void setInput(Object input)
input - the input objectpublic void setOutput(Object output)
output - the output objectpublic void setError(Throwable error)
error - the error/exceptionpublic void setStatusCode(int statusCode)
statusCode - the status codepublic void setUserID(String userId)
userId - the user IDpublic void setUserIDBaggage(String userId)
userId - the user IDpublic void setMessageID(String messageId)
messageId - the message IDpublic void setMessageIDBaggage(String messageId)
messageId - the message IDpublic void setThreadID(String threadId)
threadId - the thread IDpublic void setThreadIDBaggage(String threadId)
threadId - the thread IDpublic void setModelProvider(String provider)
provider - the model providerpublic void setModelName(String model)
model - the model namepublic void setModelTemperature(Double temperature)
temperature - the temperaturepublic void setModelTopP(Double topP)
topP - the top_ppublic void setModelTopK(Integer topK)
topK - the top_kpublic void setModelMaxTokens(Integer maxTokens)
maxTokens - the max_tokenspublic void setModelFrequencyPenalty(Double frequencyPenalty)
frequencyPenalty - the frequency_penaltypublic void setModelPresencePenalty(Double presencePenalty)
presencePenalty - the presence_penaltypublic void setModelStopSequences(List<String> stopSequences)
stopSequences - the stop_sequencespublic void setInputTokens(long tokens)
tokens - the number of input tokenspublic void setOutputTokens(long tokens)
tokens - the number of output tokenspublic void setStartTimeFirstResp(long timestampUs)
timestampUs - the timestamp in microsecondspublic void setRuntime(Runtime runt)
runt - Runtimepublic void setServiceName(String serviceName)
serviceName - the service namepublic void setLogID(String logID)
logID - the log IDpublic void setDeploymentEnv(String env)
env - the environmentpublic void setTags(Map<String,String> tags)
tags - map of tagspublic void setBaggage(String key, String value)
key - the baggage keyvalue - the baggage valuepublic void setBaggage(Map<String,String> values)
values - map of baggage keys and valuespublic Map<String,String> toHeader()
public void setTag(String key, String value)
key - the attribute keyvalue - the attribute valuepublic void setTag(String key, long value)
key - the attribute keyvalue - the attribute valuepublic void setTag(String key, double value)
key - the attribute keyvalue - the attribute valuepublic void setTag(String key, boolean value)
key - the attribute keyvalue - the attribute valuepublic CozeLoopContext getContext()
This context includes the current span, baggage, and other propagation data. It can be used to create child spans or for manual context propagation.
public void finish()
This method is automatically called when using try-with-resources. It ends the span (marking it as finished) and closes the scope (removing it from the current context). The span will then be processed by the BatchSpanProcessor and eventually exported to CozeLoop platform.
public void close()
This method is automatically called when using try-with-resources. It ends the span (marking it as finished) and closes the scope (removing it from the current context). The span will then be processed by the BatchSpanProcessor and eventually exported to CozeLoop platform.
close in interface AutoCloseableCopyright © 2026. All rights reserved.