MCPcopy Create free account
hub / github.com/astercloud/aster / distributedTracingExample

Function distributedTracingExample

examples/telemetry/main.go:223–264  ·  view source on GitHub ↗

distributedTracingExample 演示分布式追踪

()

Source from the content-addressed store, hash-verified

221
222// distributedTracingExample 演示分布式追踪
223func distributedTracingExample() {
224 ctx := context.Background()
225 tracer := telemetry.GetGlobalTracer()
226
227 // Service A: 创建一个 span
228 ctx, spanA := tracer.StartSpan(
229 ctx,
230 "service-a.process",
231 telemetry.WithSpanKind(telemetry.SpanKindServer),
232 )
233
234 // 模拟将 trace context 传播到 Service B
235 carrier := make(MapCarrier)
236 if err := tracer.Inject(ctx, carrier); err != nil {
237 log.Printf("Failed to inject context: %v", err)
238 }
239
240 spanA.AddEvent("context.injected",
241 telemetry.String("carrier.type", "http-headers"),
242 )
243
244 // Service B: 提取 trace context
245 ctxB := context.Background()
246 ctxB = tracer.Extract(ctxB, carrier)
247
248 _, spanB := tracer.StartSpan(
249 ctxB,
250 "service-b.handle",
251 telemetry.WithSpanKind(telemetry.SpanKindServer),
252 )
253
254 spanB.AddEvent("context.extracted")
255 time.Sleep(50 * time.Millisecond)
256
257 spanB.SetStatus(telemetry.StatusCodeOK, "Service B completed")
258 spanB.End()
259
260 spanA.SetStatus(telemetry.StatusCodeOK, "Service A completed")
261 spanA.End()
262
263 fmt.Println("✅ Distributed tracing completed")
264}
265
266// 辅助函数
267

Callers 1

mainFunction · 0.85

Calls 10

GetGlobalTracerFunction · 0.92
WithSpanKindFunction · 0.92
StringFunction · 0.92
PrintfMethod · 0.80
StartSpanMethod · 0.65
InjectMethod · 0.65
AddEventMethod · 0.65
ExtractMethod · 0.65
SetStatusMethod · 0.65
EndMethod · 0.65

Tested by

no test coverage detected