-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exports service.name to StackDriver if the value is present in an ins…
…tance of OpenTelemetry.Resources.Resource (#1652) When many services collaborate, information about a service's name can help understand which service creates which trace. Google Trace Explorer allows the display of a service name next to trace information.
- Loading branch information
1 parent
323e7e6
commit 6566323
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
* Update OpenTelemetry SDK version to `1.8.0`. | ||
([#1635](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1635)) | ||
* When many services collaborate, information about a service's name can help understand which service creates which trace. | ||
Check failure on line 7 in src/OpenTelemetry.Exporter.Stackdriver/CHANGELOG.md GitHub Actions / lint-md / run-markdownlintLine length
|
||
Google Trace Explorer allows the display of a service name next to trace information. | ||
Please use `services.ConfigureResource(resource => resource.AddService("my-service", "1.0.0"))` to add service name and version. | ||
Check failure on line 9 in src/OpenTelemetry.Exporter.Stackdriver/CHANGELOG.md GitHub Actions / lint-md / run-markdownlintLine length
|
||
|
||
## 1.0.0-beta.5 | ||
|
||
|
38 changes: 38 additions & 0 deletions
38
src/OpenTelemetry.Exporter.Stackdriver/Implementation/ResourceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Collections.Generic; | ||
using Google.Cloud.Trace.V2; | ||
using OpenTelemetry.Resources; | ||
|
||
namespace OpenTelemetry.Exporter.Stackdriver.Implementation; | ||
|
||
internal static class ResourceExtensions | ||
{ | ||
private static readonly HashSet<string> ExportableResourceNames = | ||
[ | ||
ResourceSemanticConventions.AttributeServiceName, | ||
ResourceSemanticConventions.AttributeServiceVersion | ||
]; | ||
|
||
/// <summary> | ||
/// Adds resource attributes to the span. | ||
/// </summary> | ||
/// <param name="span">Google Cloud Trace Span to be annotated.</param> | ||
/// <param name="resource"> | ||
/// The Resource contains attributes such as "service.name" that provide metadata about the service being traced. | ||
/// These attributes are used to annotate the Google Cloud Trace Span, enhancing the trace data available in the Google | ||
/// Trace Explorer UI. | ||
/// </param> | ||
public static void AnnotateWith(this Span span, Resource resource) | ||
{ | ||
span.Attributes ??= new Span.Types.Attributes(); | ||
foreach (var attr in resource.Attributes) | ||
{ | ||
if (ExportableResourceNames.Contains(attr.Key)) | ||
{ | ||
span.Attributes.AttributeMap.Add(attr.Key, attr.Value.ToAttributeValue()); | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/OpenTelemetry.Exporter.Stackdriver/Implementation/ResourceSemanticConventions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace OpenTelemetry.Exporter.Stackdriver.Implementation; | ||
|
||
internal class ResourceSemanticConventions | ||
{ | ||
public const string AttributeServiceName = "service.name"; | ||
public const string AttributeServiceVersion = "service.version"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
test/OpenTelemetry.Exporter.Stackdriver.Tests/ResourceExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Collections.Generic; | ||
using Google.Cloud.Trace.V2; | ||
using OpenTelemetry.Exporter.Stackdriver.Implementation; | ||
using OpenTelemetry.Resources; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Exporter.Stackdriver.Tests; | ||
|
||
public class ResourceExtensionsTests | ||
{ | ||
[Fact] | ||
public void Enriches_Span_Attributes_With_Service_Name() | ||
{ | ||
const string serviceName = "some service name"; | ||
const string serviceVersion = "2.3.4"; | ||
var resource = new Resource(new Dictionary<string, object> | ||
{ | ||
["key1"] = "value1", | ||
["key2"] = "value2", | ||
[ResourceSemanticConventions.AttributeServiceName] = serviceName, | ||
[ResourceSemanticConventions.AttributeServiceVersion] = serviceVersion, | ||
}); | ||
|
||
var span = new Span(); | ||
span.AnnotateWith(resource); | ||
Assert.Contains(span.Attributes.AttributeMap, kvp => | ||
kvp.Key == ResourceSemanticConventions.AttributeServiceName && | ||
kvp.Value.StringValue.Value == serviceName); | ||
|
||
Assert.Contains(span.Attributes.AttributeMap, kvp => | ||
kvp.Key == ResourceSemanticConventions.AttributeServiceVersion && | ||
kvp.Value.StringValue.Value == serviceVersion); | ||
} | ||
|
||
[Fact] | ||
public void Otel_Resource_Has_No_Service_Name() | ||
{ | ||
var resource = new Resource(new Dictionary<string, object> { ["key1"] = "value1", ["key2"] = "value2" }); | ||
|
||
var span = new Span(); | ||
span.AnnotateWith(resource); | ||
} | ||
} |