go-pkg/lg/metric.go

104 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-07-12 12:43:25 -06:00
package lg
import (
"context"
"log"
"net/http"
2023-07-26 16:51:35 -06:00
"os"
"runtime/debug"
2023-07-12 12:43:25 -06:00
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/runtime"
"go.opentelemetry.io/otel"
2023-07-26 16:51:35 -06:00
"go.opentelemetry.io/otel/attribute"
2023-07-12 12:43:25 -06:00
"go.opentelemetry.io/otel/exporters/prometheus"
api "go.opentelemetry.io/otel/metric"
sdk "go.opentelemetry.io/otel/sdk/metric"
2023-07-26 16:51:35 -06:00
"go.opentelemetry.io/otel/sdk/resource"
2023-07-12 12:43:25 -06:00
)
var meterKey = contextKey{"meter"}
var promHTTPKey = contextKey{"promHTTP"}
func Meter(ctx context.Context) api.Meter {
2023-07-26 16:51:35 -06:00
if t := fromContext[contextKey, api.Meter](ctx, meterKey); t != nil {
2023-07-12 12:43:25 -06:00
return t
}
2023-07-26 16:51:35 -06:00
log.Printf("default meter")
2023-07-12 12:43:25 -06:00
return otel.Meter("")
}
func NewHTTP(ctx context.Context) *httpHandle {
t := fromContext[contextKey, *prometheus.Exporter](ctx, promHTTPKey)
return &httpHandle{t}
}
func initMetrics(ctx context.Context, name string) (context.Context, func() error) {
2023-07-26 16:51:35 -06:00
goversion := ""
pkg := ""
host := ""
2024-01-15 11:26:54 -07:00
version := "0.0.1"
2023-07-26 16:51:35 -06:00
if info, ok := debug.ReadBuildInfo(); ok {
goversion = info.GoVersion
pkg = info.Path
2024-01-15 11:26:54 -07:00
version = info.Main.Version
2023-07-26 16:51:35 -06:00
}
if h, err := os.Hostname(); err == nil {
host = h
}
2023-07-12 12:43:25 -06:00
ex, err := prometheus.New()
if err != nil {
2023-07-26 16:51:35 -06:00
log.Println(err)
2023-07-12 12:43:25 -06:00
return ctx, nil
}
2023-07-26 16:51:35 -06:00
ctx = toContext(ctx, promHTTPKey, ex)
2023-07-12 12:43:25 -06:00
2023-07-26 16:51:35 -06:00
provider := sdk.NewMeterProvider(
sdk.WithResource(resource.Default()),
sdk.WithView(sdk.NewView(
sdk.Instrument{Name: "histogram_*"},
sdk.Stream{
2023-09-16 12:48:40 -06:00
Aggregation: sdk.AggregationExplicitBucketHistogram{
2023-07-26 16:51:35 -06:00
Boundaries: []float64{
2 << 6, 2 << 8, 2 << 10, 2 << 12, 2 << 14, 2 << 16, 2 << 18, 2 << 20, 2 << 22, 2 << 24, 2 << 26, 2 << 28,
},
},
},
)),
sdk.WithReader(ex),
)
2023-07-12 12:43:25 -06:00
2023-07-26 16:51:35 -06:00
meter := provider.Meter(name,
2024-01-15 11:26:54 -07:00
api.WithInstrumentationVersion(version),
2023-07-26 16:51:35 -06:00
api.WithInstrumentationAttributes(
attribute.String("app", name),
attribute.String("host", host),
attribute.String("go_version", goversion),
attribute.String("pkg", pkg),
),
)
2023-07-12 12:43:25 -06:00
ctx = toContext(ctx, meterKey, meter)
2023-07-26 16:51:35 -06:00
2023-07-12 12:43:25 -06:00
runtime.Start()
return ctx, func() error {
_, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
defer log.Println("metrics stopped")
return nil
}
}
type httpHandle struct {
exp *prometheus.Exporter
}
func (h *httpHandle) RegisterHTTP(mux *http.ServeMux) {
if h.exp == nil {
return
}
mux.Handle("/metrics", promhttp.Handler())
}