-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (93 loc) · 2.44 KB
/
main.go
File metadata and controls
108 lines (93 loc) · 2.44 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"log"
"time"
"github.com/rabbitmq/amqp091-go"
orb "github.com/startower-observability/orb"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/trace"
)
func main() {
// Initialize OpenTelemetry
initTracer()
// Connect to RabbitMQ with instrumentation
conn, err := orb.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ: %v", err)
}
defer conn.Close()
// Create instrumented channel
ch, err := conn.ChannelWithTracing()
if err != nil {
log.Fatalf("Failed to create channel: %v", err)
}
defer ch.Close()
// Declare a queue
queue, err := ch.QueueDeclare(
"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Fatalf("Failed to declare queue: %v", err)
}
ctx := context.Background()
// Publish a message with tracing
log.Println("Publishing message...")
err = ch.PublishWithTracing(ctx,
"", // exchange
queue.Name, // routing key
false, // mandatory
false, // immediate
amqp091.Publishing{
ContentType: "text/plain",
Body: []byte("Hello, World!"),
MessageId: "msg-123",
})
if err != nil {
log.Fatalf("Failed to publish message: %v", err)
}
// Start consuming messages with tracing
log.Println("Starting consumer...")
handler := func(ctx context.Context, delivery amqp091.Delivery) error {
log.Printf("Received message: %s", delivery.Body)
// Simulate processing time
time.Sleep(100 * time.Millisecond)
return nil
}
err = ch.ConsumeWithTracing(ctx,
queue.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
handler,
)
if err != nil {
log.Fatalf("Failed to start consuming: %v", err)
}
// Wait for a bit to see the message processing
time.Sleep(2 * time.Second)
log.Println("Example completed!")
}
func initTracer() {
// Create stdout exporter for demonstration
exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
log.Fatalf("Failed to create stdout exporter: %v", err)
}
// Create tracer provider
tp := trace.NewTracerProvider(
trace.WithBatcher(exporter),
)
// Set global tracer provider
otel.SetTracerProvider(tp)
log.Println("OpenTelemetry tracer initialized")
}