Skip to content

Commit b449fa1

Browse files
authored
Add telemetry.sdk.* attributes to default resource (#4369)
1 parent 0004895 commit b449fa1

4 files changed

Lines changed: 46 additions & 28 deletions

File tree

src/OpenTelemetry/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* The default resource provided by `ResourceBuilder.CreateDefault()` now
6+
adds the `telemetry.sdk.*` attributes defined in the
7+
[specification](https://github.com/open-telemetry/opentelemetry-specification/tree/12fcec1ff255b1535db75708e52a3a21f86f0fae/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value).
8+
([#4369](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4369))
59
* Fixed an issue with `HashCode` computations throwing exceptions on .NET
610
Standard 2.1 targets.
711
([#4362](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4362))

src/OpenTelemetry/Resources/ResourceBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ private ResourceBuilder()
5959
internal IServiceProvider? ServiceProvider { get; set; }
6060

6161
/// <summary>
62-
/// Creates a <see cref="ResourceBuilder"/> instance with Default
63-
/// service.name added. See <a
62+
/// Creates a <see cref="ResourceBuilder"/> instance with default attributes
63+
/// added. See <a
6464
/// href="https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value">resource
6565
/// semantic conventions</a> for details.
6666
/// Additionally it adds resource attributes parsed from OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME environment variables
@@ -70,7 +70,10 @@ private ResourceBuilder()
7070
/// </summary>
7171
/// <returns>Created <see cref="ResourceBuilder"/>.</returns>
7272
public static ResourceBuilder CreateDefault()
73-
=> new ResourceBuilder().AddResource(DefaultResource).AddEnvironmentVariableDetector();
73+
=> new ResourceBuilder()
74+
.AddResource(DefaultResource)
75+
.AddTelemetrySdk()
76+
.AddEnvironmentVariableDetector();
7477

7578
/// <summary>
7679
/// Creates an empty <see cref="ResourceBuilder"/> instance.

test/OpenTelemetry.Tests/Resources/ResourceTest.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ public void GetResourceWithTelemetrySDKAttributes()
395395
// Assert
396396
var attributes = resource.Attributes;
397397
Assert.Equal(4, attributes.Count());
398+
ValidateDefaultAttributes(attributes);
398399
ValidateTelemetrySdkAttributes(attributes);
399400
}
400401

@@ -406,8 +407,9 @@ public void GetResourceWithDefaultAttributes_EmptyResource()
406407

407408
// Assert
408409
var attributes = resource.Attributes;
409-
Assert.Single(attributes);
410+
Assert.Equal(4, attributes.Count());
410411
ValidateDefaultAttributes(attributes);
412+
ValidateTelemetrySdkAttributes(attributes);
411413
}
412414

413415
[Fact]
@@ -418,9 +420,10 @@ public void GetResourceWithDefaultAttributes_ResourceWithAttrs()
418420

419421
// Assert
420422
var attributes = resource.Attributes;
421-
Assert.Equal(3, attributes.Count());
423+
Assert.Equal(6, attributes.Count());
422424
ValidateAttributes(attributes, 0, 1);
423425
ValidateDefaultAttributes(attributes);
426+
ValidateTelemetrySdkAttributes(attributes);
424427
}
425428

426429
[Fact]
@@ -432,11 +435,12 @@ public void GetResourceWithDefaultAttributes_WithResourceEnvVar()
432435

433436
// Assert
434437
var attributes = resource.Attributes;
435-
Assert.Equal(5, attributes.Count());
438+
Assert.Equal(8, attributes.Count());
436439
ValidateAttributes(attributes, 0, 1);
437440
ValidateDefaultAttributes(attributes);
438441
Assert.Contains(new KeyValuePair<string, object>("EVKey1", "EVVal1"), attributes);
439442
Assert.Contains(new KeyValuePair<string, object>("EVKey2", "EVVal2"), attributes);
443+
ValidateTelemetrySdkAttributes(attributes);
440444
}
441445

442446
[Fact]
@@ -448,9 +452,10 @@ public void EnvironmentVariableDetectors_DoNotDuplicateAttributes()
448452

449453
// Assert
450454
var attributes = resource.Attributes;
451-
Assert.Equal(3, attributes.Count());
455+
Assert.Equal(6, attributes.Count());
452456
Assert.Contains(new KeyValuePair<string, object>("EVKey1", "EVVal1"), attributes);
453457
Assert.Contains(new KeyValuePair<string, object>("EVKey2", "EVVal2"), attributes);
458+
ValidateTelemetrySdkAttributes(attributes);
454459
}
455460

456461
[Fact]
@@ -462,9 +467,10 @@ public void GetResource_WithServiceEnvVar()
462467

463468
// Assert
464469
var attributes = resource.Attributes;
465-
Assert.Equal(3, attributes.Count());
470+
Assert.Equal(6, attributes.Count());
466471
ValidateAttributes(attributes, 0, 1);
467472
Assert.Contains(new KeyValuePair<string, object>("service.name", "some-service"), attributes);
473+
ValidateTelemetrySdkAttributes(attributes);
468474
}
469475

