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

Function basicOTelExample

examples/telemetry/main.go:43–100  ·  view source on GitHub ↗

basicOTelExample 演示基础 OpenTelemetry 集成

()

Source from the content-addressed store, hash-verified

41
42// basicOTelExample 演示基础 OpenTelemetry 集成
43func basicOTelExample() {
44 ctx := context.Background()
45
46 // 1. 创建 stdout exporter(用于演示)
47 exporter, err := stdouttrace.New(
48 stdouttrace.WithWriter(os.Stdout),
49 stdouttrace.WithPrettyPrint(),
50 )
51 if err != nil {
52 log.Fatalf("Failed to create exporter: %v", err)
53 }
54
55 // 2. 创建 OTel tracer
56 tracer, err := telemetry.NewOTelTracer(
57 "my-agent-service",
58 telemetry.WithOTelExporter(exporter),
59 telemetry.WithOTelServiceVersion("1.0.0"),
60 telemetry.WithOTelSampler(sdktrace.AlwaysSample()),
61 telemetry.WithOTelAttributes(
62 attribute.String("environment", "development"),
63 attribute.String("deployment", "local"),
64 ),
65 )
66 if err != nil {
67 log.Fatalf("Failed to create tracer: %v", err)
68 }
69 defer func() { _ = tracer.Shutdown(ctx) }()
70
71 // 3. 设置为全局 tracer
72 telemetry.SetGlobalTracer(tracer)
73
74 // 4. 创建 span
75 _, span := tracer.StartSpan(
76 ctx,
77 "basic-operation",
78 telemetry.WithSpanKind(telemetry.SpanKindInternal),
79 )
80 defer span.End()
81
82 // 5. 添加属性
83 span.SetAttributes(
84 telemetry.String("operation.type", "example"),
85 telemetry.Int("operation.count", 1),
86 )
87
88 // 6. 模拟一些工作
89 time.Sleep(100 * time.Millisecond)
90
91 // 7. 添加事件
92 span.AddEvent("operation.completed",
93 telemetry.String("result", "success"),
94 )
95
96 // 8. 设置状态
97 span.SetStatus(telemetry.StatusCodeOK, "Operation completed successfully")
98
99 fmt.Println("✅ Basic OTel integration completed")
100}

Callers 1

mainFunction · 0.85

Calls 15

ShutdownMethod · 0.95
StartSpanMethod · 0.95
NewOTelTracerFunction · 0.92
WithOTelExporterFunction · 0.92
WithOTelServiceVersionFunction · 0.92
WithOTelSamplerFunction · 0.92
WithOTelAttributesFunction · 0.92
SetGlobalTracerFunction · 0.92
WithSpanKindFunction · 0.92
StringFunction · 0.92
IntFunction · 0.92
EndMethod · 0.65

Tested by

no test coverage detected