-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (87 loc) · 2.53 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
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/uptrace/opentelemetry-go-extra/otelgorm"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
oteltrace "go.opentelemetry.io/otel/trace"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var tracer = otel.Tracer("mux-server")
var db *gorm.DB
type User struct {
ID int64
Name string
Age int
}
func main() {
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
initDB()
r := mux.NewRouter()
r.Use(otelmux.Middleware("my-server"))
r.HandleFunc("/users/{id:[0-9]+}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
name := getUser(r.Context(), id)
reply := fmt.Sprintf("user %s (id %s)\n", name, id)
_, _ = w.Write(([]byte)(reply))
}))
http.Handle("/", r)
_ = http.ListenAndServe(":8080", nil)
}
func initTracer() *sdktrace.TracerProvider {
exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
otlptracegrpc.WithEndpoint("otlp.nr-data.net:4317"),
otlptracegrpc.WithHeaders(map[string]string{"api-key": "233dee83a558dd07fa7223cd167fa3b9aef4NRAL"}),
))
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}
func initDB() {
dsn := "root:toor@tcp(127.0.0.1:3306)/foo?charset=utf8mb4&parseTime=True&loc=Local"
var err error
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
if err := db.Use(otelgorm.NewPlugin()); err != nil {
panic(err)
}
}
func getUser(ctx context.Context, id string) string {
_, span := tracer.Start(ctx, "getUser", oteltrace.WithAttributes(attribute.String("id", id)))
defer span.End()
if id == "123" {
return "otelmux tester"
}
user := User{}
if err := db.WithContext(ctx).Table("tb_users").Where("id = ?", id).First(&user).Error; err != nil {
return err.Error()
}
return fmt.Sprintf("%#v", user)
}