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

Commit 8a6eb30

Browse files
authored
Added the DeploymentLabelEnricher to be shared between projects. (#322)
1 parent bfe443d commit 8a6eb30

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.DataContracts;
7+
using Microsoft.ApplicationInsights.Extensibility;
8+
9+
namespace NuGet.Services.Logging
10+
{
11+
public class DeploymentLabelEnricher : ITelemetryInitializer
12+
{
13+
private const string DeploymentLabel = "DeploymentLabel";
14+
private readonly string _deploymentLabel;
15+
16+
public DeploymentLabelEnricher(string deploymentLabel)
17+
{
18+
_deploymentLabel = deploymentLabel ?? throw new ArgumentNullException(nameof(deploymentLabel));
19+
}
20+
21+
public void Initialize(ITelemetry telemetry)
22+
{
23+
if (!(telemetry is ISupportProperties itemTelemetry))
24+
{
25+
return;
26+
}
27+
28+
if (itemTelemetry.Properties.ContainsKey(DeploymentLabel))
29+
{
30+
return;
31+
}
32+
33+
itemTelemetry.Properties.Add(DeploymentLabel, _deploymentLabel);
34+
}
35+
}
36+
}

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="DeploymentLabelEnricher.cs" />
5354
<Compile Include="DurationMetric.cs" />
5455
<Compile Include="ExceptionTelemetryProcessor.cs" />
5556
<Compile Include="LoggingSetup.cs" />
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 System.Collections.Generic;
6+
using Microsoft.ApplicationInsights.Channel;
7+
using Microsoft.ApplicationInsights.DataContracts;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace NuGet.Services.Logging.Tests
12+
{
13+
public class DeploymentLabelEnricherTests
14+
{
15+
const string DeploymentLabel = "TestDeploymentLabel";
16+
const string DeploymentLabelPropertyName = "DeploymentLabel";
17+
18+
private DeploymentLabelEnricher Target { get; }
19+
20+
[Fact]
21+
public void ConstructorThrowsWhenDeploymentLabelNull()
22+
{
23+
var ex = Assert.Throws<ArgumentNullException>(() => new DeploymentLabelEnricher(deploymentLabel: null));
24+
Assert.Equal("deploymentLabel", ex.ParamName);
25+
}
26+
27+
[Fact]
28+
public void WorksWithAnyITelemetry()
29+
{
30+
var telemetryMock = new Mock<ITelemetry>();
31+
var ex = Record.Exception(() => Target.Initialize(telemetryMock.Object));
32+
Assert.Null(ex);
33+
}
34+
35+
[Fact]
36+
public void DoesNotOverwriteExistingLabel()
37+
{
38+
var telemetryMock = new Mock<ITelemetry>();
39+
var propertiesMock = new Mock<IDictionary<string, string>>();
40+
telemetryMock
41+
.As<ISupportProperties>()
42+
.SetupGet(sp => sp.Properties)
43+
.Returns(propertiesMock.Object);
44+
propertiesMock
45+
.Setup(p => p.ContainsKey(DeploymentLabelPropertyName))
46+
.Returns(true);
47+
Target.Initialize(telemetryMock.Object);
48+
49+
propertiesMock
50+
.Verify(p => p.ContainsKey(DeploymentLabelPropertyName), Times.Once);
51+
propertiesMock
52+
.VerifyNoOtherCalls();
53+
}
54+
55+
[Fact]
56+
public void StoresDeploymentLabel()
57+
{
58+
var telemetryMock = new Mock<ITelemetry>();
59+
var propertiesMock = new Mock<IDictionary<string, string>>();
60+
telemetryMock
61+
.As<ISupportProperties>()
62+
.SetupGet(sp => sp.Properties)
63+
.Returns(propertiesMock.Object);
64+
propertiesMock
65+
.Setup(p => p.ContainsKey(DeploymentLabelPropertyName))
66+
.Returns(false);
67+
Target.Initialize(telemetryMock.Object);
68+
69+
propertiesMock
70+
.Verify(p => p.ContainsKey(DeploymentLabelPropertyName), Times.Once);
71+
propertiesMock
72+
.Verify(p => p.Add(DeploymentLabelPropertyName, DeploymentLabel), Times.Once);
73+
}
74+
75+
public DeploymentLabelEnricherTests()
76+
{
77+
Target = new DeploymentLabelEnricher(DeploymentLabel);
78+
}
79+
}
80+
}

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="DeploymentLabelEnricherTests.cs" />
5758
<Compile Include="ExceptionTelemetryProcessorTests.cs" />
5859
<Compile Include="LoggingSetupTests.cs" />
5960
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)