-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentracing.go
59 lines (47 loc) · 1.83 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"
"github.com/alexfalkowski/go-service/trace/opentracing"
"github.com/alexfalkowski/go-service/version"
"github.com/alexfalkowski/migrieren/migrate/migrator"
otr "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"go.uber.org/fx"
)
// Tracer for opentracing.
type Tracer otr.Tracer
// StartSpanFromContext for opentracing.
func StartSpanFromContext(ctx context.Context, tracer Tracer, operation, method string, opts ...otr.StartSpanOption) (context.Context, otr.Span) {
return opentracing.StartSpanFromContext(ctx, tracer, "migrator", operation, method, opts...)
}
// NewTracer for opentracing.
func NewTracer(lc fx.Lifecycle, cfg *opentracing.Config, version version.Version) (Tracer, error) {
return opentracing.NewTracer(opentracing.TracerParams{Lifecycle: lc, Name: "migrator", Config: cfg, Version: version})
}
// Migrator for opentracing.
type Migrator struct {
migrator migrator.Migrator
tracer Tracer
}
// NewMigrator for opentracing.
func NewMigrator(migrator migrator.Migrator, tracer Tracer) *Migrator {
return &Migrator{migrator: migrator, tracer: tracer}
}
// Migrate a database to a version and returning the database logs.
func (m *Migrator) Migrate(ctx context.Context, source, db string, version uint64) ([]string, error) {
ctx, span := StartSpanFromContext(ctx, m.tracer, "migrate", fmt.Sprintf("db to version %d", version))
defer span.Finish()
logs, err := m.migrator.Migrate(ctx, source, db, version)
if err != nil {
ext.Error.Set(span, true)
span.LogFields(log.String("event", "error"), log.String("message", err.Error()))
return nil, err
}
return logs, nil
}
// Ping the migrator.
func (m *Migrator) Ping(ctx context.Context, source, db string) error {
return m.migrator.Ping(ctx, source, db)
}