From cab95bcdef5ff4602ab0a6c69a19223852991afb Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Mon, 23 Oct 2023 11:00:06 -0400 Subject: [PATCH] Move view example into prometheus example, and deprecate /example/view (#4649) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * move view example into prometheus example, and deprecate /example/view * fix lint --------- Co-authored-by: Robert PajÄ…k --- CHANGELOG.md | 2 ++ example/prometheus/go.mod | 2 +- example/prometheus/main.go | 32 +++++++++++++++++++++++++------- example/view/doc.go | 2 ++ example/view/go.mod | 1 + 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bb7a9673..857961257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add the `go.opentelemetry.io/otel/trace/embedded` package to be embedded in the exported trace API interfaces. (#4620) - Add the `go.opentelemetry.io/otel/trace/noop` package as a default no-op implementation of the trace API. (#4620) - Add context propagation in `go.opentelemetry.io/otel/example/dice`. (#4644) +- Add view configuration to `go.opentelemetry.io/otel/example/prometheus`. (#4649) ### Deprecated @@ -22,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Deprecate `go.opentelemetry.io/otel/example/fib` package is in favor of `go.opentelemetry.io/otel/example/dice`. (#4618) - Deprecate `go.opentelemetry.io/otel/trace.NewNoopTracerProvider`. Use the added `NewTracerProvider` function in `go.opentelemetry.io/otel/trace/noop` instead. (#4620) +- Deprecate `go.opentelemetry.io/otel/example/view` package in favor of `go.opentelemetry.io/otel/example/prometheus`. (#4649) ### Changed diff --git a/example/prometheus/go.mod b/example/prometheus/go.mod index 6ae665530..b2c201160 100644 --- a/example/prometheus/go.mod +++ b/example/prometheus/go.mod @@ -7,6 +7,7 @@ require ( go.opentelemetry.io/otel v1.19.0 go.opentelemetry.io/otel/exporters/prometheus v0.42.0 go.opentelemetry.io/otel/metric v1.19.0 + go.opentelemetry.io/otel/sdk v1.19.0 go.opentelemetry.io/otel/sdk/metric v1.19.0 ) @@ -20,7 +21,6 @@ require ( github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect - go.opentelemetry.io/otel/sdk v1.19.0 // indirect go.opentelemetry.io/otel/trace v1.19.0 // indirect golang.org/x/sys v0.13.0 // indirect google.golang.org/protobuf v1.31.0 // indirect diff --git a/example/prometheus/main.go b/example/prometheus/main.go index fee550de6..81f230855 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -29,9 +29,12 @@ import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/exporters/prometheus" api "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/sdk/instrumentation" "go.opentelemetry.io/otel/sdk/metric" ) +const meterName = "github.com/open-telemetry/opentelemetry-go/example/prometheus" + func main() { rng := rand.New(rand.NewSource(time.Now().UnixNano())) ctx := context.Background() @@ -43,8 +46,23 @@ func main() { if err != nil { log.Fatal(err) } - provider := metric.NewMeterProvider(metric.WithReader(exporter)) - meter := provider.Meter("github.com/open-telemetry/opentelemetry-go/example/prometheus") + provider := metric.NewMeterProvider( + metric.WithReader(exporter), + // View to customize histogram buckets and rename a single histogram instrument. + metric.WithView(metric.NewView( + metric.Instrument{ + Name: "baz", + Scope: instrumentation.Scope{Name: meterName}, + }, + metric.Stream{ + Name: "new_baz", + Aggregation: metric.AggregationExplicitBucketHistogram{ + Boundaries: []float64{64, 128, 256, 512, 1024, 2048, 4096}, + }, + }, + )), + ) + meter := provider.Meter(meterName) // Start the prometheus HTTP server and pass the exporter Collector to it go serveMetrics() @@ -75,14 +93,14 @@ func main() { } // This is the equivalent of prometheus.NewHistogramVec - histogram, err := meter.Float64Histogram("baz", api.WithDescription("a very nice histogram")) + histogram, err := meter.Float64Histogram("baz", api.WithDescription("a histogram with custom buckets and rename")) if err != nil { log.Fatal(err) } - histogram.Record(ctx, 23, opt) - histogram.Record(ctx, 7, opt) - histogram.Record(ctx, 101, opt) - histogram.Record(ctx, 105, opt) + histogram.Record(ctx, 136, opt) + histogram.Record(ctx, 64, opt) + histogram.Record(ctx, 701, opt) + histogram.Record(ctx, 830, opt) ctx, _ = signal.NotifyContext(ctx, os.Interrupt) <-ctx.Done() diff --git a/example/view/doc.go b/example/view/doc.go index 6307e5f20..ccf2a205d 100644 --- a/example/view/doc.go +++ b/example/view/doc.go @@ -13,4 +13,6 @@ // limitations under the License. // Package main provides a code sample of using metric views to customize instruments. +// +// Deprecated: See [go.opentelemetry.io/otel/example/prometheus] instead. package main diff --git a/example/view/go.mod b/example/view/go.mod index 0941e70f6..2bafabc99 100644 --- a/example/view/go.mod +++ b/example/view/go.mod @@ -1,3 +1,4 @@ +// Deprecated: see go.opentelemetry.io/otel/example/prometheus instead. module go.opentelemetry.io/otel/example/view go 1.20