Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
/ ServerCommon Public archive

Port AzureWebAppTelemetryInitializer #332

Merged
merged 2 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/NuGet.Services.Logging/NuGet.Services.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<ItemGroup>
<Compile Include="ApplicationInsights.cs" />
<Compile Include="ApplicationInsightsConfiguration.cs" />
<Compile Include="TelemetryInitializers\AzureWebAppTelemetryInitializer.cs" />
<Compile Include="TelemetryInitializers\DeploymentIdTelemetryEnricher.cs" />
<Compile Include="TelemetryInitializers\DeploymentLabelEnricher.cs" />
<Compile Include="DurationMetric.cs" />
Expand Down
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationInsightsTests.cs" />
<Compile Include="TelemetryInitializers\AzureWebAppTelemetryInitializerFacts.cs" />
<Compile Include="TelemetryInitializers\DeploymentLabelEnricherTests.cs" />
<Compile Include="Extensions\DiagnosticsTelemetryModuleExtensionsTests.cs" />
<Compile Include="ExceptionTelemetryProcessorTests.cs" />
Expand Down
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);
}
}
}