fix: metrics handling

This commit is contained in:
xuu 2023-07-26 16:51:35 -06:00
parent fcd9ab4d56
commit 5ad7fce0ac
Signed by: xuu
GPG Key ID: C63E6D61F3035024
2 changed files with 48 additions and 43 deletions

View File

@ -1,23 +1,23 @@
scalar Time scalar Time
scalar Map scalar Map
type Connection @goModel(model: "go.sour.is/ev/pkg/gql.Connection") { type Connection @goModel(model: "go.sour.is/pkg/gql.Connection") {
paging: PageInfo! paging: PageInfo!
edges: [Edge!]! edges: [Edge!]!
} }
input PageInput @goModel(model: "go.sour.is/ev/pkg/gql.PageInput") { input PageInput @goModel(model: "go.sour.is/pkg/gql.PageInput") {
after: Int = 0 after: Int = 0
before: Int before: Int
count: Int = 30 count: Int = 30
} }
type PageInfo @goModel(model: "go.sour.is/ev/pkg/gql.PageInfo") { type PageInfo @goModel(model: "go.sour.is/pkg/gql.PageInfo") {
next: Boolean! next: Boolean!
prev: Boolean! prev: Boolean!
begin: Int! begin: Int!
end: Int! end: Int!
} }
interface Edge @goModel(model: "go.sour.is/ev/pkg/gql.Edge"){ interface Edge @goModel(model: "go.sour.is/pkg/gql.Edge"){
id: ID! id: ID!
} }

View File

@ -4,25 +4,30 @@ import (
"context" "context"
"log" "log"
"net/http" "net/http"
"os"
"runtime/debug"
"time" "time"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/runtime" "go.opentelemetry.io/contrib/instrumentation/runtime"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/prometheus" "go.opentelemetry.io/otel/exporters/prometheus"
api "go.opentelemetry.io/otel/metric" api "go.opentelemetry.io/otel/metric"
sdk "go.opentelemetry.io/otel/sdk/metric" sdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/resource"
) )
var meterKey = contextKey{"meter"} var meterKey = contextKey{"meter"}
var promHTTPKey = contextKey{"promHTTP"} var promHTTPKey = contextKey{"promHTTP"}
func Meter(ctx context.Context) api.Meter { func Meter(ctx context.Context) api.Meter {
if t := fromContext[contextKey, api.Meter](ctx, tracerKey); t != nil { if t := fromContext[contextKey, api.Meter](ctx, meterKey); t != nil {
return t return t
} }
log.Printf("default meter")
return otel.Meter("") return otel.Meter("")
} }
func NewHTTP(ctx context.Context) *httpHandle { func NewHTTP(ctx context.Context) *httpHandle {
@ -31,50 +36,50 @@ func NewHTTP(ctx context.Context) *httpHandle {
} }
func initMetrics(ctx context.Context, name string) (context.Context, func() error) { func initMetrics(ctx context.Context, name string) (context.Context, func() error) {
// goversion := "" goversion := ""
// pkg := "" pkg := ""
// host := "" host := ""
// if info, ok := debug.ReadBuildInfo(); ok { if info, ok := debug.ReadBuildInfo(); ok {
// goversion = info.GoVersion goversion = info.GoVersion
// pkg = info.Path pkg = info.Path
// } }
// if h, err := os.Hostname(); err == nil { if h, err := os.Hostname(); err == nil {
// host = h host = h
// } }
// config := prometheus.Config{
// DefaultHistogramBoundaries: []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,
// },
// }
// cont := controller.New(
// processor.NewFactory(
// selector.NewWithHistogramDistribution(
// histogram.WithExplicitBoundaries(config.DefaultHistogramBoundaries),
// ),
// aggregation.CumulativeTemporalitySelector(),
// processor.WithMemory(true),
// ),
// controller.WithResource(
// resource.NewWithAttributes(
// semconv.SchemaURL,
// attribute.String("app", name),
// attribute.String("host", host),
// attribute.String("go_version", goversion),
// attribute.String("pkg", pkg),
// ),
// ),
// )
ex, err := prometheus.New() ex, err := prometheus.New()
if err != nil { if err != nil {
log.Println(err)
return ctx, nil return ctx, nil
} }
provider := sdk.NewMeterProvider(sdk.WithReader(ex))
meter := provider.Meter(name)
ctx = toContext(ctx, promHTTPKey, ex) ctx = toContext(ctx, promHTTPKey, ex)
provider := sdk.NewMeterProvider(
sdk.WithResource(resource.Default()),
sdk.WithView(sdk.NewView(
sdk.Instrument{Name: "histogram_*"},
sdk.Stream{
Aggregation: aggregation.ExplicitBucketHistogram{
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),
)
meter := provider.Meter(name,
api.WithInstrumentationVersion("0.0.1"),
api.WithInstrumentationAttributes(
attribute.String("app", name),
attribute.String("host", host),
attribute.String("go_version", goversion),
attribute.String("pkg", pkg),
),
)
ctx = toContext(ctx, meterKey, meter) ctx = toContext(ctx, meterKey, meter)
runtime.Start() runtime.Start()
return ctx, func() error { return ctx, func() error {