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

[repo/StackDriver] Prepare to .NET9 #2270

Merged
merged 11 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -11,7 +11,7 @@ namespace OpenTelemetry.Exporter.Stackdriver.Implementation;

internal static class ActivityExtensions
{
private static readonly Dictionary<string, string> LabelsToReplace = new Dictionary<string, string>
private static readonly Dictionary<string, string> LabelsToReplace = new()
{
{ "component", "/component" },
{ "http.method", "/http/method" },
Expand Down Expand Up @@ -110,29 +110,17 @@ public static Span.Types.Link ToLink(this ActivityLink link)

public static AttributeValue ToAttributeValue(this object? av)
{
switch (av)
return av switch
{
case string s:
return new AttributeValue
{
StringValue = new TruncatableString { Value = s },
};
case bool b:
return new AttributeValue { BoolValue = b };
case long l:
return new AttributeValue { IntValue = l };
case double d:
return new AttributeValue
{
StringValue = new TruncatableString { Value = d.ToString(CultureInfo.InvariantCulture) },
};
case null:
return new AttributeValue();
default:
return new AttributeValue
{
StringValue = new TruncatableString { Value = av.ToString() },
};
}
string s => new AttributeValue { StringValue = new TruncatableString { Value = s } },
bool b => new AttributeValue { BoolValue = b },
long l => new AttributeValue { IntValue = l },
double d => new AttributeValue
{
StringValue = new TruncatableString { Value = d.ToString(CultureInfo.InvariantCulture) },
},
null => new AttributeValue(),
_ => new AttributeValue { StringValue = new TruncatableString { Value = av.ToString() } },
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Google.Cloud.Trace.V2;
using Grpc.Core;
using OpenTelemetry.Exporter.Stackdriver.Implementation;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Exporter.Stackdriver;

Expand Down Expand Up @@ -78,27 +77,23 @@ internal StackdriverTraceExporter(string projectId, TraceServiceClient traceServ
/// <inheritdoc/>
public override ExportResult Export(in Batch<Activity> batch)
{
TraceServiceClient? traceWriter = this.traceServiceClient;
if (traceWriter == null)
var traceWriter = this.traceServiceClient ?? new TraceServiceClientBuilder
{
traceWriter = new TraceServiceClientBuilder
{
Settings = this.traceServiceSettings,
}.Build();
}
Settings = this.traceServiceSettings,
}.Build();

var batchSpansRequest = new BatchWriteSpansRequest
{
ProjectName = this.googleCloudProjectId,
};

Resource? resource = this.ParentProvider?.GetResource();
var resource = this.ParentProvider?.GetResource();
foreach (var activity in batch)
{
// It should never happen that the time has no correct kind, only if OpenTelemetry is used incorrectly.
if (activity.StartTimeUtc.Kind == DateTimeKind.Utc)
{
Span span = activity.ToSpan(this.googleCloudProjectId.ProjectId);
var span = activity.ToSpan(this.googleCloudProjectId.ProjectId);
if (resource != null)
{
span.AnnotateWith(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public void ActivityExtensions_Export_Duplicate_ActivityLink_Tag_Key()
var spanId1 = ActivitySpanId.CreateRandom();
var spanId2 = ActivitySpanId.CreateRandom();

ActivityContext context1 = new ActivityContext(traceId, spanId1, ActivityTraceFlags.Recorded, isRemote: true);
ActivityContext context2 = new ActivityContext(traceId, spanId2, ActivityTraceFlags.Recorded, isRemote: true);
var context1 = new ActivityContext(traceId, spanId1, ActivityTraceFlags.Recorded, isRemote: true);
var context2 = new ActivityContext(traceId, spanId2, ActivityTraceFlags.Recorded, isRemote: true);

KeyValuePair<string, object?>[] dupTags = new[]
{
new KeyValuePair<string, object?>("key1", "value1"),
new KeyValuePair<string, object?>("key2", "value2"),
new KeyValuePair<string, object?>("key1", "value3"),
};
ActivityLink link1 = new ActivityLink(context1, tags: new ActivityTagsCollection(dupTags));
ActivityLink link2 = new ActivityLink(context2);
KeyValuePair<string, object?>[] dupTags =
[
new("key1", "value1"),
new("key2", "value2"),
new("key1", "value3")
];
var link1 = new ActivityLink(context1, tags: new ActivityTagsCollection(dupTags));
var link2 = new ActivityLink(context2);

var links = new List<ActivityLink> { link1, link2 };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ static StackdriverExporterTests()
public void StackdriverExporter_CustomActivityProcessor()
{
const string ActivitySourceName = "stackdriver.test";
Guid requestId = Guid.NewGuid();
TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
var requestId = Guid.NewGuid();
var testActivityProcessor = new TestActivityProcessor();

bool startCalled = false;
bool endCalled = false;
var startCalled = false;
var endCalled = false;

testActivityProcessor.StartAction =
(a) =>
Expand Down Expand Up @@ -86,7 +86,7 @@ public void StackdriverExporter_WithServiceNameMetadata()
public void StackdriverExporter_TraceClientThrows_ExportResultFailure()
{
Exception? exception;
ExportResult result = ExportResult.Success;
var result = ExportResult.Success;
var exportedItems = new List<Activity>();
const string ActivitySourceName = "stackdriver.test";
var source = new ActivitySource(ActivitySourceName);
Expand All @@ -95,9 +95,9 @@ public void StackdriverExporter_TraceClientThrows_ExportResultFailure()

var processor = new BatchActivityExportProcessor(new InMemoryExporter<Activity>(exportedItems));

for (int i = 0; i < 10; i++)
for (var i = 0; i < 10; i++)
{
using Activity activity = CreateTestActivity();
using var activity = CreateTestActivity();
processor.OnEnd(activity);
}

Expand All @@ -122,7 +122,7 @@ void RunTest(Batch<Activity> batch)
public void StackdriverExporter_TraceClientDoesNotTrow_ExportResultSuccess()
{
Exception? exception;
ExportResult result = ExportResult.Failure;
var result = ExportResult.Failure;
var exportedItems = new List<Activity>();
const string ActivitySourceName = "stackdriver.test";
var source = new ActivitySource(ActivitySourceName);
Expand All @@ -131,9 +131,9 @@ public void StackdriverExporter_TraceClientDoesNotTrow_ExportResultSuccess()

var processor = new BatchActivityExportProcessor(new InMemoryExporter<Activity>(exportedItems));

for (int i = 0; i < 10; i++)
for (var i = 0; i < 10; i++)
{
using Activity activity = CreateTestActivity();
using var activity = CreateTestActivity();
processor.OnEnd(activity);
}

Expand All @@ -159,15 +159,14 @@ internal static Activity CreateTestActivity(
Dictionary<string, object>? additionalAttributes = null,
bool addEvents = true,
bool addLinks = true,
Resource? resource = null,
ActivityKind kind = ActivityKind.Client)
{
var startTimestamp = DateTime.UtcNow;
var endTimestamp = startTimestamp.AddSeconds(60);
var eventTimestamp = DateTime.UtcNow;
var traceId = ActivityTraceId.CreateFromString("e8ea7e9ac72de94e91fabc613f9686b2".AsSpan());

var parentSpanId = ActivitySpanId.CreateFromBytes(new byte[] { 12, 23, 34, 45, 56, 67, 78, 89 });
var parentSpanId = ActivitySpanId.CreateFromBytes([12, 23, 34, 45, 56, 67, 78, 89]);

var attributes = new Dictionary<string, object?>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OpenTelemetry.Exporter.Stackdriver.Tests;

public class TestActivityProcessor : BaseProcessor<Activity>, IDisposable
internal class TestActivityProcessor : BaseProcessor<Activity>, IDisposable
{
public Action<Activity>? StartAction;
public Action<Activity>? EndAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class TestTraceServiceClient(bool throwException) : TraceServiceClient
{
private readonly bool throwException = throwException;

public List<Span> Spans { get; } = new List<Span>();
public List<Span> Spans { get; } = [];

public override void BatchWriteSpans(BatchWriteSpansRequest request, CallSettings callSettings)
{
Expand Down
Loading