-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
43 lines (36 loc) · 1.22 KB
/
main.go
File metadata and controls
43 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"context"
"fmt"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/adapters/otel"
otelapi "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func main() {
// Setup OpenTelemetry
res := resource.NewWithAttributes(
"",
attribute.String("service.name", "mtlog-otel-example"),
)
tp := sdktrace.NewTracerProvider(sdktrace.WithResource(res))
otelapi.SetTracerProvider(tp)
defer tp.Shutdown(context.Background())
// Create tracer and start a span
tracer := otelapi.Tracer("example")
ctx, span := tracer.Start(context.Background(), "main-operation")
defer span.End()
// Create logger with OTEL trace enrichment
logger := mtlog.New(
otel.WithOTELEnricher(ctx), // Adds trace.id, span.id
mtlog.WithConsoleProperties(), // Shows properties in output
)
// Log messages - trace.id and span.id are automatically included
logger.Information("Starting application")
logger.Debug("Processing request")
logger.Warning("Low memory")
fmt.Println("\nTrace ID:", span.SpanContext().TraceID())
fmt.Println("Span ID:", span.SpanContext().SpanID())
}