-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentracing.go
59 lines (47 loc) · 1.66 KB
/
opentracing.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
package opentracing
import (
"context"
"fmt"
"strings"
"time"
stime "github.com/alexfalkowski/go-service/time"
gopentracing "github.com/alexfalkowski/go-service/transport/grpc/trace/opentracing"
"github.com/alexfalkowski/migrieren/client"
"github.com/alexfalkowski/migrieren/client/task"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
)
// Client for opentracing.
type Client struct {
cfg *client.Config
tracer gopentracing.Tracer
task.Task
}
// NewClient for zap.
func NewClient(cfg *client.Config, tracer gopentracing.Tracer, task task.Task) *Client {
return &Client{cfg: cfg, tracer: tracer, Task: task}
}
// Perform tracing for client.
func (c *Client) Perform(ctx context.Context) ([]string, error) {
start := time.Now().UTC()
operationName := fmt.Sprintf("sync %s/%d", c.cfg.Database, c.cfg.Version)
opts := []opentracing.StartSpanOption{
opentracing.Tag{Key: "client.start_time", Value: start.Format(time.RFC3339)},
opentracing.Tag{Key: "client.database", Value: c.cfg.Database},
opentracing.Tag{Key: "client.version", Value: c.cfg.Version},
opentracing.Tag{Key: "component", Value: "client"},
ext.SpanKindRPCClient,
}
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, c.tracer, operationName, opts...)
defer span.Finish()
logs, err := c.Task.Perform(ctx)
span.SetTag("client.duration", stime.ToMilliseconds(time.Since(start)))
if err != nil {
ext.Error.Set(span, true)
span.LogFields(log.String("event", "error"), log.String("message", err.Error()))
return nil, err
}
span.SetTag("client.logs", strings.Join(logs, ","))
return logs, nil
}