-
Notifications
You must be signed in to change notification settings - Fork 13
/
webhooks.go
52 lines (42 loc) · 1.46 KB
/
webhooks.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
package tracing
import (
"context"
"github.com/MainfluxLabs/mainflux/webhooks"
"github.com/opentracing/opentracing-go"
)
var (
_ webhooks.WebhookRepository = (*webhookRepositoryMiddleware)(nil)
)
type webhookRepositoryMiddleware struct {
tracer opentracing.Tracer
repo webhooks.WebhookRepository
}
// WebhookRepositoryMiddleware tracks request and their latency, and adds spans
// to context.
func WebhookRepositoryMiddleware(tracer opentracing.Tracer, repo webhooks.WebhookRepository) webhooks.WebhookRepository {
return webhookRepositoryMiddleware{
tracer: tracer,
repo: repo,
}
}
func (wrm webhookRepositoryMiddleware) Save(ctx context.Context, whs ...webhooks.Webhook) ([]webhooks.Webhook, error) {
span := createSpan(ctx, wrm.tracer, "save_webhooks")
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return wrm.repo.Save(ctx, whs...)
}
func (wrm webhookRepositoryMiddleware) RetrieveByThingID(ctx context.Context, thingID string) ([]webhooks.Webhook, error) {
span := createSpan(ctx, wrm.tracer, "retrieve_webhooks_by_thing")
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return wrm.repo.RetrieveByThingID(ctx, thingID)
}
func createSpan(ctx context.Context, tracer opentracing.Tracer, opName string) opentracing.Span {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
return tracer.StartSpan(
opName,
opentracing.ChildOf(parentSpan.Context()),
)
}
return tracer.StartSpan(opName)
}