-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/fixing-instrumentation-diagnostics…
…ource
- Loading branch information
Showing
12 changed files
with
348 additions
and
23 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
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.