-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
56 lines (44 loc) · 1.44 KB
/
client.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
package otfranz
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/twmb/franz-go/pkg/kgo"
)
// Client is a decorator around *kgo.Client that provides tracing capabilities.
type Client struct {
*kgo.Client
tracer opentracing.Tracer
}
// NewClient takes a *kgo.Client and returns a decorated Client.
func NewClient(client *kgo.Client, tracer opentracing.Tracer) *Client {
return &Client{Client: client, tracer: tracer}
}
// ProduceWithTracing wrap Produce method with tracing.
func (c *Client) ProduceWithTracing(ctx context.Context, r *kgo.Record, promise func(*kgo.Record, error)) {
if c.tracer == nil {
c.Produce(ctx, r, promise)
return
}
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, c.tracer, "kafka producer")
defer span.Finish()
ext.SpanKind.Set(span, ext.SpanKindProducerEnum)
c.Produce(ctx, r, func(record *kgo.Record, err error) {
if err != nil {
ext.LogError(span, err)
}
if promise != nil {
promise(record, err)
}
})
}
// ProduceSyncWithTracing wrap ProduceSync method with tracing.
func (c *Client) ProduceSyncWithTracing(ctx context.Context, rs ...*kgo.Record) kgo.ProduceResults {
if c.tracer == nil {
return c.ProduceSync(ctx, rs...)
}
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, c.tracer, "kafka producer")
defer span.Finish()
ext.SpanKind.Set(span, ext.SpanKindProducerEnum)
return c.ProduceSync(ctx, rs...)
}