-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice.go
171 lines (144 loc) · 4.7 KB
/
service.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/komuw/otero/log"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/trace"
)
// curl -vkL http://127.0.0.1:8081/serviceA
func serviceA(ctx context.Context, port int) {
serverPort := fmt.Sprintf(":%d", port)
address := fmt.Sprintf("127.0.0.1%s", serverPort)
var mux http.ServeMux
mux.HandleFunc("/serviceA", serviceA_HttpHandler)
handler := otelhttp.NewHandler(
&mux,
"server.http",
// If you did not set the global propagator as shown in `tracing.go`
// then you need to provide this one
// otelhttp.WithPropagators(propagator),
)
server := &http.Server{
Addr: serverPort,
Handler: handler,
}
log := log.NewLogrus(ctx)
log.Info("serviceA listening on: ", address)
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
// curl -vkL http://127.0.0.1:8082/serviceB
func serviceB(ctx context.Context, port int) {
serverPort := fmt.Sprintf(":%d", port)
address := fmt.Sprintf("127.0.0.1%s", serverPort)
var mux http.ServeMux
mux.HandleFunc("/serviceB", serviceB_HttpHandler)
handler := otelhttp.NewHandler(
&mux,
"server.http",
// If you did not set the global propagator as shown in `tracing.go`
// then you need to provide this one
// otelhttp.WithPropagators(propagator),
)
server := &http.Server{
Addr: serverPort,
Handler: handler,
}
log := log.NewLogrus(ctx)
log.Info("serviceB listening on: ", address)
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
func serviceA_HttpHandler(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer(tracerName).Start(r.Context(), "serviceA_HttpHandler")
defer span.End()
counter, _ := getMeter().SyncInt64().Counter(
"service_a_called_counter",
instrument.WithDescription("how many time the serviceA handler has been called."),
)
counter.Add(
ctx,
1,
// labels/tags
[]attribute.KeyValue{
attribute.String("handler_name", "serviceA_HttpHandler"),
attribute.Int64("req_size", r.ContentLength),
}...,
)
log := log.NewLogrus(ctx)
log.Info("serviceA_HttpHandler called")
// When serviceA is called, it calls serviceB over tcp network.
// We should still be able to propagate traces over a tcp network.
cli := &http.Client{
Transport: otelhttp.NewTransport(
http.DefaultTransport,
// If you did not set the global propagator as shown in `tracing.go`
// then you need to provide this one
// otelhttp.WithPropagators(propagator),
),
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://otero_service_b:8082/serviceB", nil)
if err != nil {
panic(err)
}
resp, err := cli.Do(req)
if err != nil {
panic(err)
}
log.Info("serviceA called serviceB and got resp.StatusCode: ", resp.StatusCode)
fmt.Fprintf(w, "hello from serviceA")
// response header contains, `Ot-Tracer-Spanid` & `Ot-Tracer-Traceid` headers that are added by the otel propagator.
// upstream services can then consume those.
log.Info("request.Header serviceA: ", r.Header)
log.Info("response.Header serviceA: ", w.Header())
}
func serviceB_HttpHandler(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer(tracerName).Start(r.Context(), "serviceB_HttpHandler")
defer span.End()
counter, _ := getMeter().SyncInt64().Counter(
"serviceB_call_counter",
instrument.WithDescription("how many time the serviceB handler has been called."),
)
counter.Add(ctx, 1)
log := log.NewLogrus(ctx)
log.Info("serviceB_HttpHandler called")
answer := add(ctx, 42, 1813)
fmt.Fprintf(w, "hello from serviceB: Answer is: %d", answer)
// response header contains, `Ot-Tracer-Spanid` & `Ot-Tracer-Traceid` headers that are added by the otel propagator.
// upstream services can then consume those.
log.Info("request.Header serviceB: ", r.Header)
log.Info("response.Header serviceB: ", w.Header())
}
func add(ctx context.Context, x, y int64) int64 {
// otel.Tracer("instrumentation/package/name", trace.WithStackTrace(true)) // can also take other opts
ctx, span := otel.Tracer(tracerName).Start(
ctx,
"add",
// add labels/tags(if any) that are specific to this scope.
trace.WithAttributes(attribute.String("method", "GET")),
trace.WithAttributes(attribute.String("endpoint", "/foo/user")),
)
defer span.End()
err := errors.New("oops, 99 problems")
span.RecordError(err, trace.WithStackTrace(true))
{ // Use different loggers.
lr := log.NewLogrus(ctx)
lr.Println("logrus: add called.")
lz := log.NewZerolog(ctx)
lz.Info().Msg("zerolog: add called.")
ls := log.NewSlog(ctx)
ls.Info("hello from slog logger.")
ls.Debug("some msg", "age", 56)
}
return x + y
}