-
Notifications
You must be signed in to change notification settings - Fork 847
[OTLP] Use packed format for metric histograms #6567
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
Open
martincostello
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
martincostello:gh-6538
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
*.sh eol=lf | ||
|
||
# Verify settings | ||
*.verified.txt text eol=lf working-tree-encoding=UTF-8 | ||
*.verified.xml text eol=lf working-tree-encoding=UTF-8 | ||
*.verified.json text eol=lf working-tree-encoding=UTF-8 | ||
*.verified.bin binary |
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 |
---|---|---|
|
@@ -355,3 +355,6 @@ rewrite.coyote.json | |
|
||
# Test results | ||
TestResults/ | ||
|
||
# Verify snapshots | ||
*.received.* |
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
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
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
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
124 changes: 124 additions & 0 deletions
124
...penTelemetryProtocol.Tests/Implementation/Serializer/ProtobufOtlpMetricSerializerTests.cs
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,124 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Diagnostics.Metrics; | ||
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; | ||
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Tests; | ||
|
||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.Implementation.Serializer; | ||
|
||
public static class ProtobufOtlpMetricSerializerTests | ||
{ | ||
[Fact] | ||
public static async Task WriteMetricsData_Serializes_Metrics_Correctly() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've verified manually that if this test is added to |
||
{ | ||
// Arrange | ||
var metrics = GenerateMetrics(); | ||
|
||
var attributes = new Dictionary<string, object> | ||
{ | ||
{ "service.name", "OpenTelemetry-DotNet" }, | ||
{ "service.version", "1.2.3" }, | ||
}; | ||
|
||
var buffer = new byte[1024]; | ||
var writePosition = 0; | ||
var resource = new Resource(attributes); | ||
|
||
// Act | ||
var actual = ProtobufOtlpMetricSerializer.WriteMetricsData( | ||
ref buffer, | ||
writePosition, | ||
resource, | ||
metrics); | ||
|
||
// Assert | ||
Assert.NotEqual(0, actual); | ||
Assert.True(actual > writePosition, $"The returned write position, {actual} is not greater than the initial write position, {writePosition}."); | ||
Assert.True(actual <= buffer.Length, $"The returned write position, {actual} is beyond the bounds of the buffer, {buffer.Length}."); | ||
|
||
using var stream = new MemoryStream(); | ||
|
||
#if NET | ||
await stream.WriteAsync(buffer.AsMemory(0, actual)); | ||
#else | ||
await stream.WriteAsync(buffer, 0, actual); | ||
#endif | ||
|
||
await Verify(stream, "bin") | ||
.IgnoreParametersForVerified() | ||
.UseDirectory("snapshots"); | ||
} | ||
|
||
private static Batch<Metric> GenerateMetrics() | ||
{ | ||
// Arrange | ||
Batch<Metric> metrics = default; | ||
|
||
// Create some metrics to export | ||
using (var exported = new ManualResetEvent(false)) | ||
{ | ||
var experimentalOptions = new ExperimentalOptions(); | ||
var exporterOptions = new OtlpExporterOptions() | ||
{ | ||
Endpoint = new($"http://localhost:4318/v1/"), | ||
Protocol = OtlpExportProtocol.HttpProtobuf, | ||
}; | ||
|
||
using var exporter = new DelegatingExporter<Metric>() | ||
{ | ||
OnExportFunc = (batch) => | ||
{ | ||
metrics = batch; | ||
exported.Set(); | ||
return ExportResult.Success; | ||
}, | ||
}; | ||
|
||
var meterName = "otlp.protobuf.serialization"; | ||
|
||
var builder = Sdk.CreateMeterProviderBuilder().AddMeter(meterName); | ||
|
||
var metricReaderOptions = new MetricReaderOptions(); | ||
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = Timeout.Infinite; | ||
|
||
builder.AddReader( | ||
(serviceProvider) => OtlpMetricExporterExtensions.BuildOtlpExporterMetricReader( | ||
serviceProvider, | ||
exporterOptions, | ||
metricReaderOptions, | ||
experimentalOptions, | ||
configureExporterInstance: (_) => exporter)); | ||
|
||
using var meterProvider = builder.Build(); | ||
using var meter = new Meter(meterName); | ||
|
||
var counter = meter.CreateCounter<int>("counter"); | ||
counter.Add(18); | ||
|
||
var gauge = meter.CreateGauge<int>("gauge"); | ||
gauge.Record(42); | ||
|
||
var histogram = meter.CreateHistogram<int>("histogram"); | ||
histogram.Record(100); | ||
|
||
Assert.True(meterProvider.ForceFlush()); | ||
|
||
Assert.NotEqual(0, metrics.Count); | ||
} | ||
|
||
// Scrub the timestamps for stable snapshots | ||
var startTime = new DateTimeOffset(2025, 10, 08, 10, 20, 11, TimeSpan.Zero); | ||
var endTime = startTime.AddSeconds(10); | ||
|
||
foreach (var metric in metrics) | ||
{ | ||
metric.AggregatorStore.OverrideTimeRange(startTime, endTime); | ||
} | ||
|
||
return metrics; | ||
} | ||
} |
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
Binary file added
BIN
+576 Bytes
...tobufOtlpMetricSerializerTests.WriteMetricsData_Serializes_Metrics_Correctly.verified.bin
Binary file not shown.
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
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
2 changes: 0 additions & 2 deletions
2
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExportProtocolParserTests.cs
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any option to make this calls by the reflection?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that if desired, but IMHO that's a bit yuck if the tests already have
[InternalsVisibleTo]
. Or alternatively I could just make the settersinternal
instead ofprviate
.