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

**URL:** <https://discuss.elastic.co/t/azure-functions-apm-not-sending-service-name-in-traces-bug-in-how-service-name-is-overridden-with-null-value/345943>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [October 27, 2023, 5:36pm UTC](https://discuss.elastic.co/t/azure-functions-apm-not-sending-service-name-in-traces-bug-in-how-service-name-is-overridden-with-null-value/345943 "2023-10-27T17:36:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrisarkameru](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrisarkameru/32/112242_2.png) [@andrisarkameru](https://discuss.elastic.co/u/andrisarkameru)\
**Post date:** [October 27, 2023, 5:36pm UTC](https://discuss.elastic.co/t/azure-functions-apm-not-sending-service-name-in-traces-bug-in-how-service-name-is-overridden-with-null-value/345943/1 "2023-10-27T17:36:20Z")

</div>

### 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](https://github.com/elastic/apm-agent-dotnet/commit/d398ca0c695f16f338e510426655ef1acc38cd7e) 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

```auto
/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:

```auto
			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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![andrisarkameru](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrisarkameru/32/112242_2.png) [@andrisarkameru](https://discuss.elastic.co/u/andrisarkameru)\
**Post date:** [October 30, 2023, 4:10pm UTC](https://discuss.elastic.co/t/azure-functions-apm-not-sending-service-name-in-traces-bug-in-how-service-name-is-overridden-with-null-value/345943/2 "2023-10-30T16:10:42Z")

</div>

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

```auto
			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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 27, 2023, 4:11pm UTC](https://discuss.elastic.co/t/azure-functions-apm-not-sending-service-name-in-traces-bug-in-how-service-name-is-overridden-with-null-value/345943/3 "2023-11-27T16:11:02Z")

</div>

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