Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 82bf919

Browse files
Port AzureWebAppTelemetryInitializer (#332)
1 parent d0667d9 commit 82bf919

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/NuGet.Services.Logging/NuGet.Services.Logging.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<ItemGroup>
5151
<Compile Include="ApplicationInsights.cs" />
5252
<Compile Include="ApplicationInsightsConfiguration.cs" />
53+
<Compile Include="TelemetryInitializers\AzureWebAppTelemetryInitializer.cs" />
5354
<Compile Include="TelemetryInitializers\DeploymentIdTelemetryEnricher.cs" />
5455
<Compile Include="TelemetryInitializers\DeploymentLabelEnricher.cs" />
5556
<Compile Include="DurationMetric.cs" />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.ApplicationInsights.Channel;
6+
using Microsoft.ApplicationInsights.Extensibility;
7+
8+
namespace NuGet.Services.Logging
9+
{
10+
/// <summary>
11+
/// Overrides the initialized telemetry context. This should be added last in
12+
/// the Application Insights telemetry list.
13+
/// See: https://github.com/microsoft/ApplicationInsights-dotnet-server/blob/e5a0edbe570e0938d3cb7a36a57b25d0db4d3c01/Src/WindowsServer/WindowsServer.Shared/AzureWebAppRoleEnvironmentTelemetryInitializer.cs#L12
14+
/// </summary>
15+
public class AzureWebAppTelemetryInitializer
16+
: ITelemetryInitializer
17+
{
18+
private const string StagingSlotSuffix = "-staging";
19+
20+
public void Initialize(ITelemetry telemetry)
21+
{
22+
// Application Insight's Azure Web App Role Environment telemetry initializer uses
23+
// the hostname for the "cloud_roleName" property, which unintentionally creates separate
24+
// role names for our production/staging slots.
25+
var roleName = telemetry.Context.Cloud.RoleName;
26+
if (!string.IsNullOrEmpty(roleName) && roleName.EndsWith(StagingSlotSuffix, StringComparison.OrdinalIgnoreCase))
27+
{
28+
telemetry.Context.Cloud.RoleName = roleName.Substring(0, roleName.Length - StagingSlotSuffix.Length);
29+
}
30+
}
31+
}
32+
}

tests/NuGet.Services.Logging.Tests/NuGet.Services.Logging.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
</ItemGroup>
5555
<ItemGroup>
5656
<Compile Include="ApplicationInsightsTests.cs" />
57+
<Compile Include="TelemetryInitializers\AzureWebAppTelemetryInitializerFacts.cs" />
5758
<Compile Include="TelemetryInitializers\DeploymentLabelEnricherTests.cs" />
5859
<Compile Include="Extensions\DiagnosticsTelemetryModuleExtensionsTests.cs" />
5960
<Compile Include="ExceptionTelemetryProcessorTests.cs" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.ApplicationInsights.Channel;
5+
using Microsoft.ApplicationInsights.DataContracts;
6+
using Moq;
7+
using Xunit;
8+
9+
namespace NuGet.Services.Logging.Tests
10+
{
11+
public class AzureWebAppTelemetryInitializerFacts
12+
{
13+
[Theory]
14+
[InlineData(null, null)]
15+
[InlineData("hello", "hello")]
16+
[InlineData("-staging-test", "-staging-test")]
17+
[InlineData("hello-staging", "hello")]
18+
[InlineData("hello-sTAGing", "hello")]
19+
public void UpdatesRoleName(string input, string expected)
20+
{
21+
var telemetry = new Mock<ITelemetry>();
22+
var context = new TelemetryContext();
23+
24+
context.Cloud.RoleName = input;
25+
26+
telemetry.Setup(t => t.Context).Returns(context);
27+
28+
var target = new AzureWebAppTelemetryInitializer();
29+
target.Initialize(telemetry.Object);
30+
31+
Assert.Equal(expected, context.Cloud.RoleName);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)