-
Notifications
You must be signed in to change notification settings - Fork 2
metrics: per-session download goodput histogram #674
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
+184
−8
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package instrument | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| sdkotel "go.opentelemetry.io/otel" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
|
|
||
| "github.com/getlantern/geo" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // newGoodputInstrument wires a manual-reader meter provider into the global | ||
| // otel state and returns a defaultInstrument plus the reader to collect from. | ||
| func newGoodputInstrument(t *testing.T) (*defaultInstrument, *sdkmetric.ManualReader) { | ||
| t.Helper() | ||
| // Restore the global meter provider after the test so the manual-reader | ||
| // provider doesn't leak into other tests in the process. | ||
| prev := sdkotel.GetMeterProvider() | ||
| t.Cleanup(func() { sdkotel.SetMeterProvider(prev) }) | ||
|
|
||
| reader := sdkmetric.NewManualReader() | ||
| provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) | ||
| sdkotel.SetMeterProvider(provider) | ||
|
|
||
| ins, err := NewDefault(geo.NoLookup{}, &mockISPLookup{}, "test-proxy") | ||
| require.NoError(t, err) | ||
| return ins, reader | ||
| } | ||
|
|
||
| // TestSessionGoodput verifies the per-session download goodput histogram is | ||
| // recorded once for a session that moved >= goodputMinBytes, with the value | ||
| // ~= received bytes / connection seconds and a receive direction tag. | ||
| func TestSessionGoodput(t *testing.T) { | ||
| ins, reader := newGoodputInstrument(t) | ||
|
|
||
| const recv = 1_100_000 // above the 1MB goodput threshold | ||
| ins.SessionGoodput(context.Background(), recv, time.Second, net.ParseIP("1.2.3.4")) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| require.NoError(t, reader.Collect(context.Background(), &rm)) | ||
|
|
||
| count, sum, found := histogramCountSum(rm, "proxy.session.goodput") | ||
| require.True(t, found, "goodput histogram should be emitted for a >=1MB session") | ||
| assert.Equal(t, uint64(1), count, "exactly one goodput sample") | ||
| // 1s open duration → goodput ~= received bytes per second. | ||
| assert.InDelta(t, float64(recv), sum, float64(recv)*0.01) | ||
|
|
||
| attrs := extractHistogramAttrs(rm, "proxy.session.goodput") | ||
| assert.Equal(t, "receive", attrs["network.io.direction"]) | ||
| // The country point attribute must always be present (empty here, since the | ||
| // test uses geo.NoLookup) so the metric stays sliceable by country. | ||
| _, hasCountry := attrs["geo.country.iso_code"] | ||
| assert.True(t, hasCountry, "goodput sample should carry the geo.country.iso_code attribute") | ||
| } | ||
|
|
||
| // TestSessionGoodputBelowThreshold verifies a sub-threshold session records no | ||
| // goodput sample. | ||
| func TestSessionGoodputBelowThreshold(t *testing.T) { | ||
| ins, reader := newGoodputInstrument(t) | ||
|
|
||
| ins.SessionGoodput(context.Background(), 42, time.Second, net.ParseIP("1.2.3.4")) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| require.NoError(t, reader.Collect(context.Background(), &rm)) | ||
|
|
||
| _, _, found := histogramCountSum(rm, "proxy.session.goodput") | ||
| assert.False(t, found, "no goodput sample below the byte threshold") | ||
| } | ||
|
|
||
| // TestSessionGoodputZeroDuration verifies a non-positive duration records no | ||
| // sample (guards against divide-by-zero). | ||
| func TestSessionGoodputZeroDuration(t *testing.T) { | ||
| ins, reader := newGoodputInstrument(t) | ||
|
|
||
| ins.SessionGoodput(context.Background(), 2_000_000, 0, net.ParseIP("1.2.3.4")) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| require.NoError(t, reader.Collect(context.Background(), &rm)) | ||
|
|
||
| _, _, found := histogramCountSum(rm, "proxy.session.goodput") | ||
| assert.False(t, found, "no goodput sample for a zero-duration session") | ||
| } | ||
|
|
||
| func histogramCountSum(rm metricdata.ResourceMetrics, name string) (uint64, float64, bool) { | ||
| for _, sm := range rm.ScopeMetrics { | ||
| for _, m := range sm.Metrics { | ||
| if m.Name != name { | ||
| continue | ||
| } | ||
| if d, ok := m.Data.(metricdata.Histogram[float64]); ok && len(d.DataPoints) > 0 { | ||
| return d.DataPoints[0].Count, d.DataPoints[0].Sum, true | ||
| } | ||
| } | ||
| } | ||
| return 0, 0, false | ||
| } | ||
|
|
||
| func extractHistogramAttrs(rm metricdata.ResourceMetrics, name string) map[string]string { | ||
| result := make(map[string]string) | ||
| for _, sm := range rm.ScopeMetrics { | ||
| for _, m := range sm.Metrics { | ||
| if m.Name != name { | ||
| continue | ||
| } | ||
| if d, ok := m.Data.(metricdata.Histogram[float64]); ok && len(d.DataPoints) > 0 { | ||
| for _, kv := range d.DataPoints[0].Attributes.ToSlice() { | ||
| result[string(kv.Key)] = kv.Value.Emit() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result | ||
| } | ||
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
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
Oops, something went wrong.
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.