diff --git a/example/prometheus/main.go b/example/prometheus/main.go index fada742d32b..95bc4945656 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -31,9 +31,14 @@ 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() @@ -41,8 +46,28 @@ func main() { // 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() + if err != nil { + log.Fatal(err) + } + + 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) @@ -67,14 +92,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()