-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for default resource creation with environment variable (
#1436) * Adding default and envVar resource. Initial commit * Adding Create method for resource creation with default resource and relevant unit tests. * Adding isNullOrEmpty check to env var value to remove redundant condition. * TracerProviderBuilder now calls Resource.Create to get the default resource with EnvVar attributes as well * The default reource now includes the attributes from env var as well. Refactoring unit tests for the same. * Adding ResourceDetector interface and public methods to add attributes from default and env var resources * Missed out new resource files. Adding * adding some more tests * Adding missing import * Updating public api list * Refactoring method name for new methods * Making IResourceDetector and corresponding impl internal until spec is official about detectors * Refactoring return * Was using wrong Sdk in test. Fixed. * Adding error handling to OtelEnvResourceDetector Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
- Loading branch information
1 parent
cb7f421
commit 4ab6da0
Showing
10 changed files
with
345 additions
and
12 deletions.
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
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,30 @@ | ||
// <copyright file="IResourceDetector.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> | ||
|
||
namespace OpenTelemetry.Resources | ||
{ | ||
/// <summary> | ||
/// An interface for Resource detectors. | ||
/// </summary> | ||
internal interface IResourceDetector | ||
{ | ||
/// <summary> | ||
/// Called to get a resource with attributes from detector. | ||
/// </summary> | ||
/// <returns>An instance of <see cref="Resource"/>.</returns> | ||
Resource Detect(); | ||
} | ||
} |
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,69 @@ | ||
// <copyright file="OtelEnvResourceDetector.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 OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Resources | ||
{ | ||
internal class OtelEnvResourceDetector : IResourceDetector | ||
{ | ||
private const string OTelResourceEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; | ||
private const char AttributeListSplitter = ','; | ||
private const char AttributeKeyValueSplitter = '='; | ||
|
||
public Resource Detect() | ||
{ | ||
var resource = Resource.Empty; | ||
|
||
try | ||
{ | ||
string envResourceAttributeValue = Environment.GetEnvironmentVariable(OTelResourceEnvVarKey); | ||
if (!string.IsNullOrEmpty(envResourceAttributeValue)) | ||
{ | ||
var attributes = ParseResourceAttributes(envResourceAttributeValue); | ||
resource = new Resource(attributes); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetrySdkEventSource.Log.ResourceDetectorFailed("OtelEnvResourceDetector", ex.Message); | ||
} | ||
|
||
return resource; | ||
} | ||
|
||
private static IEnumerable<KeyValuePair<string, object>> ParseResourceAttributes(string resourceAttributes) | ||
{ | ||
var attributes = new List<KeyValuePair<string, object>>(); | ||
|
||
string[] rawAttributes = resourceAttributes.Split(AttributeListSplitter); | ||
foreach (string rawKeyValuePair in rawAttributes) | ||
{ | ||
string[] keyValuePair = rawKeyValuePair.Split(AttributeKeyValueSplitter); | ||
if (keyValuePair.Length != 2) | ||
{ | ||
continue; | ||
} | ||
|
||
attributes.Add(new KeyValuePair<string, object>(keyValuePair[0].Trim(), keyValuePair[1].Trim())); | ||
} | ||
|
||
return attributes; | ||
} | ||
} | ||
} |
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
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
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
74 changes: 74 additions & 0 deletions
74
test/OpenTelemetry.Tests/Resources/OtelEnvResourceDetectorTest.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,74 @@ | ||
// <copyright file="OtelEnvResourceDetectorTest.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 OtelEnvResourceDetectorTest : IDisposable | ||
{ | ||
private const string OtelEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; | ||
|
||
public OtelEnvResourceDetectorTest() | ||
{ | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, null); | ||
} | ||
|
||
[Fact] | ||
public void OtelEnvResource_NullEnvVar() | ||
{ | ||
// Arrange | ||
var resource = new OtelEnvResourceDetector().Detect(); | ||
|
||
// Assert | ||
Assert.Equal(Resource.Empty, resource); | ||
} | ||
|
||
[Fact] | ||
public void OtelEnvResource_WithEnvVar_1() | ||
{ | ||
// Arrange | ||
var envVarValue = "Key1=Val1,Key2=Val2"; | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); | ||
var resource = new OtelEnvResourceDetector().Detect(); | ||
|
||
// Assert | ||
Assert.NotEqual(Resource.Empty, resource); | ||
Assert.Contains(new KeyValuePair<string, object>("Key1", "Val1"), resource.Attributes); | ||
} | ||
|
||
[Fact] | ||
public void OtelEnvResource_WithEnvVar_2() | ||
{ | ||
// Arrange | ||
var envVarValue = "Key1,Key2=Val2"; | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, envVarValue); | ||
var resource = new OtelEnvResourceDetector().Detect(); | ||
|
||
// Assert | ||
Assert.NotEqual(Resource.Empty, resource); | ||
Assert.Single(resource.Attributes); | ||
Assert.Contains(new KeyValuePair<string, object>("Key2", "Val2"), resource.Attributes); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable(OtelEnvVarKey, null); | ||
} | ||
} | ||
} |
Oops, something went wrong.