2020-12-02 07:50:59 +00:00
|
|
|
package otel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2021-08-10 05:27:27 +00:00
|
|
|
|
2021-09-23 10:50:17 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2021-09-21 12:58:26 +00:00
|
|
|
"go.opentelemetry.io/otel/exporters/prometheus"
|
2021-08-10 05:27:27 +00:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
|
|
|
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
|
|
|
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple"
|
|
|
|
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/telemetry/metrics"
|
2020-12-02 07:50:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Metrics struct {
|
|
|
|
Exporter *prometheus.Exporter
|
|
|
|
Meter metric.Meter
|
|
|
|
Counters sync.Map
|
|
|
|
UpDownSumObserver sync.Map
|
|
|
|
ValueObservers sync.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMetrics(meterName string) (metrics.Metrics, error) {
|
2021-08-10 05:27:27 +00:00
|
|
|
exporter, err := prometheus.New(
|
2020-12-02 07:50:59 +00:00
|
|
|
prometheus.Config{},
|
2021-08-10 05:27:27 +00:00
|
|
|
controller.New(
|
|
|
|
processor.New(
|
|
|
|
selector.NewWithHistogramDistribution(),
|
|
|
|
export.CumulativeExportKindSelector(),
|
|
|
|
processor.WithMemory(true),
|
|
|
|
),
|
|
|
|
),
|
2020-12-02 07:50:59 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return &Metrics{}, err
|
|
|
|
}
|
|
|
|
return &Metrics{
|
|
|
|
Exporter: exporter,
|
|
|
|
Meter: exporter.MeterProvider().Meter(meterName),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) GetExporter() http.Handler {
|
|
|
|
return m.Exporter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) GetMetricsProvider() metric.MeterProvider {
|
|
|
|
return m.Exporter.MeterProvider()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) RegisterCounter(name, description string) error {
|
|
|
|
if _, exists := m.Counters.Load(name); exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
counter := metric.Must(m.Meter).NewInt64Counter(name, metric.WithDescription(description))
|
|
|
|
m.Counters.Store(name, counter)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 10:50:17 +00:00
|
|
|
func (m *Metrics) AddCount(ctx context.Context, name string, value int64, labels map[string]attribute.Value) error {
|
2020-12-02 07:50:59 +00:00
|
|
|
counter, exists := m.Counters.Load(name)
|
|
|
|
if !exists {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "METER-4u8fs", "Errors.Metrics.Counter.NotFound")
|
|
|
|
}
|
|
|
|
counter.(metric.Int64Counter).Add(ctx, value, MapToKeyValue(labels)...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) RegisterUpDownSumObserver(name, description string, callbackFunc metric.Int64ObserverFunc) error {
|
|
|
|
if _, exists := m.UpDownSumObserver.Load(name); exists {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-21 12:58:26 +00:00
|
|
|
sumObserver := metric.Must(m.Meter).NewInt64UpDownCounterObserver(
|
2020-12-02 07:50:59 +00:00
|
|
|
name, callbackFunc, metric.WithDescription(description))
|
|
|
|
|
|
|
|
m.UpDownSumObserver.Store(name, sumObserver)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) RegisterValueObserver(name, description string, callbackFunc metric.Int64ObserverFunc) error {
|
|
|
|
if _, exists := m.UpDownSumObserver.Load(name); exists {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-21 12:58:26 +00:00
|
|
|
sumObserver := metric.Must(m.Meter).NewInt64GaugeObserver(
|
2020-12-02 07:50:59 +00:00
|
|
|
name, callbackFunc, metric.WithDescription(description))
|
|
|
|
|
|
|
|
m.UpDownSumObserver.Store(name, sumObserver)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 10:50:17 +00:00
|
|
|
func MapToKeyValue(labels map[string]attribute.Value) []attribute.KeyValue {
|
2020-12-02 07:50:59 +00:00
|
|
|
if labels == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-23 10:50:17 +00:00
|
|
|
keyValues := make([]attribute.KeyValue, 0, len(labels))
|
2020-12-02 07:50:59 +00:00
|
|
|
for key, value := range labels {
|
2021-09-23 10:50:17 +00:00
|
|
|
keyValues = append(keyValues, attribute.KeyValue{
|
|
|
|
Key: attribute.Key(key),
|
|
|
|
Value: value,
|
|
|
|
})
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
|
|
|
return keyValues
|
|
|
|
}
|