-
Notifications
You must be signed in to change notification settings - Fork 782
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
Add support for OTEL_SERVICE_NAME environmental variable #2209
Changes from 3 commits
c20a073
756ec0b
b76da07
edca8cd
bec5a63
960408f
a05f90e
17c7633
b6989e5
72038c0
d519865
3268d71
e0a9aa4
b65c03c
f36c035
a938e86
b54fe52
48b041f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// <copyright file="OtelServiceNameEnvVarDetector.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; | ||
|
||
namespace OpenTelemetry.Resources | ||
{ | ||
internal class OtelServiceNameEnvVarDetector : IResourceDetector | ||
{ | ||
private const string EnvVarKey = "OTEL_SERVICE_NAME"; | ||
CodeBlanch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public Resource Detect() | ||
{ | ||
var resource = Resource.Empty; | ||
|
||
string envResourceAttributeValue = Environment.GetEnvironmentVariable(EnvVarKey); | ||
CodeBlanch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!string.IsNullOrEmpty(envResourceAttributeValue)) | ||
pellared marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
resource = new Resource(new Dictionary<string, object> | ||
{ | ||
[ResourceSemanticConventions.AttributeServiceName] = envResourceAttributeValue, | ||
}); | ||
} | ||
|
||
return resource; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,16 +110,16 @@ public static ResourceBuilder AddAttributes(this ResourceBuilder resourceBuilder | |
} | ||
|
||
/// <summary> | ||
/// Adds resource attributes parsed from an environment variable to a | ||
/// <see cref="ResourceBuilder"/> following the <a | ||
/// Adds resource attributes parsed from OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME environment variables | ||
/// to a <see cref="ResourceBuilder"/> following the <a | ||
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable">Resource | ||
/// SDK</a>. | ||
/// </summary> | ||
/// <param name="resourceBuilder"><see cref="ResourceBuilder"/>.</param> | ||
/// <returns>Returns <see cref="ResourceBuilder"/> for chaining.</returns> | ||
public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilder resourceBuilder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this detector be out-of-the-box? This is what other languages do. For sure something for a separate PR and maybe even needs some GH Issue for discussion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on making this out-of-the-box There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will make a separate PR to address it |
||
{ | ||
return resourceBuilder.AddDetector(new OtelEnvResourceDetector()); | ||
return resourceBuilder.AddDetector(new OtelEnvResourceDetector()).AddDetector(new OtelServiceNameEnvVarDetector()); | ||
} | ||
|
||
private static string GetFileVersion() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// <copyright file="OtelServiceNameEnvVarDetectorTests.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 Xunit; | ||
|
||
namespace OpenTelemetry.Resources.Tests | ||
{ | ||
public class OtelServiceNameEnvVarDetectorTests : IDisposable | ||
{ | ||
private const string OtelEnvVarKey = "OTEL_SERVICE_NAME"; | ||
|
||
public OtelServiceNameEnvVarDetectorTests() | ||
{ | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, null); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, null); | ||
} | ||
|
||
[Fact] | ||
public void OtelServiceNameEnvVar_Null() | ||
{ | ||
// Act | ||
var resource = new OtelServiceNameEnvVarDetector().Detect(); | ||
|
||
// Assert | ||
Assert.Equal(Resource.Empty, resource); | ||
} | ||
|
||
[Fact] | ||
public void OtelServiceNameEnvVar_WithValue() | ||
{ | ||
// Arrange | ||
var envVarValue = "my-service"; | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); | ||
|
||
// Act | ||
var resource = new OtelServiceNameEnvVarDetector().Detect(); | ||
|
||
// Assert | ||
Assert.NotEqual(Resource.Empty, resource); | ||
Assert.Contains(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, envVarValue), resource.Attributes); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both
Configuration Parameters
andRemarks
seem to be too detailed and generic for the ToC. Perhaps we want to remove the H3 (e.g. replace### Remarks
with**Remarks:**
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@open-telemetry/dotnet-maintainers WDYT - should I change it?
BTW I think that the
Propagators
section should be underTracing configuration
- and not underAdvanced topics
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make a separate PR for it as I did not get feedback on what is the preference