Skip to content

Commit

Permalink
Add an example of custom histogram buckets in prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
tony612 committed Sep 16, 2022
1 parent b1e5e35 commit 60bc417
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions example/prometheus/main.go
Expand Up @@ -31,18 +31,40 @@ import (
"go.opentelemetry.io/otel/attribute"
otelprom "go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/view"
)

const meterName = "github.com/open-telemetry/opentelemetry-go/example/prometheus"

func main() {
ctx := context.Background()

// The exporter embeds a default OpenTelemetry Reader and
// implements prometheus.Collector, allowing it to be used as
// both a Reader and Collector.
exporter := otelprom.New()
provider := metric.NewMeterProvider(metric.WithReader(exporter))
meter := provider.Meter("github.com/open-telemetry/opentelemetry-go/example/prometheus")

// View to customize histogram
customBucketsView, err := view.New(
view.MatchInstrumentName("custom_histogram"),
view.MatchInstrumentationScope(instrumentation.Scope{Name: meterName}),
view.WithSetAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: []float64{64, 128, 256, 512, 1024, 2048, 4096},
}),
view.WithRename("qux"),
)
if err != nil {
log.Fatal(err)
}

// Default view to keep all instruments
defaultView, err := view.New()

provider := metric.NewMeterProvider(metric.WithReader(exporter, customBucketsView, defaultView))
meter := provider.Meter(meterName)

// Start the prometheus HTTP server and pass the exporter Collector to it
go serveMetrics(exporter.Collector)
Expand All @@ -67,14 +89,23 @@ func main() {
gauge.Add(ctx, -25, attrs...)

// This is the equivalent of prometheus.NewHistogramVec
histogram, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram"))
histogram1, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram"))
if err != nil {
log.Fatal(err)
}
histogram1.Record(ctx, 23, attrs...)
histogram1.Record(ctx, 7, attrs...)
histogram1.Record(ctx, 101, attrs...)
histogram1.Record(ctx, 105, attrs...)

histogram2, err := meter.SyncFloat64().Histogram("custom_histogram", instrument.WithDescription("a histogram with custom buckets and rename"))
if err != nil {
log.Fatal(err)
}
histogram.Record(ctx, 23, attrs...)
histogram.Record(ctx, 7, attrs...)
histogram.Record(ctx, 101, attrs...)
histogram.Record(ctx, 105, attrs...)
histogram2.Record(ctx, 136, attrs...)
histogram2.Record(ctx, 64, attrs...)
histogram2.Record(ctx, 701, attrs...)
histogram2.Record(ctx, 830, attrs...)

ctx, _ = signal.NotifyContext(ctx, os.Interrupt)
<-ctx.Done()
Expand Down

0 comments on commit 60bc417

Please sign in to comment.