-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
112 lines (83 loc) · 2.81 KB
/
hello.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
package main
import (
"log"
"net/http"
"time"
jaegerExp "contrib.go.opencensus.io/exporter/jaeger"
"contrib.go.opencensus.io/exporter/ocagent"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
jaegerClient "github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"go.opencensus.io/trace"
)
func withOpenTracing() {
router := initializeRouter()
withJaegerClientOpentracing() // add the exporter
err := router.Run()
if err != nil {
log.Fatal("error while starting the service")
}
}
func initializeRouter() *gin.Engine {
router := gin.Default()
logrus.Info("initializing router!")
router.GET("/hello", handleHelloWithOpenTracing) // add respective handler func for the exporter
return router
}
func handleHelloWithOpenCensus(ctx *gin.Context) {
_, span := trace.StartSpan(ctx, "/hello-service")
defer span.End()
span.AddAttributes(trace.StringAttribute("aKey", "aValue"))
span.Annotate([]trace.Attribute{trace.StringAttribute("anAnnotatedKey", "anAnnotatedValue")}, "an annotation string")
mapOfAtt := map[string]interface{}{}
span.AddLink(trace.Link{Attributes: mapOfAtt})
logrus.Info("in hello handler!")
ctx.JSON(http.StatusOK, gin.H{"data": "hello world"})
}
func handleHelloWithOpenTracing(ctx *gin.Context) {
tracer := opentracing.GlobalTracer()
tracer.StartSpan("hello-opertation")
logrus.Info("logrus info in handle func")
log.Println("log println in handle func")
ctx.JSON(http.StatusOK, gin.H{"data": "hello world"})
}
func withOCAgentExporter() {
oce, _ := ocagent.NewExporter(ocagent.WithInsecure(),
ocagent.WithReconnectionPeriod(1*time.Second),
ocagent.WithAddress("localhost:55678"),
ocagent.WithServiceName("HEALTH_SERVICE_OCAGENT"))
trace.RegisterExporter(oce)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}
func withOCJaegerExporter() {
collectorEndpointURI := "http://localhost:14268/api/traces"
je, err := jaegerExp.NewExporter(jaegerExp.Options{
Endpoint: collectorEndpointURI,
ServiceName: "HEALTH_SERVICE_JAEGER_EXPORTER",
})
if err != nil {
log.Fatalf("Failed to create the Jaeger exporter: %v", err)
}
trace.RegisterExporter(je)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}
func withJaegerClientOpentracing() {
collectorEndpointURI := "http://localhost:14268/api/traces"
jaegerConfig := &config.Configuration{
ServiceName: "HEALTH_SERVICE_JAEGER_CLIENT",
Sampler: &config.SamplerConfig{Type: "const", Param: 1},
Reporter: &config.ReporterConfig{
LogSpans: true,
CollectorEndpoint: collectorEndpointURI,
},
}
closer, err := jaegerConfig.InitGlobalTracer(
"HEALTH_SERVICE_JAEGER_CLIENT",
config.Logger(jaegerClient.StdLogger))
if err != nil {
logrus.Fatal("error in jaeger init", err)
}
defer closer.Close()
}