-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (51 loc) · 1.71 KB
/
main.go
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/sdk/resource"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)
func main() {
ctx := context.Background()
// Set up the OpenTelemetry Metric exporter
exporter, err := otlpmetrichttp.New(ctx)
if err != nil {
log.Fatalf("failed to create OTLP metric exporter: %v", err)
}
// Configure the meter provider
meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(
sdkmetric.NewPeriodicReader(exporter, sdkmetric.WithInterval(2*time.Second)),
),
sdkmetric.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("otel-example-app"),
)),
)
otel.SetMeterProvider(meterProvider)
defer func() {
if err := meterProvider.Shutdown(ctx); err != nil {
log.Fatalf("failed to shutdown MeterProvider: %v", err)
}
}()
meter := otel.Meter("otel-example-meter")
requestCount, err := meter.Int64Counter("request_count")
if err != nil {
log.Fatalf("failed to create request_count counter: %v", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Increment the request count
requestCount.Add(ctx, 1)
fmt.Fprintf(w, "Hello, OpenTelemetry Metrics!")
log.Println("Request handled")
})
log.Println("Server is running on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}