Skip to content

Commit 9697b48

Browse files
committed
Add deployment ID value to Application Insights telemetry (#7247)
Address #7250
1 parent 255919b commit 9697b48

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

src/NuGetGallery/App_Start/OwinStartup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static void Configuration(IAppBuilder app)
8282
TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
8383

8484
// Add enrichers
85+
TelemetryConfiguration.Active.TelemetryInitializers.Add(new DeploymentIdTelemetryEnricher());
8586
TelemetryConfiguration.Active.TelemetryInitializers.Add(new ClientInformationTelemetryEnricher());
8687

8788
var telemetryProcessorChainBuilder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@
568568
<Compile Include="Services\ValidationService.cs" />
569569
<Compile Include="Telemetry\ClientInformationTelemetryEnricher.cs" />
570570
<Compile Include="Telemetry\ClientTelemetryPIIProcessor.cs" />
571+
<Compile Include="Telemetry\DeploymentIdTelemetryEnricher.cs" />
571572
<Compile Include="Telemetry\QuietExceptionLogger.cs" />
572573
<Compile Include="Infrastructure\Authentication\CredentialValidator.cs" />
573574
<Compile Include="Infrastructure\Authentication\ICredentialValidator.cs" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
using Microsoft.WindowsAzure.ServiceRuntime;
8+
9+
namespace NuGetGallery
10+
{
11+
public class DeploymentIdTelemetryEnricher : ITelemetryInitializer
12+
{
13+
private const string PropertyKey = "CloudDeploymentId";
14+
15+
private static readonly Lazy<string> LazyDeploymentId = new Lazy<string>(() =>
16+
{
17+
try
18+
{
19+
if (RoleEnvironment.IsAvailable)
20+
{
21+
return RoleEnvironment.DeploymentId;
22+
}
23+
}
24+
catch
25+
{
26+
// This likely means the cloud service runtime is not available.
27+
}
28+
29+
return null;
30+
});
31+
32+
internal virtual string DeploymentId => LazyDeploymentId.Value;
33+
34+
public void Initialize(ITelemetry telemetry)
35+
{
36+
if (telemetry == null
37+
|| DeploymentId == null
38+
|| telemetry.Context.Properties.ContainsKey(PropertyKey))
39+
{
40+
return;
41+
}
42+
43+
telemetry.Context.Properties.Add(PropertyKey, DeploymentId);
44+
}
45+
}
46+
}

tests/NuGetGallery.Facts/NuGetGallery.Facts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Services\PackageDeprecationServiceFacts.cs" />
9494
<Compile Include="Services\SearchSideBySideServiceFacts.cs" />
9595
<Compile Include="Services\StatusServiceFacts.cs" />
96+
<Compile Include="Telemetry\DeploymentIdTelemetryEnricherFacts.cs" />
9697
<Compile Include="TestData\TestDataResourceUtility.cs" />
9798
<Compile Include="UsernameValidationRegex.cs" />
9899
<Compile Include="Extensions\NumberExtensionsFacts.cs" />
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 NuGetGallery
10+
{
11+
public class DeploymentIdTelemetryEnricherFacts
12+
{
13+
private const string PropertyKey = "CloudDeploymentId";
14+
15+
public DeploymentIdTelemetryEnricherFacts()
16+
{
17+
Telemetry = new Mock<ITelemetry>();
18+
Telemetry.Setup(x => x.Context).Returns(new TelemetryContext());
19+
Target = new Mock<TestableDeploymentIdTelemetryEnricher>();
20+
Target.Object.SetDeploymentId("TheDeploymentId");
21+
}
22+
23+
public Mock<ITelemetry> Telemetry { get; }
24+
public Mock<TestableDeploymentIdTelemetryEnricher> Target { get; }
25+
26+
[Fact]
27+
public void DoesNothingWithNullTelemetry()
28+
{
29+
Target.Object.Initialize(telemetry: null);
30+
31+
Target.Verify(x => x.DeploymentId, Times.Never);
32+
Assert.DoesNotContain(PropertyKey, Telemetry.Object.Context.Properties.Keys);
33+
}
34+
35+
[Fact]
36+
public void DoesNothingWhenDeploymentIdIsNull()
37+
{
38+
Target.Object.SetDeploymentId(null);
39+
40+
Target.Object.Initialize(Telemetry.Object);
41+
42+
Target.Verify(x => x.DeploymentId, Times.Never);
43+
Assert.DoesNotContain(PropertyKey, Telemetry.Object.Context.Properties.Keys);
44+
}
45+
46+
[Fact]
47+
public void DoesNothingWhenDeploymentIdIsAlreadySet()
48+
{
49+
Telemetry.Object.Context.Properties[PropertyKey] = "something else";
50+
51+
Target.Object.Initialize(Telemetry.Object);
52+
53+
Target.Verify(x => x.DeploymentId, Times.Never);
54+
Assert.Equal(Telemetry.Object.Context.Properties[PropertyKey], "something else");
55+
}
56+
57+
[Fact]
58+
public void SetsDeploymentId()
59+
{
60+
Target.Object.Initialize(Telemetry.Object);
61+
62+
Assert.Equal(Telemetry.Object.Context.Properties[PropertyKey], "TheDeploymentId");
63+
}
64+
65+
public class TestableDeploymentIdTelemetryEnricher : DeploymentIdTelemetryEnricher
66+
{
67+
private string _deploymentId;
68+
69+
public void SetDeploymentId(string deploymentId)
70+
{
71+
_deploymentId = deploymentId;
72+
}
73+
74+
internal override string DeploymentId => _deploymentId;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)