Skip to content

Commit

Permalink
WEBSITE_CLOUD_ROLENAME value, if set, will override site name in Appl…
Browse files Browse the repository at this point in the history
…ication Insights. Resolves #2635
  • Loading branch information
kashimiz committed Mar 3, 2021
1 parent 826670a commit 99f2fc9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class WebJobsRoleEnvironmentTelemetryInitializer : ITelemetryInitialize
{
internal const string AzureWebsiteName = "WEBSITE_SITE_NAME";
internal const string AzureWebsiteSlotName = "WEBSITE_SLOT_NAME";
internal const string AzureWebsiteCloudRolename = "WEBSITE_CLOUD_ROLENAME";
private const string DefaultProductionSlotName = "production";
private const string WebAppSuffix = ".azurewebsites.net";

Expand All @@ -35,12 +36,22 @@ public void Initialize(ITelemetry telemetry)
return;
}

Lazy<string> websiteCloudRoleName = new Lazy<string>(() =>
{
return Environment.GetEnvironmentVariable(AzureWebsiteCloudRolename);
});

Lazy<string> siteSlotName = new Lazy<string>(() =>
{
// We cannot cache these values as the environment variables can change on the fly.
return GetAzureWebsiteUniqueSlotName();
});

if (!string.IsNullOrEmpty(websiteCloudRoleName.Value))
{
telemetry.Context.Cloud.RoleName = websiteCloudRoleName.Value;
}

if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
{
telemetry.Context.Cloud.RoleName = siteSlotName.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,83 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.Azure.WebJobs.Logging.ApplicationInsights;
using Xunit;

namespace Microsoft.Azure.WebJobs.Host.UnitTests.Loggers
{
public class WebJobsRoleEnvironmentTelemetryInitializerTests : IDisposable
public class WebJobsRoleEnvironmentTelemetryInitializerTests
{
public WebJobsRoleEnvironmentTelemetryInitializerTests()
{
// make sure these are clear before each test
SetEnvironmentVariables(null, null);
}
public WebJobsRoleEnvironmentTelemetryInitializerTests() { }

[Fact]
public void Initialize_DoesNotThrow_WhenNoEnvironmentVariables()
{
var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteName, null))
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteSlotName, null))
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteCloudRolename, null))
{
var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();

var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);
var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);

Assert.Null(telemetry.Context.Cloud.RoleName);
Assert.Null(telemetry.Context.GetInternalContext().NodeName);
Assert.Null(telemetry.Context.Cloud.RoleName);
Assert.Null(telemetry.Context.GetInternalContext().NodeName);
}
}

[Fact]
public void Initialize_WithSlot()
{
SetEnvironmentVariables("mytestsite", "Staging");

var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteName, "mytestsite"))
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteSlotName, "Staging"))
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteCloudRolename, null))
{
var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();

var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);
var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);

Assert.Equal("mytestsite-staging", telemetry.Context.Cloud.RoleName);
Assert.Equal("mytestsite-staging.azurewebsites.net", telemetry.Context.GetInternalContext().NodeName);
Assert.Equal("mytestsite-staging", telemetry.Context.Cloud.RoleName);
Assert.Equal("mytestsite-staging.azurewebsites.net", telemetry.Context.GetInternalContext().NodeName);
}
}

[Fact]
public void Initialize_WithProductionSlot()
{
SetEnvironmentVariables("mytestsite", "Production");
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteName, "mytestsite"))
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteSlotName, "Production"))
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteCloudRolename, null))
{
var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();

var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();
var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);

var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);

Assert.Equal("mytestsite", telemetry.Context.Cloud.RoleName);
Assert.Equal("mytestsite.azurewebsites.net", telemetry.Context.GetInternalContext().NodeName);
Assert.Equal("mytestsite", telemetry.Context.Cloud.RoleName);
Assert.Equal("mytestsite.azurewebsites.net", telemetry.Context.GetInternalContext().NodeName);
}
}

private static void SetEnvironmentVariables(string websiteName, string slotName)
[Fact]
public void Initialize_WithWebsiteCloudRoleName()
{
Environment.SetEnvironmentVariable(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteName, websiteName);
Environment.SetEnvironmentVariable(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteSlotName, slotName);
}
var testCloudRoleName = "mycloudrolename";

public void Dispose()
{
SetEnvironmentVariables(null, null);
using (EnvVarHolder.Set(WebJobsRoleEnvironmentTelemetryInitializer.AzureWebsiteCloudRolename, testCloudRoleName))
{
var initializer = new WebJobsRoleEnvironmentTelemetryInitializer();

var telemetry = new TraceTelemetry();
initializer.Initialize(telemetry);

Assert.Equal(testCloudRoleName, telemetry.Context.Cloud.RoleName);
}
}
}
}

0 comments on commit 99f2fc9

Please sign in to comment.