-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
102 lines (90 loc) · 3.64 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 (
"fmt"
"github.com/go-redis/redis/extra/redisotel"
"github.com/go-redis/redis/v8"
"github.com/gorilla/mux"
"github.com/namsral/flag"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/trace/jaeger"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"log"
"net/http"
"os"
)
var (
jaegerEndpoint string
redisEndpoint string
address string
)
type server struct {
client *redis.Client
router *mux.Router
}
func initTracer(serviceName string, jaegerAgentEndpoint string) {
// Spans are created by tracers, which can be acquired from a Tracer Provider.
// In our example we will use Jaeger as our trace provider
// To do so, we will follow Jaeger godoc and leverage the NewExportPipeline() method
// that sets up a complete export pipeline with the recommended setup for trace provider
tp, _, err := jaeger.NewExportPipeline(
jaeger.WithCollectorEndpoint(fmt.Sprintf("%s/api/traces", jaegerAgentEndpoint)),
jaeger.WithProcess(jaeger.Process{
ServiceName: serviceName,
}),
jaeger.WithSDK(&sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
)
if err != nil {
log.Fatal(err)
}
// Tracers can be accessed with a TracerProvider.
// In implementations of the API, the TracerProvider is expected to be the stateful
// object that holds any configuration.
// Normally, the TracerProvider is expected to be accessed from a central place.
// Thus, the API SHOULD provide a way to set/register and access a global default TracerProvider.
//
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#tracerprovider
otel.SetTracerProvider(tp)
// Traces can extend beyond a single process. This requires context propagation, a mechanism where identifiers for a trace are sent to remote processes.
// TextMapPropagator performs the injection and extraction of a cross-cutting concern value as string key/values
// pairs into carriers that travel in-band across process boundaries.
// The carrier of propagated data on both the client (injector) and server (extractor) side is usually an HTTP request.
// In order to increase compatibility, the key/value pairs MUST only consist of US-ASCII characters that make up
// valid HTTP header fields as per RFC 7230.
//
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md#textmap-propagator
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
}
func main() {
flag.String(flag.DefaultConfigFlagname, "", "path to config file")
flag.StringVar(&jaegerEndpoint, "jaegerAgentEndpoint", "http://localhost:14268", "Jaeger agent endpoint")
flag.StringVar(&redisEndpoint, "redisEndpoint", "localhost:6379", "Redis endpoint")
flag.StringVar(&address, "address", ":7777", "Serving address")
flag.Parse()
logger := log.New(os.Stdout, "sampleHTTPServer: ", log.LstdFlags)
logger.Println("Starting tracer...")
initTracer("sampleHTTPServer", jaegerEndpoint)
logger.Println("Connecting to redis...")
redisClient := initRedis(redisEndpoint)
logger.Println("Starting server...")
sampleServer := initService(redisClient)
err := http.ListenAndServe(address, sampleServer.router)
if err != nil {
panic(err)
}
}
func initRedis(address string) *redis.Client {
redisClient := redis.NewClient(&redis.Options{
Addr: address,
})
redisClient.AddHook(redisotel.TracingHook{})
return redisClient
}
func initService(redisClient *redis.Client) server {
sampleServer := server{
client: redisClient,
router: mux.NewRouter(),
}
sampleServer.routes()
return sampleServer
}