Azure Functions APM not sending service.name in traces - bug in how service.name is overridden with null value

APM Agent version

dotnet apm agent

Initially tested on:
"Elastic.Apm.Azure.Functions" Version="1.25.0" NuGet package

Later tried debugging on commit: d398ca0 by adding it locally to my project.

Environment

Windows
Azure Functions v4 dotnet-isolated
.net 6.0

Elastic APM server
"build_date": "2023-09-18T13:00:04-04:00",
"build_sha": "825865682816d18b5e66e94949bce8d2f0172044",
"version": "8.10.2"

Describe the bug

APM Traces is not sending service.name to APM server, hence getting rejected with

/intake/v2/events. APM Server response: status code: BadRequest, content:
[2023-10-27T16:57:24.330Z] {"accepted":0,"errors":[{"message":"validation error: metadata: service: 'name' required",

Service name is provided to the app by loca.settings.json variable: WEBSITE_SITE_NAME

My initial (limited knowledge) conclusion leads to the fact that Class AzureFunctionsContext incorrectly gets MetaData
MetaData = AzureFunctionsMetadataProvider.GetAzureFunctionsMetaData(Logger);
Because it does not supply _environmentVariables that the method GetAzureFunctionsMetaData reads from.

And then it overrides Service.Name property in method UpdateServiceInformation like this:

			if (service.Name == AbstractConfigurationReader.AdaptServiceName(AbstractConfigurationReader.DiscoverDefaultServiceName()))
			{
				// Only override the service name if it was set to default.
				service.Name = MetaData.WebsiteSiteName;
			}

When debugging I see that service.Name was a reasonable name (my project name) and then it got updated with null value.

Same thing happens with service.Runtime in the same method above. It gets overwritten with null because of the incorrectly read MetaData object. And is also causing APM server error if I manually fix the service.name problem.

Not sure what was the initial idea, but I see that the method correctly reads MetaData when it is called from
here:

private Api.Cloud GetMetadata()
		{
			var data = GetAzureFunctionsMetaData(_logger, _environmentVariables);

This leads me to conclusion that I have correctly provided the environment variable for it to be read as WebSiteName, but the Context is reading it incorrectly.

To Reproduce

Steps to reproduce the behavior:

Use Basic dotnet-isolated Azure function V4 locally and send APM traces to server.
Enable APM logging in debug mode to see that traces are not being sent with the error mentioned above.

Expected behavior

Expected to WebsiteSiteName property be read from environment variables and thus service.name would be set from Context when traces are sent.

Actual behavior

Traces are sent without service.name property.

1 Like

After more in depth debugging I found out that MetaData is indeed null but because of some missing environment variables when testing locally.

			if (helper.NullOrEmptyVariable(AzureEnvironmentVariables.FunctionsExtensionVersion,
				    functionsExtensionVersion) ||
			    helper.NullOrEmptyVariable(AzureEnvironmentVariables.WebsiteOwnerName, websiteOwnerName) ||
			    helper.NullOrEmptyVariable(AzureEnvironmentVariables.WebsiteSiteName, websiteSiteName))
				return new AzureFunctionsMetaData { IsValid = false };

			var tokens = helper.TokenizeWebSiteOwnerName(websiteOwnerName);
			if (!tokens.HasValue) return new AzureFunctionsMetaData { IsValid = false };

Basicly this invalidates AzureFunctionsMetaData object if any of the properties are not set. And of course I didn't think of setting any of them locally.

I didn't found any documentation about that - am I missing it, or maybe these Ifs are too harsh and should not invalidate setup of service.name / service.runtime if the WEBSITE_OWNER_NAME is not set, for example?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.