|
1
|
|
|
package telemetry |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"os" |
|
7
|
|
|
"runtime" |
|
8
|
|
|
"time" |
|
9
|
|
|
|
|
10
|
|
|
"go.opentelemetry.io/otel" |
|
11
|
|
|
"go.opentelemetry.io/otel/metric/noop" |
|
12
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.10.0" |
|
13
|
|
|
|
|
14
|
|
|
"go.opentelemetry.io/contrib/instrumentation/host" |
|
15
|
|
|
orn "go.opentelemetry.io/contrib/instrumentation/runtime" |
|
16
|
|
|
"go.opentelemetry.io/otel/attribute" |
|
17
|
|
|
omt "go.opentelemetry.io/otel/metric" |
|
18
|
|
|
"go.opentelemetry.io/otel/sdk/metric" |
|
19
|
|
|
"go.opentelemetry.io/otel/sdk/resource" |
|
20
|
|
|
|
|
21
|
|
|
"github.com/Permify/permify/internal" |
|
22
|
|
|
) |
|
23
|
|
|
|
|
24
|
|
|
// NewMeter - Creates new meter |
|
25
|
|
|
func NewMeter(exporter metric.Exporter, interval time.Duration) func(context.Context) error { |
|
26
|
|
|
hostName, err := os.Hostname() |
|
27
|
|
|
if err != nil { |
|
28
|
|
|
return func(context.Context) error { return nil } |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
mp := metric.NewMeterProvider( |
|
32
|
|
|
metric.WithReader(metric.NewPeriodicReader( |
|
33
|
|
|
exporter, |
|
34
|
|
|
metric.WithInterval(interval), |
|
35
|
|
|
)), |
|
36
|
|
|
metric.WithResource(resource.NewWithAttributes( |
|
37
|
|
|
semconv.SchemaURL, |
|
38
|
|
|
semconv.ServiceNameKey.String("permify"), |
|
39
|
|
|
attribute.String("id", internal.Identifier), |
|
40
|
|
|
attribute.String("project.id", internal.Identifier), |
|
41
|
|
|
attribute.String("version", internal.Version), |
|
42
|
|
|
attribute.String("host_name", hostName), |
|
43
|
|
|
attribute.String("os", runtime.GOOS), |
|
44
|
|
|
attribute.String("arch", runtime.GOARCH), |
|
45
|
|
|
)), |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
if err = orn.Start( |
|
49
|
|
|
orn.WithMinimumReadMemStatsInterval(time.Second), |
|
50
|
|
|
orn.WithMeterProvider(mp), |
|
51
|
|
|
); err != nil { |
|
52
|
|
|
return func(context.Context) error { return nil } |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if err = host.Start(host.WithMeterProvider(mp)); err != nil { |
|
56
|
|
|
return func(context.Context) error { return nil } |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
otel.SetMeterProvider(mp) |
|
60
|
|
|
|
|
61
|
|
|
return mp.Shutdown |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// NewNoopMeter - Creates new noop meter |
|
65
|
|
|
func NewNoopMeter() omt.Meter { |
|
66
|
|
|
mp := noop.MeterProvider{} |
|
67
|
|
|
return mp.Meter("permify") |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
func NewCounter(meter omt.Meter, name, description string) omt.Int64Counter { |
|
71
|
|
|
counter, err := meter.Int64Counter( |
|
72
|
|
|
name, |
|
73
|
|
|
omt.WithDescription(description), |
|
74
|
|
|
) |
|
75
|
|
|
|
|
76
|
|
|
if err != nil { |
|
77
|
|
|
fmt.Errorf("failed to create counter: %w", err) |
|
|
|
|
|
|
78
|
|
|
panic(err) |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return counter |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
func NewHistogram(meter omt.Meter, name, unit, description string) omt.Int64Histogram { |
|
85
|
|
|
histogram, err := meter.Int64Histogram( |
|
86
|
|
|
name, |
|
87
|
|
|
omt.WithUnit(unit), |
|
88
|
|
|
omt.WithDescription(description), |
|
89
|
|
|
) |
|
90
|
|
|
|
|
91
|
|
|
if err != nil { |
|
92
|
|
|
fmt.Errorf("failed to create histogram: %w", err) |
|
|
|
|
|
|
93
|
|
|
panic(err) |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return histogram |
|
97
|
|
|
} |
|
98
|
|
|
|