470476
[Fact]
@@ -477,9 +483,10 @@ public void GetResource_WithServiceNameSetWithTwoEnvVars()
477483

478484
// Assert
479485
var attributes = resource.Attributes;
480-
Assert.Equal(3, attributes.Count());
486+
Assert.Equal(6, attributes.Count());
481487
ValidateAttributes(attributes, 0, 1);
482488
Assert.Contains(new KeyValuePair<string, object>("service.name", "from-service-name"), attributes);
489+
ValidateTelemetrySdkAttributes(attributes);
483490
}
484491

485492
[Fact]
@@ -492,9 +499,10 @@ public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode()
492499

493500
// Assert
494501
var attributes = resource.Attributes;
495-
Assert.Equal(4, attributes.Count());
502+
Assert.Equal(7, attributes.Count());
496503
ValidateAttributes(attributes, 0, 1);
497504
Assert.Contains(new KeyValuePair<string, object>("service.name", "from-code"), attributes);
505+
ValidateTelemetrySdkAttributes(attributes);
498506
}
499507

500508
[Fact]
@@ -562,6 +570,21 @@ public void ResourceBuilder_AddDetectorInternal_Test()
562570
Assert.True(validTestRun);
563571
}
564572

573+
internal static void ValidateTelemetrySdkAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
574+
{
575+
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.name", "opentelemetry"), attributes);
576+
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.language", "dotnet"), attributes);
577+
var versionAttribute = attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version"));
578+
Assert.Single(versionAttribute);
579+
}
580+
581+
internal static void ValidateDefaultAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
582+
{
583+
var serviceName = attributes.Where(pair => pair.Key.Equals("service.name"));
584+
Assert.Single(serviceName);
585+
Assert.Contains("unknown_service", serviceName.FirstOrDefault().Value as string);
586+
}
587+
565588
private static void ClearEnvVars()
566589
{
567590
Environment.SetEnvironmentVariable(OtelEnvResourceDetector.EnvVarKey, null);
@@ -594,21 +617,6 @@ private static void ValidateResource(Resource resource, int attributeCount)
594617
ValidateAttributes(resource.Attributes);
595618
}
596619

597-
private static void ValidateTelemetrySdkAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
598-
{
599-
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.name", "opentelemetry"), attributes);
600-
Assert.Contains(new KeyValuePair<string, object>("telemetry.sdk.language", "dotnet"), attributes);
601-
var versionAttribute = attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version"));
602-
Assert.Single(versionAttribute);
603-
}
604-
605-
private static void ValidateDefaultAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
606-
{
607-
var serviceName = attributes.Where(pair => pair.Key.Equals("service.name"));
608-
Assert.Single(serviceName);
609-
Assert.Contains("unknown_service", serviceName.FirstOrDefault().Value as string);
610-
}
611-
612620
private static Dictionary<string, object> CreateAttributes(int attributeCount, int startIndex = 0)
613621
{
614622
var attributes = new Dictionary<string, object>();

test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Diagnostics;
1818
using OpenTelemetry.Instrumentation;
1919
using OpenTelemetry.Resources;
20+
using OpenTelemetry.Resources.Tests;
2021
using OpenTelemetry.Tests;
2122
using Xunit;
2223

@@ -1077,9 +1078,11 @@ public void TracerProviderSdkBuildsWithDefaultResource()
10771078

10781079
Assert.NotNull(resource);
10791080
Assert.NotEqual(Resource.Empty, resource);
1080-
Assert.Single(resource.Attributes);
1081-
Assert.Equal(ResourceSemanticConventions.AttributeServiceName, resource.Attributes.FirstOrDefault().Key);
1082-
Assert.Contains("unknown_service", (string)resource.Attributes.FirstOrDefault().Value);
1081+
1082+
var attributes = resource.Attributes;
1083+
Assert.Equal(4, attributes.Count());
1084+
ResourceTest.ValidateDefaultAttributes(attributes);
1085+
ResourceTest.ValidateTelemetrySdkAttributes(attributes);
10831086
}
10841087

10851088
[Theory]

0 commit comments

Comments
 (0)