Skip to content

Commit

Permalink
[repo/Instana] Prepare to .NET9 (#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Oct 30, 2024
1 parent 9b8b4ba commit 0a89d81
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 249 deletions.
14 changes: 7 additions & 7 deletions src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ internal enum SpanKind

internal class InstanaSpan
{
private InstanaSpanTransformInfo transformInfo = new InstanaSpanTransformInfo();
private InstanaSpanTransformInfo transformInfo = new();
private string n = string.Empty;
private string t = string.Empty;
private string lt = string.Empty;
private From f = new From();
private From f = new();
private string p = string.Empty;
private string s = string.Empty;
private SpanKind k = SpanKind.NOT_SET;
private long ts;
private long d;
private bool tp;
private int ec;
private Data data = new Data()
private Data data = new()
{
data = new Dictionary<string, object>(8),
Events = new List<SpanEvent>(8),
Expand Down Expand Up @@ -197,9 +197,9 @@ internal bool IsEmpty()
internal class Data
#pragma warning restore SA1402 // File may only contain a single type
{
private List<SpanEvent> events = new List<SpanEvent>(8);
private Dictionary<string, object> dataField = new Dictionary<string, object>(8);
private Dictionary<string, string> tags = new Dictionary<string, string>(2);
private List<SpanEvent> events = new(8);
private Dictionary<string, object> dataField = new(8);
private Dictionary<string, string> tags = new(2);

#pragma warning disable SA1300 // Element should begin with upper-case letter

Expand Down Expand Up @@ -241,7 +241,7 @@ internal class SpanEvent
#pragma warning restore SA1402 // File may only contain a single type
{
private string name = string.Empty;
private Dictionary<string, string> tags = new Dictionary<string, string>();
private Dictionary<string, string> tags = [];

public string Name
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ internal class InstanaSpanFactory
{
internal static InstanaSpan CreateSpan()
{
InstanaSpan instanaSpan = new InstanaSpan
var instanaSpan = new InstanaSpan
{
Data = new Data()
{
data = new Dictionary<string, object>(),
Tags = new Dictionary<string, string>(),
data = [],
Tags = [],
Events = new List<SpanEvent>(8),
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal static async Task SerializeToStreamWriterAsync(InstanaSpan instanaSpan,
await writer.WriteAsync(COMMA).ConfigureAwait(false);
await AppendPropertyAsync(DateToUnixMillis(instanaSpan.Ts), "ts", writer).ConfigureAwait(false);
await writer.WriteAsync(COMMA).ConfigureAwait(false);
await AppendPropertyAsync((long)(instanaSpan.D / 10_000), "d", writer).ConfigureAwait(false);
await AppendPropertyAsync(instanaSpan.D / 10_000L, "d", writer).ConfigureAwait(false);
await writer.WriteAsync(COMMA).ConfigureAwait(false);
await AppendObjectAsync(SerializeDataAsync, "data", instanaSpan, writer).ConfigureAwait(false);
await writer.WriteAsync(COMMA).ConfigureAwait(false);
Expand Down Expand Up @@ -227,55 +227,53 @@ private static async Task SerializeEventsAsync(InstanaSpan instanaSpan, StreamWr
return;
}

using (var enumerator = instanaSpan.Data.Events.GetEnumerator())
using var enumerator = instanaSpan.Data.Events.GetEnumerator();
byte i = 0;
try
{
byte i = 0;
try
await writer.WriteAsync("[").ConfigureAwait(false);
while (enumerator.MoveNext())
{
await writer.WriteAsync("[").ConfigureAwait(false);
while (enumerator.MoveNext())
if (i > 0)
{
if (i > 0)
{
await writer.WriteAsync(COMMA).ConfigureAwait(false);
}
else
{
i = 1;
}
await writer.WriteAsync(COMMA).ConfigureAwait(false);
}
else
{
i = 1;
}

await writer.WriteAsync(OPEN_BRACE).ConfigureAwait(false);
await writer.WriteAsync(OPEN_BRACE).ConfigureAwait(false);
await writer.WriteAsync(QUOTE).ConfigureAwait(false);
await writer.WriteAsync(InstanaExporterConstants.EVENT_NAME_FIELD).ConfigureAwait(false);
await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false);
await writer.WriteAsync(enumerator.Current.Name).ConfigureAwait(false);
await writer.WriteAsync(QUOTE_COMMA_QUOTE).ConfigureAwait(false);
await writer.WriteAsync(InstanaExporterConstants.EVENT_TIMESTAMP_FIELD).ConfigureAwait(false);
await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false);
await writer.WriteAsync(DateToUnixMillis(enumerator.Current.Ts).ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
await writer.WriteAsync(QUOTE).ConfigureAwait(false);

if (enumerator.Current.Tags.Count > 0)
{
await writer.WriteAsync(COMMA).ConfigureAwait(false);
await writer.WriteAsync(QUOTE).ConfigureAwait(false);
await writer.WriteAsync(InstanaExporterConstants.EVENT_NAME_FIELD).ConfigureAwait(false);
await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false);
await writer.WriteAsync(enumerator.Current.Name).ConfigureAwait(false);
await writer.WriteAsync(QUOTE_COMMA_QUOTE).ConfigureAwait(false);
await writer.WriteAsync(InstanaExporterConstants.EVENT_TIMESTAMP_FIELD).ConfigureAwait(false);
await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false);
await writer.WriteAsync(DateToUnixMillis(enumerator.Current.Ts).ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
await writer.WriteAsync(InstanaExporterConstants.TAGS_FIELD).ConfigureAwait(false);
await writer.WriteAsync(QUOTE).ConfigureAwait(false);

if (enumerator.Current.Tags.Count > 0)
{
await writer.WriteAsync(COMMA).ConfigureAwait(false);
await writer.WriteAsync(QUOTE).ConfigureAwait(false);
await writer.WriteAsync(InstanaExporterConstants.TAGS_FIELD).ConfigureAwait(false);
await writer.WriteAsync(QUOTE).ConfigureAwait(false);
await writer.WriteAsync(COLON).ConfigureAwait(false);
await SerializeTagsLogicAsync(enumerator.Current.Tags, writer).ConfigureAwait(false);
}

await writer.WriteAsync(CLOSE_BRACE).ConfigureAwait(false);
await writer.WriteAsync(COLON).ConfigureAwait(false);
await SerializeTagsLogicAsync(enumerator.Current.Tags, writer).ConfigureAwait(false);
}

await writer.WriteAsync("]").ConfigureAwait(false);
}
catch (InvalidOperationException)
{
// if the collection gets modified while serializing, we might get a collision.
// There is no good way of preventing this and continuing normally except locking
// which needs investigation
await writer.WriteAsync(CLOSE_BRACE).ConfigureAwait(false);
}

await writer.WriteAsync("]").ConfigureAwait(false);
}
catch (InvalidOperationException)
{
// if the collection gets modified while serializing, we might get a collision.
// There is no good way of preventing this and continuing normally except locking
// which needs investigation
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ public virtual async Task ProcessAsync(Activity activity, InstanaSpan instanaSpa

protected virtual void PreProcess(Activity activity, InstanaSpan instanaSpan)
{
if (instanaSpan.TransformInfo == null)
{
instanaSpan.TransformInfo = new InstanaSpanTransformInfo();
}
instanaSpan.TransformInfo ??= new InstanaSpanTransformInfo();

if (instanaSpan.Data == null)
instanaSpan.Data ??= new Data()
{
instanaSpan.Data = new Data()
{
data = new Dictionary<string, object>(),
Events = new List<SpanEvent>(8),
Tags = new Dictionary<string, string>(),
};
}
data = [],
Events = new List<SpanEvent>(8),
Tags = [],
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp

instanaSpan.N = InstanaExporterConstants.OTEL_SPAN_TYPE;

string traceId = activity.TraceId.ToHexString();
var traceId = activity.TraceId.ToHexString();
if (traceId.Length == 32)
{
instanaSpan.T = traceId.Substring(16);
Expand All @@ -24,8 +24,8 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp
instanaSpan.T = traceId;
}

bool hasParent = false;
string parentSpanId = activity.ParentSpanId.ToHexString();
var hasParent = false;
var parentSpanId = activity.ParentSpanId.ToHexString();
if (!string.IsNullOrEmpty(parentSpanId) && GetLongFromHex(parentSpanId) != 0)
{
hasParent = true;
Expand All @@ -52,19 +52,13 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp

private static SpanKind GetSpanKind(ActivityKind activityKind)
{
switch (activityKind)
return activityKind switch
{
case ActivityKind.Consumer:
case ActivityKind.Server:
return SpanKind.ENTRY;
case ActivityKind.Client:
case ActivityKind.Producer:
return SpanKind.EXIT;
case ActivityKind.Internal:
return SpanKind.INTERMEDIATE;
default:
return SpanKind.NOT_SET;
}
ActivityKind.Consumer or ActivityKind.Server => SpanKind.ENTRY,
ActivityKind.Client or ActivityKind.Producer => SpanKind.EXIT,
ActivityKind.Internal => SpanKind.INTERMEDIATE,
_ => SpanKind.NOT_SET,
};
}

private static long GetLongFromHex(string hexValue)
Expand All @@ -73,7 +67,7 @@ private static long GetLongFromHex(string hexValue)
{
try
{
string[] ids = hexValue.Split(',');
var ids = hexValue.Split(',');
return Convert.ToInt64(ids[ids.Length - 1].Trim(), 16);
}
catch (Exception)
Expand All @@ -86,7 +80,7 @@ private static long GetLongFromHex(string hexValue)

private static void SetKind(Activity activity, InstanaSpan instanaSpan)
{
bool isEntrySpan = false;
var isEntrySpan = false;

if (instanaSpan.Data.data != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp
{
Name = activityEvent.Name,
Ts = activityEvent.Timestamp.Ticks,
Tags = new Dictionary<string, string>(),
Tags = [],
};

foreach (var eventTag in activityEvent.Tags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp

this.PreProcess(activity, instanaSpan);

string statusCode = string.Empty;
string statusDesc = string.Empty;
Dictionary<string, string> tags = new Dictionary<string, string>();
var statusCode = string.Empty;
var statusDesc = string.Empty;
var tags = new Dictionary<string, string>();
foreach (var tag in activity.Tags)
{
if (tag.Key == "otel.status_code")
{
statusCode = (tag.Value as string) ?? string.Empty;
statusCode = tag.Value ?? string.Empty;
continue;
}

if (tag.Key == "otel.status_description")
{
statusDesc = (tag.Value as string) ?? string.Empty;
statusDesc = tag.Value ?? string.Empty;
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class SpanSender : ISpanSender
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
{
private readonly Task queueSenderTask;
private readonly ConcurrentQueue<InstanaSpan> spansQueue = new ConcurrentQueue<InstanaSpan>();
private readonly ConcurrentQueue<InstanaSpan> spansQueue = new();

public SpanSender()
{
Expand All @@ -33,7 +33,7 @@ private async void TaskSpanSender()
while (true)
{
// check if we can send spans
if (this.spansQueue.TryPeek(out InstanaSpan _))
if (this.spansQueue.TryPeek(out var _))
{
// actually send spans
await Transport.SendSpansAsync(this.spansQueue).ConfigureAwait(false);
Expand Down
Loading

0 comments on commit 0a89d81

Please sign in to comment.