-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (49 loc) · 1.32 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
package main
import (
"context"
"fmt"
"os"
"time"
"contrib.go.opencensus.io/exporter/jaeger"
"github.com/bzon/errors"
"github.com/go-kit/kit/log"
"go.opencensus.io/trace"
)
func main() {
// logging
var logger log.Logger
{
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
logger = log.With(logger, "ts", log.DefaultTimestamp, "caller", log.DefaultCaller)
}
// tracing
je, err := jaeger.NewExporter(jaeger.Options{
AgentEndpoint: "localhost:6831",
CollectorEndpoint: "http://localhost:14268/api/traces",
ServiceName: "erroring-service",
})
if err != nil {
panic(fmt.Sprintf("Failed to create the Jaeger exporter: %v", err))
}
trace.RegisterExporter(je)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
for {
workerr := work(context.Background(), logger)
if ec, ok := workerr.(errors.ErrorTracer); ok {
logger.Log(
"message", ec.Error(),
"logging.googleapis.com/spanId", ec.TraceContext().SpanID,
"logging.googleapis.com/trace", ec.TraceContext().TraceID,
"logging.googleapis.com/sourceLocation", ec.SourceLocation(),
)
}
time.Sleep(3 * time.Second)
}
}
func work(ctx context.Context, logger log.Logger) error {
_, span := trace.StartSpan(ctx, "work")
defer span.End()
// error with context
err := errors.NewT(span, "error")
return err
}