Skip to content

Commit

Permalink
Add View example
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Oct 26, 2022
1 parent f061ec4 commit 45b1008
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions sdk/metric/view_test.go
Expand Up @@ -16,6 +16,7 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric"

import (
"fmt"
"regexp"
"testing"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -822,3 +823,38 @@ func ExampleNewView_wildcard() {
// name: computation.time.ms
// unit: ms
}

func ExampleView() {
re := regexp.MustCompile(`[._](ms|byte)$`)
var view View = func(i Instrument) (Stream, bool) {
// Any instrument that does not have a unit suffix defined, but has a
// dimensional unit defined, update the name with a unit suffix.
if re.MatchString(i.Name) {
return Stream{Instrument: i}, false
}
switch i.Unit {
case unit.Milliseconds:
i.Name += ".ms"
case unit.Bytes:
i.Name += ".byte"
default:
return Stream{Instrument: i}, false
}
return Stream{Instrument: i}, true
}

stream, _ := view(Instrument{
Name: "computation.time.ms",
Unit: unit.Milliseconds,
})
fmt.Println("name:", stream.Name)

stream, _ = view(Instrument{
Name: "heap.size",
Unit: unit.Bytes,
})
fmt.Println("name:", stream.Name)
// Output:
// name: computation.time.ms
// name: heap.size.byte
}

0 comments on commit 45b1008

Please sign in to comment.