Skip to content

Commit

Permalink
[otlp] refactor constants (#6010)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra authored Dec 3, 2024
1 parent 44bc90a commit 0c775e5
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer;

/// <summary>
/// Defines field number constants for fields defined in
/// <see href="https://github.com/open-telemetry/opentelemetry-proto/blob/v1.2.0/opentelemetry/proto/common/v1/common.proto"/>.
/// </summary>
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer;

/// <summary>
/// Defines field number constants for fields defined in
/// <see href="https://github.com/open-telemetry/opentelemetry-proto/blob/v1.2.0/opentelemetry/proto/logs/v1/logs.proto"/>.
/// </summary>
internal static class ProtobufOtlpLogFieldNumberConstants
{
#pragma warning disable SA1310 // Field names should not contain underscore

// Logs data
internal const int LogsData_Resource_Logs = 1;

Expand Down Expand Up @@ -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
}

Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer;

using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Defines field number constants for fields defined in
/// <see href="https://github.com/open-telemetry/opentelemetry-proto/blob/v1.2.0/opentelemetry/proto/metrics/v1/metrics.proto"/>.
/// </summary>
[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
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
}
Expand Down
Loading

0 comments on commit 0c775e5

Please sign in to comment.