Skip to content
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

[otlp] refactor constants #6010

Merged
merged 6 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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/common/v1/common.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.")]
TimothyMothra marked this conversation as resolved.
Show resolved Hide resolved
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,15 @@

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/logs/v1/logs.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 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 +68,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 @@ -126,28 +126,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
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> 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;
}
Expand Down Expand Up @@ -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<char> value)
Expand All @@ -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)
Expand Down
Loading
Loading