Azure / Azure/azure-functions-dotnet-worker

Azure Function Defaults override deployment.environment.name in OTEL_RESOURCE_ATTRIBUTES

Open
#3,404 2 comments 0 reactions 1 assignee Claimed by @RohitRanjanMS View on GitHub
logging needs-discussion Needs: Triage (Functions) potential-bug
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

### Description

The FunctionsResourceDirector's Detect() method doesn't check to see if the OTEL_RESOURCE_ATTRIBUTES environment variable has an attribute of `deployment.environment` or `deployment.environment.name` already set before overwriting it. There's an assumption that the name of the slot represents the environment. If it is felt that the slot must be incorporated, then I feel it would be better served appending `-{slot-name}` when there's an existing value.

### Steps to reproduce

Adding this test in [https://github.com/Azure/azure-functions-dotnet-worker/blob/main/test/DotNetWorker.OpenTelemetry.Tests/EndToEndTests.cs](EndToEndTests.cs) will show the failures.

```csharp
public static IEnumerable EnvironmentNameSkippedData =>
[
// OTEL_RESOURCE_ATTRIBUTES[deployment.environment.name] not set locally
[new Dictionary { { "OTEL_RESOURCE_ATTRIBUTES", "other.key=value" } }],
// OTEL_RESOURCE_ATTRIBUTES[deployment.environment.name] set locally
[new Dictionary { { "OTEL_RESOURCE_ATTRIBUTES", "deployment.environment.name=custom-name,other.key=value" } }],
// OTEL_RESOURCE_ATTRIBUTES[deployment.environment.name] set in Azure
[new Dictionary { { "WEBSITE_SITE_NAME", "appName" }, { "WEBSITE_SLOT_NAME", "staging" }, { "OTEL_RESOURCE_ATTRIBUTES", "deployment.environment.name=custom-name,other.key=value" } }],
// OTEL_RESOURCE_ATTRIBUTES[deployment.environment] set locally
[new Dictionary { { "OTEL_RESOURCE_ATTRIBUTES", "deployment.environment=custom-name,other.key=value" } }],
// OTEL_RESOURCE_ATTRIBUTES[deployment.environment] set in Azure
[new Dictionary { { "WEBSITE_SITE_NAME", "appName" }, { "WEBSITE_SLOT_NAME", "staging" }, { "OTEL_RESOURCE_ATTRIBUTES", "deployment.environment=custom-name,other.key=value" } }],
// Leading/trailing whitespace around pair is trimmed
[new Dictionary { { "WEBSITE_SITE_NAME", "appName" }, { "OTEL_RESOURCE_ATTRIBUTES", "other.key=value, deployment.environment=custom-name" } }],
];

[Theory]
[MemberData(nameof(EnvironmentNameSkippedData))]
public void ResourceDetector_EnvironmentName_SkippedWhenConfigured(Dictionary envVars)
{
using var _ = new TestScopedEnvironmentVariable(envVars);
FunctionsResourceDetector detector = new FunctionsResourceDetector();
Resource resource = detector.Detect();

Assert.DoesNotContain(resource.Attributes, a => a.Key == "deployment.environment.name");
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.