Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add view example tests #3460

Merged
merged 25 commits into from Nov 21, 2022
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5aea817
Add the InstrumentKind type and vars to sdk/metric
MrAlias Nov 10, 2022
5b5d7e4
Add the Instrument type to sdk/metric
MrAlias Nov 10, 2022
80061df
Add the Stream type to sdk/metric
MrAlias Nov 10, 2022
c772a39
Add the View type to sdk/metric
MrAlias Nov 10, 2022
791245a
Add NewView to create Views matching OTel spec
MrAlias Nov 10, 2022
8e591c5
Add unit tests for NewView
MrAlias Nov 10, 2022
dd3512d
Add changes to changelog
MrAlias Nov 10, 2022
b33ebaf
Apply suggestions from code review
MrAlias Nov 11, 2022
9519abd
Update CHANGELOG.md
MrAlias Nov 11, 2022
e2d716c
Merge branch 'main' into metric-view
MrAlias Nov 11, 2022
f7f7d0e
Update match and mask comments of Instrument
MrAlias Nov 11, 2022
25b9636
Merge branch 'main' into metric-view
MrAlias Nov 11, 2022
0e46a90
Merge branch 'main' into metric-view
MrAlias Nov 11, 2022
623525e
Explain wildcard logic in NewView with comment
MrAlias Nov 11, 2022
56ef349
Drop views that replace name for multi-inst match
MrAlias Nov 11, 2022
a350588
Comment how users are expected to match zero-vals
MrAlias Nov 11, 2022
3bdcf8a
Remove InstrumentKind and Scope from Stream
MrAlias Nov 11, 2022
f2c47b9
Fix redundant word in NewView comment
MrAlias Nov 11, 2022
6d4746b
Add view example tests
MrAlias Nov 11, 2022
9b7440c
Merge branch 'main' into view-example-tests
MrAlias Nov 16, 2022
6237da7
Merge branch 'main' into view-example-tests
MrAlias Nov 16, 2022
cfde868
Merge branch 'main' into view-example-tests
MrAlias Nov 19, 2022
8d88439
Update comments to examples
MrAlias Nov 19, 2022
99db340
Merge branch 'main' into view-example-tests
MrAlias Nov 21, 2022
e694e34
Fix broken English
MrAlias Nov 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 133 additions & 0 deletions sdk/metric/view_test.go
Expand Up @@ -15,6 +15,8 @@
package metric // import "go.opentelemetry.io/otel/sdk/metric"

import (
"fmt"
"regexp"
"testing"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -469,3 +471,134 @@ func TestNewViewAggregationErrorLogged(t *testing.T) {
assert.Nil(t, got.Aggregation, "erroring aggregation used")
assert.Equal(t, 1, l.ErrorN())
}

func ExampleNewView() {
// Create a view that renames the "latency" instrument from the v0.34.0
// version of the "http" instrumentation library as "request.latency".
view := NewView(Instrument{
Name: "latency",
Scope: instrumentation.Scope{
Name: "http",
Version: "v0.34.0",
},
}, Stream{Name: "request.latency"})

// The created view can then be registered with the OpenTelemetry metric
// SDK using the WithView option. Below is an example of how the view
// function in the SDK for certain instruments.

stream, _ := view(Instrument{
Name: "latency",
Description: "request latency",
Unit: unit.Milliseconds,
Kind: InstrumentKindSyncCounter,
Scope: instrumentation.Scope{
Name: "http",
Version: "v0.34.0",
SchemaURL: "https://opentelemetry.io/schemas/1.0.0",
},
})
fmt.Println("name:", stream.Name)
fmt.Println("description:", stream.Description)
fmt.Println("unit:", stream.Unit)
// Output:
// name: request.latency
// description: request latency
// 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.
view := NewView(
Instrument{Scope: instrumentation.Scope{Name: "db"}},
Stream{Aggregation: aggregation.Drop{}},
)

// The created view can then be registered with the OpenTelemetry metric
// SDK using the WithView option. Below is an example of how the view
// function in the SDK for certain instruments.

stream, _ := view(Instrument{
Name: "queries",
Kind: InstrumentKindSyncCounter,
Scope: instrumentation.Scope{Name: "db", Version: "v0.4.0"},
})
fmt.Println("name:", stream.Name)
fmt.Printf("aggregation: %#v", stream.Aggregation)
// Output:
// name: queries
// aggregation: aggregation.Drop{}
}

func ExampleNewView_wildcard() {
// Create a view that sets unit to milliseconds for any instrument with a
// name suffix of ".ms".
view := NewView(
Instrument{Name: "*.ms"},
Stream{Unit: unit.Milliseconds},
)

// The created view can then be registered with the OpenTelemetry metric
// SDK using the WithView option. Below is an example of how the view
// function in the SDK for certain instruments.

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

func ExampleView() {
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
// The NewView function provides convenient creation of common Views
// construction. However, it is limited in what it can create.
//
// When NewView is not able to provide the functionally needed, a custom
// View can be constructed directly. Here a custom View is constructed that
// uses Go's regular expression matching to ensure all data stream names
// have a suffix of the units it uses.

re := regexp.MustCompile(`[._](ms|byte)$`)
var view View = func(i Instrument) (Stream, bool) {
s := Stream{Name: i.Name, Description: i.Description, Unit: i.Unit}
// 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 s, false
}
switch i.Unit {
case unit.Milliseconds:
s.Name += ".ms"
case unit.Bytes:
s.Name += ".byte"
default:
return s, false
}
return s, true
}

// The created view can then be registered with the OpenTelemetry metric
// SDK using the WithView option. Below is an example of how the view
// function in the SDK for certain instruments.

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
}