This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port AzureWebAppTelemetryInitializer (#332)
- Loading branch information
1 parent
d0667d9
commit 82bf919
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/NuGet.Services.Logging/TelemetryInitializers/AzureWebAppTelemetryInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
|
||
namespace NuGet.Services.Logging | ||
{ | ||
/// <summary> | ||
/// Overrides the initialized telemetry context. This should be added last in | ||
/// the Application Insights telemetry list. | ||
/// See: https://github.com/microsoft/ApplicationInsights-dotnet-server/blob/e5a0edbe570e0938d3cb7a36a57b25d0db4d3c01/Src/WindowsServer/WindowsServer.Shared/AzureWebAppRoleEnvironmentTelemetryInitializer.cs#L12 | ||
/// </summary> | ||
public class AzureWebAppTelemetryInitializer | ||
: ITelemetryInitializer | ||
{ | ||
private const string StagingSlotSuffix = "-staging"; | ||
|
||
public void Initialize(ITelemetry telemetry) | ||
{ | ||
// Application Insight's Azure Web App Role Environment telemetry initializer uses | ||
// the hostname for the "cloud_roleName" property, which unintentionally creates separate | ||
// role names for our production/staging slots. | ||
var roleName = telemetry.Context.Cloud.RoleName; | ||
if (!string.IsNullOrEmpty(roleName) && roleName.EndsWith(StagingSlotSuffix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
telemetry.Context.Cloud.RoleName = roleName.Substring(0, roleName.Length - StagingSlotSuffix.Length); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...uGet.Services.Logging.Tests/TelemetryInitializers/AzureWebAppTelemetryInitializerFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace NuGet.Services.Logging.Tests | ||
{ | ||
public class AzureWebAppTelemetryInitializerFacts | ||
{ | ||
[Theory] | ||
[InlineData(null, null)] | ||
[InlineData("hello", "hello")] | ||
[InlineData("-staging-test", "-staging-test")] | ||
[InlineData("hello-staging", "hello")] | ||
[InlineData("hello-sTAGing", "hello")] | ||
public void UpdatesRoleName(string input, string expected) | ||
{ | ||
var telemetry = new Mock<ITelemetry>(); | ||
var context = new TelemetryContext(); | ||
|
||
context.Cloud.RoleName = input; | ||
|
||
telemetry.Setup(t => t.Context).Returns(context); | ||
|
||
var target = new AzureWebAppTelemetryInitializer(); | ||
target.Initialize(telemetry.Object); | ||
|
||
Assert.Equal(expected, context.Cloud.RoleName); | ||
} | ||
} | ||
} |