forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtlpTraceExporterTests.cs
More file actions
643 lines (524 loc) · 27.1 KB
/
OtlpTraceExporterTests.cs
File metadata and controls
643 lines (524 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// <copyright file="OtlpTraceExporterTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Google.Protobuf.Collections;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Resources;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;
using OtlpCollector = OpenTelemetry.Proto.Collector.Trace.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpTrace = OpenTelemetry.Proto.Trace.V1;
using Status = OpenTelemetry.Trace.Status;
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
{
[Collection("xUnitCollectionPreventingTestsThatDependOnSdkConfigurationFromRunningInParallel")]
public class OtlpTraceExporterTests : Http2UnencryptedSupportTests
{
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();
static OtlpTraceExporterTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
var listener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
};
ActivitySource.AddActivityListener(listener);
}
[Fact]
public void AddOtlpTraceExporterNamedOptionsSupported()
{
int defaultExporterOptionsConfigureOptionsInvocations = 0;
int namedExporterOptionsConfigureOptionsInvocations = 0;
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
services.Configure<OtlpExporterOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
services.Configure<OtlpExporterOptions>("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++);
})
.AddOtlpExporter()
.AddOtlpExporter("Exporter2", o => { })
.Build();
Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations);
Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations);
}
[Fact]
public void OtlpExporter_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.AddOtlpExporter());
}
[Fact]
public void UserHttpFactoryCalled()
{
OtlpExporterOptions options = new OtlpExporterOptions();
var defaultFactory = options.HttpClientFactory;
int invocations = 0;
options.Protocol = OtlpExportProtocol.HttpProtobuf;
options.HttpClientFactory = () =>
{
invocations++;
return defaultFactory();
};
using (var exporter = new OtlpTraceExporter(options))
{
Assert.Equal(1, invocations);
}
using (var provider = Sdk.CreateTracerProviderBuilder()
.AddOtlpExporter(o =>
{
o.Protocol = OtlpExportProtocol.HttpProtobuf;
o.HttpClientFactory = options.HttpClientFactory;
})
.Build())
{
Assert.Equal(2, invocations);
}
options.HttpClientFactory = null;
Assert.Throws<InvalidOperationException>(() =>
{
using var exporter = new OtlpTraceExporter(options);
});
options.HttpClientFactory = () => null;
Assert.Throws<InvalidOperationException>(() =>
{
using var exporter = new OtlpTraceExporter(options);
});
}
[Fact]
public void ServiceProviderHttpClientFactoryInvoked()
{
IServiceCollection services = new ServiceCollection();
services.AddHttpClient();
int invocations = 0;
services.AddHttpClient("OtlpTraceExporter", configureClient: (client) => invocations++);
services.AddOpenTelemetry().WithTracing(builder => builder
.AddOtlpExporter(o => o.Protocol = OtlpExportProtocol.HttpProtobuf));
using var serviceProvider = services.BuildServiceProvider();
var tracerProvider = serviceProvider.GetRequiredService<TracerProvider>();
Assert.Equal(1, invocations);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ToOtlpResourceSpansTest(bool includeServiceNameInResource)
{
var evenTags = new[] { new KeyValuePair<string, object>("k0", "v0") };
var oddTags = new[] { new KeyValuePair<string, object>("k1", "v1") };
var sources = new[]
{
new ActivitySource("even", "2.4.6"),
new ActivitySource("odd", "1.3.5"),
};
var resourceBuilder = ResourceBuilder.CreateEmpty();
if (includeServiceNameInResource)
{
resourceBuilder.AddService("service-name", "ns1");
}
var exportedItems = new List<Activity>();
var builder = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(resourceBuilder)
.AddSource(sources[0].Name)
.AddSource(sources[1].Name)
.AddProcessor(new SimpleActivityExportProcessor(new InMemoryExporter<Activity>(exportedItems)));
using var openTelemetrySdk = builder.Build();
const int numOfSpans = 10;
bool isEven;
for (var i = 0; i < numOfSpans; i++)
{
isEven = i % 2 == 0;
var source = sources[i % 2];
var activityKind = isEven ? ActivityKind.Client : ActivityKind.Server;
var activityTags = isEven ? evenTags : oddTags;
using Activity activity = source.StartActivity($"span-{i}", activityKind, parentContext: default, activityTags);
}
Assert.Equal(10, exportedItems.Count);
var batch = new Batch<Activity>(exportedItems.ToArray(), exportedItems.Count);
RunTest(DefaultSdkLimitOptions, batch);
void RunTest(SdkLimitOptions sdkOptions, Batch<Activity> batch)
{
var request = new OtlpCollector.ExportTraceServiceRequest();
request.AddBatch(sdkOptions, resourceBuilder.Build().ToOtlpResource(), batch);
Assert.Single(request.ResourceSpans);
var otlpResource = request.ResourceSpans.First().Resource;
if (includeServiceNameInResource)
{
Assert.Contains(otlpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceName && kvp.Value.StringValue == "service-name");
Assert.Contains(otlpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceNamespace && kvp.Value.StringValue == "ns1");
}
else
{
Assert.Contains(otlpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceName && kvp.Value.ToString().Contains("unknown_service:"));
}
var scopeSpans = request.ResourceSpans.First().ScopeSpans;
Assert.Equal(2, scopeSpans.Count);
foreach (var scope in scopeSpans)
{
Assert.Equal(numOfSpans / 2, scope.Spans.Count);
Assert.NotNull(scope.Scope);
var expectedSpanNames = new List<string>();
var start = scope.Scope.Name == "even" ? 0 : 1;
for (var i = start; i < numOfSpans; i += 2)
{
expectedSpanNames.Add($"span-{i}");
}
var otlpSpans = scope.Spans;
Assert.Equal(expectedSpanNames.Count, otlpSpans.Count);
var kv0 = new OtlpCommon.KeyValue { Key = "k0", Value = new OtlpCommon.AnyValue { StringValue = "v0" } };
var kv1 = new OtlpCommon.KeyValue { Key = "k1", Value = new OtlpCommon.AnyValue { StringValue = "v1" } };
var expectedTag = scope.Scope.Name == "even"
? kv0
: kv1;
foreach (var otlpSpan in otlpSpans)
{
Assert.Contains(otlpSpan.Name, expectedSpanNames);
Assert.Contains(expectedTag, otlpSpan.Attributes);
}
}
}
}
[Fact]
public void SpanLimitsTest()
{
var sdkOptions = new SdkLimitOptions()
{
AttributeValueLengthLimit = 4,
AttributeCountLimit = 3,
SpanEventCountLimit = 1,
SpanLinkCountLimit = 1,
};
var tags = new ActivityTagsCollection()
{
new KeyValuePair<string, object>("TruncatedTag", "12345"),
new KeyValuePair<string, object>("TruncatedStringArray", new string[] { "12345", "1234", string.Empty, null }),
new KeyValuePair<string, object>("TruncatedObjectTag", new object()),
new KeyValuePair<string, object>("OneTagTooMany", 1),
};
var links = new[]
{
new ActivityLink(default, tags),
new ActivityLink(default, tags),
};
using var activitySource = new ActivitySource(nameof(this.SpanLimitsTest));
using var activity = activitySource.StartActivity("root", ActivityKind.Server, default(ActivityContext), tags, links);
var event1 = new ActivityEvent("Event", DateTime.UtcNow, tags);
var event2 = new ActivityEvent("OneEventTooMany", DateTime.Now, tags);
activity.AddEvent(event1);
activity.AddEvent(event2);
var otlpSpan = activity.ToOtlpSpan(sdkOptions);
Assert.NotNull(otlpSpan);
Assert.Equal(3, otlpSpan.Attributes.Count);
Assert.Equal(1u, otlpSpan.DroppedAttributesCount);
Assert.Equal("1234", otlpSpan.Attributes[0].Value.StringValue);
ArrayValueAsserts(otlpSpan.Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString().Substring(0, 4), otlpSpan.Attributes[2].Value.StringValue);
Assert.Single(otlpSpan.Events);
Assert.Equal(1u, otlpSpan.DroppedEventsCount);
Assert.Equal(3, otlpSpan.Events[0].Attributes.Count);
Assert.Equal(1u, otlpSpan.Events[0].DroppedAttributesCount);
Assert.Equal("1234", otlpSpan.Events[0].Attributes[0].Value.StringValue);
ArrayValueAsserts(otlpSpan.Events[0].Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString().Substring(0, 4), otlpSpan.Events[0].Attributes[2].Value.StringValue);
Assert.Single(otlpSpan.Links);
Assert.Equal(1u, otlpSpan.DroppedLinksCount);
Assert.Equal(3, otlpSpan.Links[0].Attributes.Count);
Assert.Equal(1u, otlpSpan.Links[0].DroppedAttributesCount);
Assert.Equal("1234", otlpSpan.Links[0].Attributes[0].Value.StringValue);
ArrayValueAsserts(otlpSpan.Links[0].Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString().Substring(0, 4), otlpSpan.Links[0].Attributes[2].Value.StringValue);
void ArrayValueAsserts(RepeatedField<OtlpCommon.AnyValue> values)
{
var expectedStringArray = new string[] { "1234", "1234", string.Empty, null };
for (var i = 0; i < expectedStringArray.Length; ++i)
{
var expectedValue = expectedStringArray[i];
var expectedValueCase = expectedValue != null
? OtlpCommon.AnyValue.ValueOneofCase.StringValue
: OtlpCommon.AnyValue.ValueOneofCase.None;
var actual = values[i];
Assert.Equal(expectedValueCase, actual.ValueCase);
if (expectedValueCase != OtlpCommon.AnyValue.ValueOneofCase.None)
{
Assert.Equal(expectedValue, actual.StringValue);
}
}
}
}
[Fact]
public void ToOtlpSpanTest()
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var rootActivity = activitySource.StartActivity("root", ActivityKind.Producer);
var attributes = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("bool", true),
new KeyValuePair<string, object>("long", 1L),
new KeyValuePair<string, object>("string", "text"),
new KeyValuePair<string, object>("double", 3.14),
new KeyValuePair<string, object>("int", 1),
new KeyValuePair<string, object>("datetime", DateTime.UtcNow),
new KeyValuePair<string, object>("bool_array", new bool[] { true, false }),
new KeyValuePair<string, object>("int_array", new int[] { 1, 2 }),
new KeyValuePair<string, object>("double_array", new double[] { 1.0, 2.09 }),
new KeyValuePair<string, object>("string_array", new string[] { "a", "b" }),
};
foreach (var kvp in attributes)
{
rootActivity.SetTag(kvp.Key, kvp.Value);
}
var startTime = new DateTime(2020, 02, 20, 20, 20, 20, DateTimeKind.Utc);
DateTimeOffset dateTimeOffset;
dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(0);
var expectedUnixTimeTicks = (ulong)(startTime.Ticks - dateTimeOffset.Ticks);
var duration = TimeSpan.FromMilliseconds(1555);
rootActivity.SetStartTime(startTime);
rootActivity.SetEndTime(startTime + duration);
Span<byte> traceIdSpan = stackalloc byte[16];
rootActivity.TraceId.CopyTo(traceIdSpan);
var traceId = traceIdSpan.ToArray();
var otlpSpan = rootActivity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan);
Assert.Equal("root", otlpSpan.Name);
Assert.Equal(OtlpTrace.Span.Types.SpanKind.Producer, otlpSpan.Kind);
Assert.Equal(traceId, otlpSpan.TraceId);
Assert.Empty(otlpSpan.ParentSpanId);
Assert.Null(otlpSpan.Status);
Assert.Empty(otlpSpan.Events);
Assert.Empty(otlpSpan.Links);
OtlpTestHelpers.AssertOtlpAttributes(attributes, otlpSpan.Attributes);
var expectedStartTimeUnixNano = 100 * expectedUnixTimeTicks;
Assert.Equal(expectedStartTimeUnixNano, otlpSpan.StartTimeUnixNano);
var expectedEndTimeUnixNano = expectedStartTimeUnixNano + (duration.TotalMilliseconds * 1_000_000);
Assert.Equal(expectedEndTimeUnixNano, otlpSpan.EndTimeUnixNano);
var childLinks = new List<ActivityLink> { new ActivityLink(rootActivity.Context, new ActivityTagsCollection(attributes)) };
var childActivity = activitySource.StartActivity(
"child",
ActivityKind.Client,
rootActivity.Context,
links: childLinks);
childActivity.SetStatus(Status.Error);
var childEvents = new List<ActivityEvent> { new ActivityEvent("e0"), new ActivityEvent("e1", default, new ActivityTagsCollection(attributes)) };
childActivity.AddEvent(childEvents[0]);
childActivity.AddEvent(childEvents[1]);
Span<byte> parentIdSpan = stackalloc byte[8];
rootActivity.Context.SpanId.CopyTo(parentIdSpan);
var parentId = parentIdSpan.ToArray();
otlpSpan = childActivity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan);
Assert.Equal("child", otlpSpan.Name);
Assert.Equal(OtlpTrace.Span.Types.SpanKind.Client, otlpSpan.Kind);
Assert.Equal(traceId, otlpSpan.TraceId);
Assert.Equal(parentId, otlpSpan.ParentSpanId);
// Assert.Equal(OtlpTrace.Status.Types.StatusCode.NotFound, otlpSpan.Status.Code);
Assert.Equal(Status.Error.Description ?? string.Empty, otlpSpan.Status.Message);
Assert.Empty(otlpSpan.Attributes);
Assert.Equal(childEvents.Count, otlpSpan.Events.Count);
for (var i = 0; i < childEvents.Count; i++)
{
Assert.Equal(childEvents[i].Name, otlpSpan.Events[i].Name);
OtlpTestHelpers.AssertOtlpAttributes(childEvents[i].Tags.ToList(), otlpSpan.Events[i].Attributes);
}
childLinks.Reverse();
Assert.Equal(childLinks.Count, otlpSpan.Links.Count);
for (var i = 0; i < childLinks.Count; i++)
{
OtlpTestHelpers.AssertOtlpAttributes(childLinks[i].Tags.ToList(), otlpSpan.Links[i].Attributes);
}
}
[Fact]
public void ToOtlpSpanActivitiesWithNullArrayTest()
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var rootActivity = activitySource.StartActivity("root", ActivityKind.Client);
Assert.NotNull(rootActivity);
var stringArr = new string[] { "test", string.Empty, null };
rootActivity.SetTag("stringArray", stringArr);
var otlpSpan = rootActivity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan);
var stringArray = otlpSpan.Attributes.FirstOrDefault(kvp => kvp.Key == "stringArray");
Assert.NotNull(stringArray);
Assert.Equal("test", stringArray.Value.ArrayValue.Values[0].StringValue);
Assert.Equal(string.Empty, stringArray.Value.ArrayValue.Values[1].StringValue);
Assert.Equal(OtlpCommon.AnyValue.ValueOneofCase.None, stringArray.Value.ArrayValue.Values[2].ValueCase);
}
[Theory]
[InlineData(ActivityStatusCode.Unset, "Description will be ignored if status is Unset.")]
[InlineData(ActivityStatusCode.Ok, "Description will be ignored if status is Okay.")]
[InlineData(ActivityStatusCode.Error, "Description will be kept if status is Error.")]
public void ToOtlpSpanNativeActivityStatusTest(ActivityStatusCode expectedStatusCode, string statusDescription)
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var activity = activitySource.StartActivity("Name");
activity.SetStatus(expectedStatusCode, statusDescription);
var otlpSpan = activity.ToOtlpSpan(DefaultSdkLimitOptions);
if (expectedStatusCode == ActivityStatusCode.Unset)
{
Assert.Null(otlpSpan.Status);
}
else
{
Assert.NotNull(otlpSpan.Status);
Assert.Equal((int)expectedStatusCode, (int)otlpSpan.Status.Code);
if (expectedStatusCode == ActivityStatusCode.Error)
{
Assert.Equal(statusDescription, otlpSpan.Status.Message);
}
if (expectedStatusCode == ActivityStatusCode.Ok)
{
Assert.Empty(otlpSpan.Status.Message);
}
}
}
[Theory]
[InlineData(StatusCode.Unset, "Unset", "Description will be ignored if status is Unset.")]
[InlineData(StatusCode.Ok, "Ok", "Description must only be used with the Error StatusCode.")]
[InlineData(StatusCode.Error, "Error", "Error description.")]
public void ToOtlpSpanStatusTagTest(StatusCode expectedStatusCode, string statusCodeTagValue, string statusDescription)
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var activity = activitySource.StartActivity("Name");
activity.SetTag(SpanAttributeConstants.StatusCodeKey, statusCodeTagValue);
activity.SetTag(SpanAttributeConstants.StatusDescriptionKey, statusDescription);
var otlpSpan = activity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan.Status);
Assert.Equal((int)expectedStatusCode, (int)otlpSpan.Status.Code);
if (expectedStatusCode == StatusCode.Error)
{
Assert.Equal(statusDescription, otlpSpan.Status.Message);
}
else
{
Assert.Empty(otlpSpan.Status.Message);
}
}
[Theory]
[InlineData(StatusCode.Unset, "uNsET")]
[InlineData(StatusCode.Ok, "oK")]
[InlineData(StatusCode.Error, "ERROR")]
public void ToOtlpSpanStatusTagIsCaseInsensitiveTest(StatusCode expectedStatusCode, string statusCodeTagValue)
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var activity = activitySource.StartActivity("Name");
activity.SetTag(SpanAttributeConstants.StatusCodeKey, statusCodeTagValue);
var otlpSpan = activity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan.Status);
Assert.Equal((int)expectedStatusCode, (int)otlpSpan.Status.Code);
}
[Fact]
public void ToOtlpSpanActivityStatusTakesPrecedenceOverStatusTagsWhenActivityStatusCodeIsOk()
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var activity = activitySource.StartActivity("Name");
const string TagDescriptionOnError = "Description when TagStatusCode is Error.";
activity.SetStatus(ActivityStatusCode.Ok);
activity.SetTag(SpanAttributeConstants.StatusCodeKey, "ERROR");
activity.SetTag(SpanAttributeConstants.StatusDescriptionKey, TagDescriptionOnError);
var otlpSpan = activity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan.Status);
Assert.Equal((int)ActivityStatusCode.Ok, (int)otlpSpan.Status.Code);
Assert.Empty(otlpSpan.Status.Message);
}
[Fact]
public void ToOtlpSpanActivityStatusTakesPrecedenceOverStatusTagsWhenActivityStatusCodeIsError()
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var activity = activitySource.StartActivity("Name");
const string StatusDescriptionOnError = "Description when ActivityStatusCode is Error.";
activity.SetStatus(ActivityStatusCode.Error, StatusDescriptionOnError);
activity.SetTag(SpanAttributeConstants.StatusCodeKey, "OK");
var otlpSpan = activity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan.Status);
Assert.Equal((int)ActivityStatusCode.Error, (int)otlpSpan.Status.Code);
Assert.Equal(StatusDescriptionOnError, otlpSpan.Status.Message);
}
[Fact]
public void ToOtlpSpanPeerServiceTest()
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var rootActivity = activitySource.StartActivity("root", ActivityKind.Client);
rootActivity.SetTag(SemanticConventions.AttributeHttpHost, "opentelemetry.io");
var otlpSpan = rootActivity.ToOtlpSpan(DefaultSdkLimitOptions);
Assert.NotNull(otlpSpan);
var peerService = otlpSpan.Attributes.FirstOrDefault(kvp => kvp.Key == SemanticConventions.AttributePeerService);
Assert.NotNull(peerService);
Assert.Equal("opentelemetry.io", peerService.Value.StringValue);
}
[Fact]
public void UseOpenTelemetryProtocolActivityExporterWithCustomActivityProcessor()
{
if (Environment.Version.Major == 3)
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
const string ActivitySourceName = "otlp.test";
TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
bool startCalled = false;
bool endCalled = false;
testActivityProcessor.StartAction =
(a) =>
{
startCalled = true;
};
testActivityProcessor.EndAction =
(a) =>
{
endCalled = true;
};
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(ActivitySourceName)
.AddProcessor(testActivityProcessor)
.AddOtlpExporter()
.Build();
using var source = new ActivitySource(ActivitySourceName);
var activity = source.StartActivity("Test Otlp Activity");
activity?.Stop();
Assert.True(startCalled);
Assert.True(endCalled);
}
[Fact]
public void Shutdown_ClientShutdownIsCalled()
{
var exportClientMock = new Mock<IExportClient<OtlpCollector.ExportTraceServiceRequest>>();
var exporter = new OtlpTraceExporter(new OtlpExporterOptions(), DefaultSdkLimitOptions, exportClientMock.Object);
var result = exporter.Shutdown();
exportClientMock.Verify(m => m.Shutdown(It.IsAny<int>()), Times.Once());
}
[Fact]
public void Null_BatchExportProcessorOptions_SupportedTest()
{
Sdk.CreateTracerProviderBuilder()
.AddOtlpExporter(
o =>
{
o.Protocol = OtlpExportProtocol.HttpProtobuf;
o.ExportProcessorType = ExportProcessorType.Batch;
o.BatchExportProcessorOptions = null;
});
}
}
}