-
Notifications
You must be signed in to change notification settings - Fork 376
[Geneva] Log error when buffer too small #4027
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics.Tracing; | ||
| using System.Globalization; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
@@ -243,6 +244,19 @@ internal ExportResult Export(in Batch<Metric> batch) | |
| break; | ||
| } | ||
| } | ||
| catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException) | ||
| { | ||
| // The fixed-size serialization buffer's bounds were exceeded. | ||
| // Note: SerializeByte/SerializeUInt16/etc. throw IndexOutOfRangeException when | ||
| // the buffer index exceeds the array length; Encoding.UTF8.GetBytes throws | ||
| // ArgumentException on .NET 5+ when the destination span is too small. | ||
| if (ExporterEventSource.Log.IsEnabled(EventLevel.Error, EventKeywords.All)) | ||
| { | ||
| ExporterEventSource.Log.MetricSerializationBufferFull(metric.Name, GenevaMetricExporter.BufferSize); | ||
| } | ||
|
|
||
| result = ExportResult.Failure; | ||
| } | ||
|
Comment on lines
+247
to
+259
|
||
| catch (Exception ex) | ||
| { | ||
| ExporterEventSource.Log.FailedToSendMetricData(monitoringAccount, metricNamespace, metric.Name, ex); // TODO: preallocate exception or no exception | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
| using System.Diagnostics.Tracing; | ||
| using System.Globalization; | ||
| using System.Net.Sockets; | ||
| using System.Reflection; | ||
|
|
@@ -1411,5 +1412,82 @@ private static UserdataV2 GetSerializedData(Metric metric, TlvMetricExporter exp | |
|
|
||
| return result; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TlvMetricExporter_BufferOverflow_LogsBufferFullEvent() | ||
| { | ||
| var capturedEvents = new List<EventWrittenEventArgs>(); | ||
| var path = string.Empty; | ||
| Socket server = null; | ||
|
|
||
| using var listener = new BufferOverflowEventListener(capturedEvents); | ||
| try | ||
|
Comment on lines
+1419
to
+1424
|
||
| { | ||
| string connectionString; | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| connectionString = "Account=OTelMonitoringAccount;Namespace=OTelMetricNamespace"; | ||
| } | ||
| else | ||
| { | ||
| path = GenerateTempFilePath(); | ||
| connectionString = $"Endpoint=unix:{path};Account=OTelMonitoringAccount;Namespace=OTelMetricNamespace"; | ||
| var endpoint = new UnixDomainSocketEndPoint(path); | ||
| server = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); | ||
| server.Bind(endpoint); | ||
| server.Listen(1); | ||
| } | ||
|
|
||
| using var meter = new Meter("BufferOverflowTest", "0.0.1"); | ||
| var counter = meter.CreateCounter<long>("overflowCounter"); | ||
|
|
||
| using (var meterProvider = Sdk.CreateMeterProviderBuilder() | ||
| .AddMeter("BufferOverflowTest") | ||
| .AddGenevaMetricExporter(options => options.ConnectionString = connectionString) | ||
| .Build()) | ||
| { | ||
| // Record a metric with a tag value large enough to overflow the fixed-size serialization buffer. | ||
| counter.Add(1, new KeyValuePair<string, object>("bigTag", new string('x', GenevaMetricExporter.BufferSize + 100))); | ||
| } | ||
|
|
||
| // Verify that the buffer overflow was logged with the dedicated event (ID 11). | ||
| Assert.Contains(capturedEvents, e => e.EventId == 11); | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| finally | ||
| { | ||
| server?.Dispose(); | ||
| if (!string.IsNullOrEmpty(path)) | ||
| { | ||
| try | ||
| { | ||
| File.Delete(path); | ||
| } | ||
| catch | ||
| { | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private sealed class BufferOverflowEventListener : EventListener | ||
| { | ||
| private readonly List<EventWrittenEventArgs> capturedEvents; | ||
|
|
||
| public BufferOverflowEventListener(List<EventWrittenEventArgs> capturedEvents) | ||
| { | ||
| this.capturedEvents = capturedEvents; | ||
| } | ||
|
|
||
| protected override void OnEventSourceCreated(EventSource eventSource) | ||
| { | ||
| if (eventSource.Name == "OpenTelemetry-Exporter-Geneva") | ||
| { | ||
| this.EnableEvents(eventSource, EventLevel.Error, EventKeywords.All); | ||
| } | ||
| } | ||
|
|
||
| protected override void OnEventWritten(EventWrittenEventArgs eventData) | ||
| => this.capturedEvents.Add(eventData); | ||
| } | ||
| } | ||
| #pragma warning restore CA1861 // // Prefer 'static readonly' fields over constant array arguments if the called method is called repeatedly and is not mutating the passed array | ||
Uh oh!
There was an error while loading. Please reload this page.