zitadel/internal/telemetry/metrics/otel/open_telemetry.go
Fabi 6b3f5b984c
feat: metrics (#1024)
* refactor: switch from opencensus to opentelemetry

* tempo works as designed nooooot

* fix: log traceids

* with grafana agent

* fix: http tracing

* fix: cleanup files

* chore: remove todo

* fix: bad test

* fix: ignore methods in grpc interceptors

* fix: remove test log

* clean up

* typo

* fix(config): configure tracing endpoint

* fix(span): add error id to span

* feat: metrics package

* feat: metrics package

* fix: counter

* fix: metric

* try metrics

* fix: coutner metrics

* fix: active sessin counter

* fix: active sessin counter

* fix: change current Sequence table

* fix: change current Sequence table

* fix: current sequences

* fix: spooler div metrics

* fix: console view

* fix: merge master

* fix: Last spool run on search result instead of eventtimestamp

* fix: go mod

* Update console/src/assets/i18n/de.json

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: pr review

* fix: map

* update oidc pkg

* fix: handlers

* fix: value observer

* fix: remove fmt

* fix: handlers

* fix: tests

* fix: handler minimum cycle duration 1s

* fix(spooler): handler channel buffer

* fix interceptors

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2020-12-02 08:50:59 +01:00

93 lines
2.5 KiB
Go

package otel
import (
"context"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/telemetry/metrics"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/exporters/metric/prometheus"
"go.opentelemetry.io/otel/label"
"net/http"
"sync"
)
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) {
exporter, err := prometheus.NewExportPipeline(
prometheus.Config{},
)
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
}
func (m *Metrics) AddCount(ctx context.Context, name string, value int64, labels map[string]interface{}) error {
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
}
sumObserver := metric.Must(m.Meter).NewInt64UpDownSumObserver(
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
}
sumObserver := metric.Must(m.Meter).NewInt64ValueObserver(
name, callbackFunc, metric.WithDescription(description))
m.UpDownSumObserver.Store(name, sumObserver)
return nil
}
func MapToKeyValue(labels map[string]interface{}) []label.KeyValue {
if labels == nil {
return nil
}
keyValues := make([]label.KeyValue, 0, len(labels))
for key, value := range labels {
keyValues = append(keyValues, label.Any(key, value))
}
return keyValues
}