-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservability.go
More file actions
76 lines (70 loc) · 2.68 KB
/
Copy pathobservability.go
File metadata and controls
76 lines (70 loc) · 2.68 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package glock
import (
"context"
"fmt"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
// RecordStart starts a new tracing span for a given operation.
func RecordStart(ctx context.Context, backend, action, lockID string) (context.Context, trace.Span) {
GetLogger().Info(fmt.Sprintf("attempting to %s lock", action), "lockID", lockID)
return GetTracer().Start(
ctx,
fmt.Sprintf("%s_lock.%s", backend, action),
trace.WithAttributes(
attribute.String("lock.id", lockID),
attribute.String("backend", backend),
),
)
}
// HandleError logs, records metrics, and returns a formatted error.
func HandleError(
ctx context.Context,
span trace.Span,
err error,
backend, action, msg, lockID string) error {
span.RecordError(err)
span.SetStatus(codes.Error, msg)
GetLogger().Error(err, msg, "lockID", lockID)
metrics := GetMetrics()
switch action {
case ActionAcquire:
metrics.lockAcquiredCounter.Add(ctx, 1,
metric.WithAttributes(attribute.Bool("success", false), attribute.String("backend", backend)))
case ActionRelease:
metrics.lockReleaseCounter.Add(ctx, 1,
metric.WithAttributes(attribute.Bool("success", false), attribute.String("backend", backend)))
case ActionRenew:
metrics.lockRenewCounter.Add(ctx, 1,
metric.WithAttributes(attribute.Bool("success", false), attribute.String("backend", backend)))
}
return fmt.Errorf("%s: %w", msg, err)
}
// RecordSuccess logs and records success metrics.
func RecordSuccess(
ctx context.Context,
span trace.Span,
startTime time.Time,
action, backend, lockID string) {
metrics := GetMetrics()
GetLogger().Info(fmt.Sprintf("lock %s successfully", action), "lockID", lockID)
duration := time.Since(startTime).Seconds()
span.SetStatus(codes.Ok, fmt.Sprintf("lock %s", action))
switch action {
case ActionAcquiredSuccessfully:
metrics.lockAcquiredCounter.Add(ctx, 1,
metric.WithAttributes(attribute.Bool("success", true), attribute.String("backend", backend)))
metrics.lockAcquireLatency.Record(ctx, duration, metric.WithAttributes(attribute.String("backend", backend)))
case ActionReleasedSuccessfully:
metrics.lockReleaseCounter.Add(ctx, 1,
metric.WithAttributes(attribute.Bool("success", true), attribute.String("backend", backend)))
metrics.lockReleaseLatency.Record(ctx, duration, metric.WithAttributes(attribute.String("backend", backend)))
case ActionRenewedSuccessfully:
metrics.lockRenewCounter.Add(ctx, 1,
metric.WithAttributes(attribute.Bool("success", true), attribute.String("backend", backend)))
metrics.lockRenewLatency.Record(ctx, duration, metric.WithAttributes(attribute.String("backend", backend)))
}
}