1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-12-03 23:21:27 +02:00
Files
opentelemetry-go/example/view/main.go
Tyler Yahn f8fcfda872 Add options to measurement methods (#3971)
* Add options to measurement methods

* Update noop

* Update global impl

* Update SDK impl

* Fix metric API example

* Update prometheus exporter tests

* Update examples

* WithAttributes and WithAttributeSet

* Add changes to changelog

* Accept slice instead of variadic to new conf funcs

* Clarify WithAttributes performance in docs

* Address feedback about WithAttributes comment

* Add changelog entry for WithAttribute{s,Set}

* Remove number scope from measure opts

* Update changelog

* Remove left-over test cases

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
2023-04-18 07:16:06 -07:00

100 lines
2.7 KiB
Go

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"github.com/prometheus/client_golang/prometheus/promhttp"
"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"
)
const meterName = "github.com/open-telemetry/opentelemetry-go/example/view"
func main() {
ctx := context.Background()
// The exporter embeds a default OpenTelemetry Reader, allowing it to be used in WithReader.
exporter, err := otelprom.New()
if err != nil {
log.Fatal(err)
}
provider := metric.NewMeterProvider(
metric.WithReader(exporter),
// View to customize histogram buckets and rename a single histogram instrument.
metric.WithView(metric.NewView(
metric.Instrument{
Name: "custom_histogram",
Scope: instrumentation.Scope{Name: meterName},
},
metric.Stream{
Name: "bar",
Aggregation: aggregation.ExplicitBucketHistogram{
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()
opt := instrument.WithAttributes(
attribute.Key("A").String("B"),
attribute.Key("C").String("D"),
)
counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter"))
if err != nil {
log.Fatal(err)
}
counter.Add(ctx, 5, opt)
histogram, err := meter.Float64Histogram("custom_histogram", instrument.WithDescription("a histogram with custom buckets and rename"))
if err != nil {
log.Fatal(err)
}
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()
}
func serveMetrics() {
log.Printf("serving metrics at localhost:2222/metrics")
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(":2222", nil)
if err != nil {
fmt.Printf("error serving http: %v", err)
return
}
}