-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (50 loc) · 1.46 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
package main
import (
"net/http"
"strings"
"time"
"github.com/labstack/echo-contrib/jaegertracing"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
// Enable metrics middleware
p := prometheus.NewPrometheus("echo", urlSkipper)
p.Use(e)
// Enable tracing middleware
c := jaegertracing.New(e, urlSkipper)
defer c.Close()
e.GET("/", func(c echo.Context) error {
// Wrap slowFunc on a new span to trace it's execution passing the function arguments
jaegertracing.TraceFunction(c, slowFunc, "Test String")
return c.String(http.StatusOK, "Hello, World!")
})
e.GET("/child-span", func(c echo.Context) error {
// Do something before creating the child span
time.Sleep(40 * time.Millisecond)
sp := jaegertracing.CreateChildSpan(c, "Child span for additional processing")
defer sp.Finish()
sp.LogEvent("Test log")
sp.SetBaggageItem("Test baggage", "baggage")
sp.SetTag("Test tag", "New Tag")
time.Sleep(100 * time.Millisecond)
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
// urlSkipper middleware ignores metrics on some route
func urlSkipper(c echo.Context) bool {
if strings.HasPrefix(c.Path(), "/testurl") {
return true
}
if strings.HasPrefix(c.Path(), "/testurl2") {
return true
}
return false
}
// A function to be wrapped. No need to change it's arguments due to tracing
func slowFunc(s string) {
time.Sleep(200 * time.Millisecond)
return
}