Skip to content

Commit

Permalink
Add ExampleNewView_attributeFilter and refine ExampleNewView_drop (#4527
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pellared authored Sep 19, 2023
1 parent a5190f6 commit be43b92
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions sdk/metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"regexp"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -167,13 +168,12 @@ func ExampleNewView() {
// unit: ms
}

func ExampleNewView_drop() {
// Create a view that sets the drop aggregator for all instrumentation from
// the "db" library, effectively turning-off all instrumentation from that
// library.
func ExampleNewView_wildcard() {
// Create a view that sets unit to milliseconds for any instrument with a
// name suffix of ".ms".
view := metric.NewView(
metric.Instrument{Scope: instrumentation.Scope{Name: "db"}},
metric.Stream{Aggregation: metric.AggregationDrop{}},
metric.Instrument{Name: "*.ms"},
metric.Stream{Unit: "ms"},
)

// The created view can then be registered with the OpenTelemetry metric
Expand All @@ -185,47 +185,55 @@ func ExampleNewView_drop() {
// Below is an example of how the view will
// function in the SDK for certain instruments.
stream, _ := view(metric.Instrument{
Name: "queries",
Kind: metric.InstrumentKindCounter,
Scope: instrumentation.Scope{Name: "db", Version: "v0.4.0"},
Name: "computation.time.ms",
Unit: "1",
})
fmt.Println("name:", stream.Name)
fmt.Printf("aggregation: %#v", stream.Aggregation)
fmt.Println("unit:", stream.Unit)
// Output:
// name: queries
// aggregation: metric.AggregationDrop{}
// name: computation.time.ms
// unit: ms
}

func ExampleNewView_wildcard() {
// Create a view that sets unit to milliseconds for any instrument with a
// name suffix of ".ms".
func ExampleNewView_drop() {
// Create a view that drops the "latency" instrument from the "http"
// instrumentation library.
view := metric.NewView(
metric.Instrument{Name: "*.ms"},
metric.Stream{Unit: "ms"},
metric.Instrument{
Name: "latency",
Scope: instrumentation.Scope{Name: "http"},
},
metric.Stream{Aggregation: metric.AggregationDrop{}},
)

// The created view can then be registered with the OpenTelemetry metric
// SDK using the WithView option.
_ = metric.NewMeterProvider(
metric.WithView(view),
)
}

// Below is an example of how the view will
// function in the SDK for certain instruments.
stream, _ := view(metric.Instrument{
Name: "computation.time.ms",
Unit: "1",
})
fmt.Println("name:", stream.Name)
fmt.Println("unit:", stream.Unit)
// Output:
// name: computation.time.ms
// unit: ms
func ExampleNewView_attributeFilter() {
// Create a view removes the "http.request.method" attribute recorded by
// the "latency" instrument from the "http" instrumentation library.
view := metric.NewView(
metric.Instrument{
Name: "latency",
Scope: instrumentation.Scope{Name: "http"},
},
metric.Stream{AttributeFilter: attribute.NewDenyKeysFilter("http.request.method")},
)

// The created view can then be registered with the OpenTelemetry metric
// SDK using the WithView option.
_ = metric.NewMeterProvider(
metric.WithView(view),
)
}

func ExampleNewView_exponentialHistogram() {
// Create a view that makes the "latency" instrument
// to be reported as an exponential histogram.
// Create a view that makes the "latency" instrument from the "http"
// instrumentation library to be reported as an exponential histogram.
view := metric.NewView(
metric.Instrument{
Name: "latency",
Expand Down

0 comments on commit be43b92

Please sign in to comment.