github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/pkg/metrics/metrics.go (about) 1 /*This file is part of kuberpult. 2 3 Kuberpult is free software: you can redistribute it and/or modify 4 it under the terms of the Expat(MIT) License as published by 5 the Free Software Foundation. 6 7 Kuberpult is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 MIT License for more details. 11 12 You should have received a copy of the MIT License 13 along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. 14 15 Copyright 2023 freiheit.com*/ 16 17 package metrics 18 19 import ( 20 "context" 21 "net/http" 22 23 "go.opentelemetry.io/otel/metric" 24 sdkmetric "go.opentelemetry.io/otel/sdk/metric" 25 26 "github.com/prometheus/client_golang/prometheus" 27 "github.com/prometheus/client_golang/prometheus/promhttp" 28 otelprom "go.opentelemetry.io/otel/exporters/prometheus" 29 ) 30 31 const ( 32 EventTagApplication = "kuberpult_application" 33 EventTagEnvironment = "kuberpult_environment" 34 EventTagEnvironmentGroup = "kuberpult_environment_group" 35 ) 36 37 func Init() (metric.MeterProvider, http.Handler, error) { 38 39 reg := prometheus.NewPedanticRegistry() 40 41 promExp, err := otelprom.New(otelprom.WithRegisterer(reg), otelprom.WithoutScopeInfo(), otelprom.WithoutTargetInfo()) 42 if err != nil { 43 return nil, nil, err 44 } 45 meterProvider := sdkmetric.NewMeterProvider( 46 sdkmetric.WithReader(promExp), 47 ) 48 49 //exhaustruct:ignore 50 return meterProvider, promhttp.HandlerFor(reg, promhttp.HandlerOpts{}), nil 51 } 52 53 type ctxKeyType struct{} 54 55 var ctxKey ctxKeyType = ctxKeyType{} 56 57 func FromContext(ctx context.Context) metric.MeterProvider { 58 return ctx.Value(ctxKey).(metric.MeterProvider) 59 } 60 61 func WithProvider(ctx context.Context, pv metric.MeterProvider) context.Context { 62 return context.WithValue(ctx, ctxKey, pv) 63 }