Skip to content

Commit ddd2096

Browse files
authored
fix(tracing): adapt to tracing CozeLoop (#127)
* fix(tracing): fix distributed tracing requirments attributes * fix(tracing): fix distributed tracing requirments attributes remove unknown * fix(tracing): remove print * fix(tracing): set space_id, fix call_type --------- Co-authored-by: tangou <[email protected]>
1 parent 8eb63bd commit ddd2096

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/app.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
from veadk.tracing.telemetry.opentelemetry_tracer import OpentelemetryTracer
3737
from veadk.types import AgentRunConfig
3838
from veadk.utils.logger import get_logger
39+
from volcengine.base.Request import Request
40+
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
41+
from opentelemetry import context
3942

4043
logger = get_logger(__name__)
4144

@@ -117,6 +120,8 @@ async def agent_card() -> dict:
117120
agent_card = await agent_card_builder.build()
118121
return agent_card.model_dump()
119122

123+
async def get_cozeloop_space_id() -> dict:
124+
return {"space_id": os.getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME", default="")}
120125

121126
load_tracer()
122127

@@ -132,7 +137,7 @@ async def agent_card() -> dict:
132137

133138
a2a_app.post("/run_agent", operation_id="run_agent", tags=["mcp"])(run_agent_func)
134139
a2a_app.get("/agent_card", operation_id="agent_card", tags=["mcp"])(agent_card)
135-
140+
a2a_app.get("/get_cozeloop_space_id", operation_id="get_cozeloop_space_id", tags=["mcp"])(get_cozeloop_space_id)
136141

137142
# === Build mcp server ===
138143

@@ -159,6 +164,25 @@ async def combined_lifespan(app: FastAPI):
159164
redoc_url=None
160165
)
161166

167+
@app.middleware("http")
168+
async def otel_context_middleware(request: Request, call_next):
169+
carrier = {
170+
"traceparent": request.headers.get("Traceparent"),
171+
"tracestate": request.headers.get("Tracestate"),
172+
}
173+
logger.debug(f"carrier: {carrier}")
174+
if carrier["traceparent"] is None:
175+
return await call_next(request)
176+
else:
177+
ctx = TraceContextTextMapPropagator().extract(carrier=carrier)
178+
logger.debug(f"ctx: {ctx}")
179+
token = context.attach(ctx)
180+
try:
181+
response = await call_next(request)
182+
finally:
183+
context.detach(token)
184+
return response
185+
162186
# Mount A2A routes to main app
163187
for route in a2a_app.routes:
164188
app.routes.append(route)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
veadk-python
1+
veadk-python
2+
fastapi
3+
uvicorn[standard]

veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ while [[ $# -gt 0 ]]; do
3333
esac
3434
done
3535

36-
# in case of deployment deps not installed in user's requirements.txt
37-
if pip list | grep -q "^fastapi \|^uvicorn "; then
38-
echo "fastapi and uvicorn already installed"
39-
else
40-
python3 -m pip install uvicorn[standard] fastapi
41-
fi
42-
4336
# Check if MODEL_AGENT_API_KEY is set
4437
if [ -z "$MODEL_AGENT_API_KEY" ]; then
4538
echo "MODEL_AGENT_API_KEY is not set. Please set it in your environment variables."

veadk/integrations/ve_faas/ve_faas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def _create_application(
152152
"GatewayName": gateway_name,
153153
"ServiceName": service_name,
154154
"UpstreamName": upstream_name,
155+
"EnableKeyAuth": True,
156+
"EnableMcpSession": True,
155157
},
156158
"TemplateId": self.template_id,
157159
},

veadk/tracing/telemetry/attributes/extractors/common_attributes_extractors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def common_cozeloop_report_source(**kwargs) -> str:
4949
return "veadk"
5050

5151

52+
def common_cozeloop_call_type(**kwargs) -> str:
53+
return kwargs.get("call_type")
54+
55+
5256
def llm_openinference_instrumentation_veadk(**kwargs) -> str:
5357
return VERSION
5458

@@ -68,4 +72,5 @@ def llm_openinference_instrumentation_veadk(**kwargs) -> str:
6872
"user.id": common_gen_ai_user_id, # CozeLoop / TLS required
6973
"session.id": common_gen_ai_session_id, # CozeLoop / TLS required
7074
"cozeloop.report.source": common_cozeloop_report_source, # CozeLoop required
75+
"cozeloop.call_type": common_cozeloop_call_type, # CozeLoop required
7176
}

veadk/tracing/telemetry/telemetry.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ def trace_call_llm(
227227
model_name=invocation_context.agent.model_name
228228
if isinstance(invocation_context.agent, Agent)
229229
else "",
230+
call_type=span.context.trace_state.get("call_type")
231+
if hasattr(span, "context")
232+
and hasattr(span.context, "trace_state")
233+
and hasattr(span.context.trace_state, "get")
234+
else "",
230235
)
231236

232237
llm_attributes_mapping = ATTRIBUTES.get("llm", {})

0 commit comments

Comments
 (0)