-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add the sdk/metric/internal/exemplar package
#4835
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c4e769
Add the sdk/metric/internal/exemplar pkg
MrAlias 504ff47
Rename testReservoir to ReservoirTest
MrAlias df0b497
Run go mod tidy
MrAlias 01852f8
Merge branch 'main' into exemplar-pkg
MrAlias 0da68d1
Merge branch 'main' into exemplar-pkg
MrAlias b4f8613
Merge branch 'main' into exemplar-pkg
MrAlias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // 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 exemplar provides an implementation of the OpenTelemetry exemplar | ||
| // reservoir to be used in metric collection pipelines. | ||
| package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // 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 exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar" | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| ) | ||
|
|
||
| // Reservoir holds the sampled exemplar of measurements made. | ||
| type Reservoir[N int64 | float64] interface { | ||
| // Offer accepts the parameters associated with a measurement. The | ||
| // parameters will be stored as an exemplar if the Reservoir decides to | ||
| // sample the measurement. | ||
| // | ||
| // The passed ctx needs to contain any baggage or span that were active | ||
| // when the measurement was made. This information may be used by the | ||
| // Reservoir in making a sampling decision. | ||
| // | ||
| // The time t is the time when the measurement was made. The val and attr | ||
| // parameters are the value and dropped (filtered) attributes of the | ||
| // measurement respectively. | ||
| Offer(ctx context.Context, t time.Time, val N, attr []attribute.KeyValue) | ||
|
|
||
| // Collect returns all the held exemplars. | ||
| // | ||
| // The Reservoir state is preserved after this call. See Flush to | ||
| // copy-and-clear instead. | ||
| Collect(dest *[]metricdata.Exemplar[N]) | ||
dashpole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Flush returns all the held exemplars. | ||
| // | ||
| // The Reservoir state is reset after this call. See Collect to preserve | ||
| // the state instead. | ||
| Flush(dest *[]metricdata.Exemplar[N]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // 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 exemplar | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| // Sat Jan 01 2000 00:00:00 GMT+0000. | ||
| var staticTime = time.Unix(946684800, 0) | ||
|
|
||
| type factory[N int64 | float64] func(requstedCap int) (r Reservoir[N], actualCap int) | ||
|
|
||
| func ReservoirTest[N int64 | float64](f factory[N]) func(*testing.T) { | ||
dashpole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| t.Run("CaptureSpanContext", func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| r, n := f(1) | ||
| if n < 1 { | ||
| t.Skip("skipping, reservoir capacity less than 1:", n) | ||
| } | ||
|
|
||
| tID, sID := trace.TraceID{0x01}, trace.SpanID{0x01} | ||
| sc := trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: tID, | ||
| SpanID: sID, | ||
| TraceFlags: trace.FlagsSampled, | ||
| }) | ||
| ctx := trace.ContextWithSpanContext(ctx, sc) | ||
|
|
||
| r.Offer(ctx, staticTime, 10, nil) | ||
|
|
||
| var dest []metricdata.Exemplar[N] | ||
| r.Collect(&dest) | ||
|
|
||
| want := metricdata.Exemplar[N]{ | ||
| Time: staticTime, | ||
| Value: 10, | ||
| SpanID: []byte(sID[:]), | ||
| TraceID: []byte(tID[:]), | ||
| } | ||
| require.Len(t, dest, 1, "number of collected exemplars") | ||
| assert.Equal(t, want, dest[0]) | ||
| }) | ||
|
|
||
| t.Run("FilterAttributes", func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| r, n := f(1) | ||
| if n < 1 { | ||
| t.Skip("skipping, reservoir capacity less than 1:", n) | ||
| } | ||
|
|
||
| adminTrue := attribute.Bool("admin", true) | ||
| r.Offer(ctx, staticTime, 10, []attribute.KeyValue{adminTrue}) | ||
|
|
||
| var dest []metricdata.Exemplar[N] | ||
| r.Collect(&dest) | ||
|
|
||
| want := metricdata.Exemplar[N]{ | ||
| FilteredAttributes: []attribute.KeyValue{adminTrue}, | ||
| Time: staticTime, | ||
| Value: 10, | ||
| } | ||
| require.Len(t, dest, 1, "number of collected exemplars") | ||
| assert.Equal(t, want, dest[0]) | ||
| }) | ||
|
|
||
| t.Run("CollectDoesNotFlush", func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| r, n := f(1) | ||
| if n < 1 { | ||
| t.Skip("skipping, reservoir capacity less than 1:", n) | ||
| } | ||
|
|
||
| r.Offer(ctx, staticTime, 10, nil) | ||
|
|
||
| var dest []metricdata.Exemplar[N] | ||
| r.Collect(&dest) | ||
| require.Len(t, dest, 1, "number of collected exemplars") | ||
|
|
||
| dest = dest[:0] | ||
| r.Collect(&dest) | ||
| assert.Len(t, dest, 1, "Collect flushed reservoir") | ||
| }) | ||
|
|
||
| t.Run("FlushFlushes", func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| r, n := f(1) | ||
| if n < 1 { | ||
| t.Skip("skipping, reservoir capacity less than 1:", n) | ||
| } | ||
|
|
||
| r.Offer(ctx, staticTime, 10, nil) | ||
|
|
||
| var dest []metricdata.Exemplar[N] | ||
| r.Flush(&dest) | ||
| require.Len(t, dest, 1, "number of flushed exemplars") | ||
|
|
||
| r.Flush(&dest) | ||
| assert.Len(t, dest, 0, "Flush did not flush reservoir") | ||
| }) | ||
|
|
||
| t.Run("MultipleOffers", func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| r, n := f(3) | ||
| if n < 1 { | ||
| t.Skip("skipping, reservoir capacity less than 1:", n) | ||
| } | ||
|
|
||
| for i := 0; i < n+1; i++ { | ||
| v := N(i) | ||
| r.Offer(ctx, staticTime, v, nil) | ||
| } | ||
|
|
||
| var dest []metricdata.Exemplar[N] | ||
| r.Flush(&dest) | ||
| assert.Len(t, dest, n, "multiple offers did not fill reservoir") | ||
|
|
||
| // Ensure the flush reset also resets any couting state. | ||
| for i := 0; i < n+1; i++ { | ||
| v := N(2 * i) | ||
| r.Offer(ctx, staticTime, v, nil) | ||
| } | ||
|
|
||
| dest = dest[:0] | ||
| r.Flush(&dest) | ||
| assert.Len(t, dest, n, "internal count state not reset") | ||
| }) | ||
|
|
||
| t.Run("DropAll", func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| r, n := f(0) | ||
| if n > 0 { | ||
| t.Skip("skipping, reservoir capacity greater than 0:", n) | ||
| } | ||
|
|
||
| r.Offer(context.Background(), staticTime, 10, nil) | ||
|
|
||
| dest := []metricdata.Exemplar[N]{{}} // Should be reset to empty. | ||
| r.Collect(&dest) | ||
| assert.Len(t, dest, 0, "no exemplars should be collected") | ||
|
|
||
| r.Offer(context.Background(), staticTime, 10, nil) | ||
| dest = []metricdata.Exemplar[N]{{}} // Should be reset to empty. | ||
| r.Flush(&dest) | ||
| assert.Len(t, dest, 0, "no exemplars should be flushed") | ||
| }) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.