-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
143 lines (122 loc) · 3.93 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/credentials"
)
var (
nameServiceUrl = os.Getenv("NAME_ENDPOINT") + "/name"
messageServiceUrl = os.Getenv("MESSAGE_ENDPOINT") + "/message"
tracer trace.Tracer
)
func getGrpcEndpoint() string {
apiEndpoint, exists := os.LookupEnv("HONEYCOMB_API_ENDPOINT")
if !exists {
apiEndpoint = "api.honeycomb.io:443"
} else {
u, err := url.Parse(apiEndpoint)
if err != nil {
panic(fmt.Errorf("error %s parsing url: %s", err, apiEndpoint))
}
var host, port string
if u.Port() != "" {
host, port, _ = net.SplitHostPort(u.Host)
} else {
host = u.Host
port = "443"
}
apiEndpoint = fmt.Sprintf("%s:%s", host, port)
}
return apiEndpoint
}
func newExporter(ctx context.Context) (*otlptrace.Exporter, error) {
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(getGrpcEndpoint()),
otlptracegrpc.WithHeaders(map[string]string{
"x-honeycomb-team": os.Getenv("HONEYCOMB_API_KEY"),
"x-honeycomb-dataset": os.Getenv("HONEYCOMB_DATASET"),
}),
otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")),
}
client := otlptracegrpc.NewClient(opts...)
return otlptrace.New(ctx, client)
}
func newTraceProvider(exp *otlptrace.Exporter) *sdktrace.TracerProvider {
r, _ := resource.Merge(
resource.Default(),
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("frontend-go"),
))
return sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(r),
)
}
func main() {
ctx := context.Background()
exp, err := newExporter(ctx)
if err != nil {
log.Fatalf("failed to initialize exporter: %v", err)
}
tp := newTraceProvider(exp)
// Handle this error in a sensible manner where possible
defer func() { _ = tp.Shutdown(ctx) }()
// Set the Tracer Provider and the W3C Trace Context propagator as globals.
// Important, otherwise this won't let you see distributed traces be connected!
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}),
)
tracer = tp.Tracer("greeting-service/year-service")
mux := http.NewServeMux()
mux.HandleFunc("/greeting", func(w http.ResponseWriter, r *http.Request) {
name := getName(r.Context())
message := getMessage(r.Context())
_, _ = fmt.Fprintf(w, "Hello %s, %s", name, message)
})
wrappedHandler := otelhttp.NewHandler(mux, "frontend")
log.Println("Listening on http://localhost:7777/greeting")
log.Fatal(http.ListenAndServe(":7777", wrappedHandler))
}
func getName(ctx context.Context) string {
var getNameSpan trace.Span
ctx, getNameSpan = tracer.Start(ctx, "✨ call /name ✨")
defer getNameSpan.End()
return makeRequest(ctx, nameServiceUrl)
}
func getMessage(ctx context.Context) string {
var getMessageSpan trace.Span
ctx, getMessageSpan = tracer.Start(ctx, "✨ call /message ✨")
defer getMessageSpan.End()
return makeRequest(ctx, messageServiceUrl)
}
func makeRequest(ctx context.Context, url string) string {
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
client := http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
res, err := client.Do(req)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
panic(err)
}
return string(body)
}