From 0c775e58fa8eb55468f321d5612b527ed80afb9f Mon Sep 17 00:00:00 2001 From: Timothy Mothra Date: Tue, 3 Dec 2024 13:57:57 -0800 Subject: [PATCH] [otlp] refactor constants (#6010) --- .../Implementation/Serializer/.editorconfig | 4 ++ .../ProtobufOtlpCommonFieldNumberConstants.cs | 32 +++++++++++++ .../ProtobufOtlpLogFieldNumberConstants.cs | 29 ++--------- .../Serializer/ProtobufOtlpLogSerializer.cs | 4 +- .../ProtobufOtlpMetricFieldNumberConstants.cs | 27 ----------- .../ProtobufOtlpMetricSerializer.cs | 8 ++-- .../Serializer/ProtobufOtlpTagWriter.cs | 48 +++++++++---------- .../ProtobufOtlpTraceFieldNumberConstants.cs | 28 ++--------- .../Serializer/ProtobufOtlpTraceSerializer.cs | 10 ++-- 9 files changed, 79 insertions(+), 111 deletions(-) create mode 100644 src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/.editorconfig create mode 100644 src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpCommonFieldNumberConstants.cs diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/.editorconfig b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/.editorconfig new file mode 100644 index 00000000000..822889f9eaa --- /dev/null +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/.editorconfig @@ -0,0 +1,4 @@ +[*Constants.cs] +# SA1310: Field names should not contain underscore +# Justification: These names describe the nested names and properties in the .Proto file. +dotnet_diagnostic.SA1310.severity = none diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpCommonFieldNumberConstants.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpCommonFieldNumberConstants.cs new file mode 100644 index 00000000000..4d220d598e8 --- /dev/null +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpCommonFieldNumberConstants.cs @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer; + +/// +/// Defines field number constants for fields defined in +/// . +/// +internal static class ProtobufOtlpCommonFieldNumberConstants +{ + // InstrumentationScope + internal const int InstrumentationScope_Name = 1; + internal const int InstrumentationScope_Version = 2; + internal const int InstrumentationScope_Attributes = 3; + internal const int InstrumentationScope_Dropped_Attributes_Count = 4; + + // KeyValue + internal const int KeyValue_Key = 1; + internal const int KeyValue_Value = 2; + + // AnyValue + internal const int AnyValue_String_Value = 1; + internal const int AnyValue_Bool_Value = 2; + internal const int AnyValue_Int_Value = 3; + internal const int AnyValue_Double_Value = 4; + internal const int AnyValue_Array_Value = 5; + internal const int AnyValue_Kvlist_Value = 6; + internal const int AnyValue_Bytes_Value = 7; + + internal const int ArrayValue_Value = 1; +} diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogFieldNumberConstants.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogFieldNumberConstants.cs index 1ff402d6b1c..f7c97f6e222 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogFieldNumberConstants.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogFieldNumberConstants.cs @@ -3,10 +3,12 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer; +/// +/// Defines field number constants for fields defined in +/// . +/// internal static class ProtobufOtlpLogFieldNumberConstants { -#pragma warning disable SA1310 // Field names should not contain underscore - // Logs data internal const int LogsData_Resource_Logs = 1; @@ -63,30 +65,7 @@ internal static class ProtobufOtlpLogFieldNumberConstants internal const int Severity_Number_Fatal4 = 24; // LogRecordFlags - internal const int LogRecord_Flags_Do_Not_Use = 0; internal const int LogRecord_Flags_Trace_Flags_Mask = 0x000000FF; - - // InstrumentationScope - internal const int InstrumentationScope_Name = 1; - internal const int InstrumentationScope_Version = 2; - internal const int InstrumentationScope_Attributes = 3; - internal const int InstrumentationScope_Dropped_Attributes_Count = 4; - - // KeyValue - internal const int KeyValue_Key = 1; - internal const int KeyValue_Value = 2; - - // AnyValue - internal const int AnyValue_String_Value = 1; - internal const int AnyValue_Bool_Value = 2; - internal const int AnyValue_Int_Value = 3; - internal const int AnyValue_Double_Value = 4; - internal const int AnyValue_Array_Value = 5; - internal const int AnyValue_Kvlist_Value = 6; - internal const int AnyValue_Bytes_Value = 7; - - internal const int ArrayValue_Value = 1; -#pragma warning restore SA1310 // Field names should not contain underscore } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogSerializer.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogSerializer.cs index f2e9895a6a9..0f8acfab5a2 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogSerializer.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogSerializer.cs @@ -113,7 +113,7 @@ internal static int WriteScopeLog(byte[] buffer, int writePosition, SdkLimitOpti // numberOfUtf8CharsInString + tagSize + length field size. writePosition = ProtobufSerializer.WriteTagAndLength(buffer, writePosition, numberOfUtf8CharsInString + 1 + serializedLengthSize, ProtobufOtlpLogFieldNumberConstants.ScopeLogs_Scope, ProtobufWireType.LEN); - writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpLogFieldNumberConstants.InstrumentationScope_Name, numberOfUtf8CharsInString, value); + writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Name, numberOfUtf8CharsInString, value); for (int i = 0; i < logRecords.Count; i++) { @@ -279,7 +279,7 @@ private static int WriteLogRecordBody(byte[] buffer, int writePosition, ReadOnly // length = numberOfUtf8CharsInString + tagSize + length field size. writePosition = ProtobufSerializer.WriteTagAndLength(buffer, writePosition, numberOfUtf8CharsInString + 1 + serializedLengthSize, ProtobufOtlpLogFieldNumberConstants.LogRecord_Body, ProtobufWireType.LEN); - writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_String_Value, numberOfUtf8CharsInString, value); + writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_String_Value, numberOfUtf8CharsInString, value); return writePosition; } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricFieldNumberConstants.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricFieldNumberConstants.cs index 49612492f3f..a416f6a985f 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricFieldNumberConstants.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricFieldNumberConstants.cs @@ -3,13 +3,10 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer; -using System.Diagnostics.CodeAnalysis; - /// /// Defines field number constants for fields defined in /// . /// -[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "These names describe the nested names and properties in the .Proto file.")] internal static class ProtobufOtlpMetricFieldNumberConstants { // Metrics Data @@ -126,28 +123,4 @@ internal static class ProtobufOtlpMetricFieldNumberConstants internal const int Exemplar_Value_As_Int = 6; internal const int Exemplar_Span_Id = 4; internal const int Exemplar_Trace_Id = 5; - - // ---------- COMMON FIELDS BELOW THIS LINE ---------- - // TODO: THESE SHOULD BE MOVED TO A SEPARATE FILE. These are currently copied in each Constants file. - - // InstrumentationScope - internal const int InstrumentationScope_Name = 1; - internal const int InstrumentationScope_Version = 2; - internal const int InstrumentationScope_Attributes = 3; - internal const int InstrumentationScope_Dropped_Attributes_Count = 4; - - // KeyValue - internal const int KeyValue_Key = 1; - internal const int KeyValue_Value = 2; - - // AnyValue - internal const int AnyValue_String_Value = 1; - internal const int AnyValue_Bool_Value = 2; - internal const int AnyValue_Int_Value = 3; - internal const int AnyValue_Double_Value = 4; - internal const int AnyValue_Array_Value = 5; - internal const int AnyValue_Kvlist_Value = 6; - internal const int AnyValue_Bytes_Value = 7; - - internal const int ArrayValue_Value = 1; } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricSerializer.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricSerializer.cs index e729539d802..a16f4bf66c5 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricSerializer.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricSerializer.cs @@ -93,10 +93,10 @@ private static int WriteScopeMetric(byte[] buffer, int writePosition, string met var meterVersion = metrics[0].MeterVersion; var meterTags = metrics[0].MeterTags; - writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpMetricFieldNumberConstants.InstrumentationScope_Name, meterName); + writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Name, meterName); if (meterVersion != null) { - writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpMetricFieldNumberConstants.InstrumentationScope_Version, meterVersion); + writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Version, meterVersion); } if (meterTags != null) @@ -105,14 +105,14 @@ private static int WriteScopeMetric(byte[] buffer, int writePosition, string met { for (int i = 0; i < readonlyMeterTags.Count; i++) { - writePosition = WriteTag(buffer, writePosition, readonlyMeterTags[i], ProtobufOtlpMetricFieldNumberConstants.InstrumentationScope_Attributes); + writePosition = WriteTag(buffer, writePosition, readonlyMeterTags[i], ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Attributes); } } else { foreach (var tag in meterTags) { - writePosition = WriteTag(buffer, writePosition, tag, ProtobufOtlpMetricFieldNumberConstants.InstrumentationScope_Attributes); + writePosition = WriteTag(buffer, writePosition, tag, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Attributes); } } } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTagWriter.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTagWriter.cs index 79b3213a1cb..14643dc6676 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTagWriter.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTagWriter.cs @@ -17,60 +17,60 @@ private ProtobufOtlpTagWriter() protected override void WriteIntegralTag(ref OtlpTagWriterState state, string key, long value) { // Write KeyValue tag - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Key, key); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Key, key); // Write KeyValue.Value tag, length and value. var size = ProtobufSerializer.ComputeVarInt64Size((ulong)value) + 1; // ComputeVarint64Size(ulong) + TagSize - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, size, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); - state.WritePosition = ProtobufSerializer.WriteInt64WithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Int_Value, (ulong)value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, size, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteInt64WithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Int_Value, (ulong)value); } protected override void WriteFloatingPointTag(ref OtlpTagWriterState state, string key, double value) { // Write KeyValue tag - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Key, key); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Key, key); // Write KeyValue.Value tag, length and value. - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 9, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); // 8 + TagSize - state.WritePosition = ProtobufSerializer.WriteDoubleWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Double_Value, value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 9, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); // 8 + TagSize + state.WritePosition = ProtobufSerializer.WriteDoubleWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Double_Value, value); } protected override void WriteBooleanTag(ref OtlpTagWriterState state, string key, bool value) { // Write KeyValue tag - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Key, key); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Key, key); // Write KeyValue.Value tag, length and value. - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 2, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); // 1 + TagSize - state.WritePosition = ProtobufSerializer.WriteBoolWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Bool_Value, value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 2, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); // 1 + TagSize + state.WritePosition = ProtobufSerializer.WriteBoolWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Bool_Value, value); } protected override void WriteStringTag(ref OtlpTagWriterState state, string key, ReadOnlySpan value) { // Write KeyValue tag - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Key, key); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Key, key); // Write KeyValue.Value tag, length and value. var numberOfUtf8CharsInString = ProtobufSerializer.GetNumberOfUtf8CharsInString(value); var serializedLengthSize = ProtobufSerializer.ComputeVarInt64Size((ulong)numberOfUtf8CharsInString); // length = numberOfUtf8CharsInString + tagSize + length field size. - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, numberOfUtf8CharsInString + 1 + serializedLengthSize, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_String_Value, numberOfUtf8CharsInString, value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, numberOfUtf8CharsInString + 1 + serializedLengthSize, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_String_Value, numberOfUtf8CharsInString, value); } protected override void WriteArrayTag(ref OtlpTagWriterState state, string key, ref OtlpTagWriterArrayState value) { // TODO: Expand OtlpTagWriterArrayState.Buffer on IndexOutOfRangeException. // Write KeyValue tag - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Key, key); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Key, key); // Write KeyValue.Value tag and length var serializedLengthSize = ProtobufSerializer.ComputeVarInt64Size((ulong)value.WritePosition); - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, value.WritePosition + 1 + serializedLengthSize, ProtobufOtlpTraceFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); // Array content length + Array tag size + length field size + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, value.WritePosition + 1 + serializedLengthSize, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN); // Array content length + Array tag size + length field size // Write Array tag and length - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, value.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Array_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, value.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Array_Value, ProtobufWireType.LEN); Buffer.BlockCopy(value.Buffer, 0, state.Buffer, state.WritePosition, value.WritePosition); state.WritePosition += value.WritePosition; } @@ -113,26 +113,26 @@ public override OtlpTagWriterArrayState BeginWriteArray() public override void WriteNullValue(ref OtlpTagWriterArrayState state) { - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 0, ProtobufOtlpTraceFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 0, ProtobufOtlpCommonFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); } public override void WriteIntegralValue(ref OtlpTagWriterArrayState state, long value) { var size = ProtobufSerializer.ComputeVarInt64Size((ulong)value) + 1; - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, size, ProtobufOtlpTraceFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); - state.WritePosition = ProtobufSerializer.WriteInt64WithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Int_Value, (ulong)value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, size, ProtobufOtlpCommonFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteInt64WithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Int_Value, (ulong)value); } public override void WriteFloatingPointValue(ref OtlpTagWriterArrayState state, double value) { - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 9, ProtobufOtlpTraceFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); - state.WritePosition = ProtobufSerializer.WriteDoubleWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Double_Value, value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 9, ProtobufOtlpCommonFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteDoubleWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Double_Value, value); } public override void WriteBooleanValue(ref OtlpTagWriterArrayState state, bool value) { - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 2, ProtobufOtlpTraceFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); - state.WritePosition = ProtobufSerializer.WriteBoolWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_Bool_Value, value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 2, ProtobufOtlpCommonFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteBoolWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_Bool_Value, value); } public override void WriteStringValue(ref OtlpTagWriterArrayState state, ReadOnlySpan value) @@ -142,8 +142,8 @@ public override void WriteStringValue(ref OtlpTagWriterArrayState state, ReadOnl var serializedLengthSize = ProtobufSerializer.ComputeVarInt64Size((ulong)numberOfUtf8CharsInString); // length = numberOfUtf8CharsInString + tagSize + length field size. - state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, numberOfUtf8CharsInString + 1 + serializedLengthSize, ProtobufOtlpTraceFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); - state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpTraceFieldNumberConstants.AnyValue_String_Value, numberOfUtf8CharsInString, value); + state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, numberOfUtf8CharsInString + 1 + serializedLengthSize, ProtobufOtlpCommonFieldNumberConstants.ArrayValue_Value, ProtobufWireType.LEN); + state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.AnyValue_String_Value, numberOfUtf8CharsInString, value); } public override void EndWriteArray(ref OtlpTagWriterArrayState state) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceFieldNumberConstants.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceFieldNumberConstants.cs index fa0b9737a68..67daf09a48c 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceFieldNumberConstants.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceFieldNumberConstants.cs @@ -3,10 +3,12 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer; +/// +/// Defines field number constants for fields defined in +/// . +/// internal static class ProtobufOtlpTraceFieldNumberConstants { -#pragma warning disable SA1310 // Field names should not contain underscore - // Traces data internal const int TracesData_Resource_Spans = 1; @@ -70,27 +72,5 @@ internal static class ProtobufOtlpTraceFieldNumberConstants internal const int StatusCode_Unset = 0; internal const int StatusCode_Ok = 1; internal const int StatusCode_Error = 2; - - // InstrumentationScope - internal const int InstrumentationScope_Name = 1; - internal const int InstrumentationScope_Version = 2; - internal const int InstrumentationScope_Attributes = 3; - internal const int InstrumentationScope_Dropped_Attributes_Count = 4; - - // KeyValue - internal const int KeyValue_Key = 1; - internal const int KeyValue_Value = 2; - - // AnyValue - internal const int AnyValue_String_Value = 1; - internal const int AnyValue_Bool_Value = 2; - internal const int AnyValue_Int_Value = 3; - internal const int AnyValue_Double_Value = 4; - internal const int AnyValue_Array_Value = 5; - internal const int AnyValue_Kvlist_Value = 6; - internal const int AnyValue_Bytes_Value = 7; - - internal const int ArrayValue_Value = 1; -#pragma warning restore SA1310 // Field names should not contain underscore } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceSerializer.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceSerializer.cs index 7db78213b07..4e77ca8b0fa 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceSerializer.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceSerializer.cs @@ -117,10 +117,10 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt int instrumentationScopeLengthPosition = writePosition; writePosition += ReserveSizeForLength; - writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.InstrumentationScope_Name, activitySource.Name); + writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Name, activitySource.Name); if (activitySource.Version != null) { - writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.InstrumentationScope_Version, activitySource.Version); + writePosition = ProtobufSerializer.WriteStringWithTag(buffer, writePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Version, activitySource.Version); } if (activitySource.Tags != null) @@ -141,7 +141,7 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt { if (otlpTagWriterState.TagCount < maxAttributeCount) { - otlpTagWriterState.WritePosition = ProtobufSerializer.WriteTag(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, ProtobufOtlpTraceFieldNumberConstants.InstrumentationScope_Attributes, ProtobufWireType.LEN); + otlpTagWriterState.WritePosition = ProtobufSerializer.WriteTag(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Attributes, ProtobufWireType.LEN); int instrumentationScopeAttributesLengthPosition = otlpTagWriterState.WritePosition; otlpTagWriterState.WritePosition += ReserveSizeForLength; @@ -163,7 +163,7 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt { if (otlpTagWriterState.TagCount < maxAttributeCount) { - otlpTagWriterState.WritePosition = ProtobufSerializer.WriteTag(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, ProtobufOtlpTraceFieldNumberConstants.InstrumentationScope_Attributes, ProtobufWireType.LEN); + otlpTagWriterState.WritePosition = ProtobufSerializer.WriteTag(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Attributes, ProtobufWireType.LEN); int instrumentationScopeAttributesLengthPosition = otlpTagWriterState.WritePosition; otlpTagWriterState.WritePosition += ReserveSizeForLength; @@ -182,7 +182,7 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt if (otlpTagWriterState.DroppedTagCount > 0) { - otlpTagWriterState.WritePosition = ProtobufSerializer.WriteTag(buffer, otlpTagWriterState.WritePosition, ProtobufOtlpTraceFieldNumberConstants.InstrumentationScope_Dropped_Attributes_Count, ProtobufWireType.VARINT); + otlpTagWriterState.WritePosition = ProtobufSerializer.WriteTag(buffer, otlpTagWriterState.WritePosition, ProtobufOtlpCommonFieldNumberConstants.InstrumentationScope_Dropped_Attributes_Count, ProtobufWireType.VARINT); otlpTagWriterState.WritePosition = ProtobufSerializer.WriteVarInt32(buffer, otlpTagWriterState.WritePosition, (uint)otlpTagWriterState.DroppedTagCount); }