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

Commit f14515b

Browse files
Move and consolidate TelemetryInitializers from NuGet.Jobs to NuGet.Services.Logging (#329)
1 parent 6333cbd commit f14515b

7 files changed

Lines changed: 195 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="TelemetryInitializers\DeploymentIdTelemetryEnricher.cs" />
5454
<Compile Include="TelemetryInitializers\DeploymentLabelEnricher.cs" />
5555
<Compile Include="DurationMetric.cs" />
56+
<Compile Include="TelemetryInitializers\JobPropertiesTelemetryInitializer.cs" />
5657
<Compile Include="TelemetryProcessors\ExceptionTelemetryProcessor.cs" />
5758
<Compile Include="Extensions\DiagnosticsTelemetryModuleExtensions.cs" />
5859
<Compile Include="LoggingSetup.cs" />
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Microsoft.ApplicationInsights.Extensibility;
9+
10+
namespace NuGet.Services.Logging
11+
{
12+
public class JobPropertiesTelemetryInitializer
13+
: ITelemetryInitializer
14+
{
15+
private readonly string _jobName;
16+
private readonly string _instanceName;
17+
private readonly IDictionary<string, string> _globalDimensions;
18+
19+
public JobPropertiesTelemetryInitializer(
20+
string jobName,
21+
string instanceName,
22+
IDictionary<string, string> globalDimensions)
23+
{
24+
_jobName = jobName ?? throw new ArgumentNullException(nameof(jobName));
25+
_instanceName = instanceName ?? throw new ArgumentNullException(nameof(instanceName));
26+
_globalDimensions = globalDimensions ?? throw new ArgumentNullException(nameof(globalDimensions));
27+
}
28+
29+
public void Initialize(ITelemetry telemetry)
30+
{
31+
// We need to cast to ISupportProperties to avoid using the deprecated telemetry.Context.Properties API.
32+
// https://github.com/Microsoft/ApplicationInsights-Home/issues/300
33+
if (!(telemetry is ISupportProperties itemTelemetry))
34+
{
35+
return;
36+
}
37+
38+
itemTelemetry.Properties["JobName"] = _jobName;
39+
itemTelemetry.Properties["InstanceName"] = _instanceName;
40+
41+
foreach (var dimension in _globalDimensions)
42+
{
43+
itemTelemetry.Properties[dimension.Key] = dimension.Value;
44+
}
45+
}
46+
}
47+
}

src/NuGet.Services.Logging/TelemetryInitializers/SupportPropertiesTelemetryInitializer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ protected SupportPropertiesTelemetryInitializer(string propertyName, string prop
2222

2323
public void Initialize(ITelemetry telemetry)
2424
{
25+
// We need to cast to ISupportProperties to avoid using the deprecated telemetry.Context.Properties API.
26+
// https://github.com/Microsoft/ApplicationInsights-Home/issues/300
2527
if (!(telemetry is ISupportProperties itemTelemetry))
2628
{
2729
return;
2830
}
2931

32+
// Note that telemetry initializers can be called multiple times for the same telemetry item.
33+
// https://github.com/microsoft/ApplicationInsights-dotnet-server/issues/977
3034
if (itemTelemetry.Properties.ContainsKey(PropertyName))
3135
{
3236
return;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
</ItemGroup>
5555
<ItemGroup>
5656
<Compile Include="ApplicationInsightsTests.cs" />
57-
<Compile Include="DeploymentLabelEnricherTests.cs" />
57+
<Compile Include="TelemetryInitializers\DeploymentLabelEnricherTests.cs" />
5858
<Compile Include="Extensions\DiagnosticsTelemetryModuleExtensionsTests.cs" />
5959
<Compile Include="ExceptionTelemetryProcessorTests.cs" />
6060
<Compile Include="LoggingSetupTests.cs" />
6161
<Compile Include="Properties\AssemblyInfo.cs" />
6262
<Compile Include="RequestTelemetryProcessorTests.cs" />
6363
<Compile Include="Extensions\TelemetryClientExtensionsTests.cs" />
64+
<Compile Include="TelemetryInitializers\JobPropertiesTelemetryInitializerTests.cs" />
65+
<Compile Include="TestableTelemetry.cs" />
6466
<Compile Include="TelemetryProcessorTest.cs" />
6567
</ItemGroup>
6668
<ItemGroup>

tests/NuGet.Services.Logging.Tests/DeploymentLabelEnricherTests.cs renamed to tests/NuGet.Services.Logging.Tests/TelemetryInitializers/DeploymentLabelEnricherTests.cs

File renamed without changes.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 JobPropertiesTelemetryInitializerTests
14+
{
15+
private readonly TelemetryContext _telemetryContext = new TelemetryContext();
16+
private readonly Mock<ITelemetry> _telemetry = new Mock<ITelemetry>();
17+
18+
public JobPropertiesTelemetryInitializerTests()
19+
{
20+
_telemetry.SetupGet(x => x.Context)
21+
.Returns(_telemetryContext);
22+
}
23+
24+
[Fact]
25+
public void Constructor_WhenJobNameIsNull_Throws()
26+
{
27+
var exception = Assert.Throws<ArgumentNullException>(
28+
() => new JobPropertiesTelemetryInitializer(
29+
jobName: null,
30+
instanceName: "instanceName",
31+
globalDimensions: new Dictionary<string, string>()));
32+
33+
Assert.Equal("jobName", exception.ParamName);
34+
}
35+
36+
[Fact]
37+
public void Constructor_WhenInstanceNameIsNull_Throws()
38+
{
39+
var exception = Assert.Throws<ArgumentNullException>(
40+
() => new JobPropertiesTelemetryInitializer(
41+
jobName: "jobName",
42+
instanceName: null,
43+
globalDimensions: new Dictionary<string, string>()));
44+
45+
Assert.Equal("instanceName", exception.ParamName);
46+
}
47+
48+
[Fact]
49+
public void Constructor_WhenGlobalDimensionsIsNull_Throws()
50+
{
51+
var exception = Assert.Throws<ArgumentNullException>(
52+
() => new JobPropertiesTelemetryInitializer(
53+
jobName: "jobName",
54+
instanceName: "instanceName",
55+
globalDimensions: null));
56+
57+
Assert.Equal("globalDimensions", exception.ParamName);
58+
}
59+
60+
[Fact]
61+
public void Initialize_WhenGlobalDimensionsIsEmpty_SetsJobNameAndInstanceName()
62+
{
63+
var telemetry = new TestableTelemetry();
64+
65+
var initializer = new JobPropertiesTelemetryInitializer(
66+
jobName: "a",
67+
instanceName: "b",
68+
globalDimensions: new Dictionary<string, string>());
69+
70+
initializer.Initialize(telemetry);
71+
72+
Assert.Equal(2, telemetry.Properties.Count);
73+
Assert.Equal("a", telemetry.Properties["JobName"]);
74+
Assert.Equal("b", telemetry.Properties["InstanceName"]);
75+
}
76+
77+
[Fact]
78+
public void Initialize_WhenGlobalDimensionsIsNotEmpty_SetsTelemetry()
79+
{
80+
var globalDimensions = new Dictionary<string, string>()
81+
{
82+
{ "a", "b" },
83+
{ "c", "d" }
84+
};
85+
var telemetry = new TestableTelemetry();
86+
87+
var initializer = new JobPropertiesTelemetryInitializer(
88+
jobName: "jobName",
89+
instanceName: "instanceName",
90+
globalDimensions: globalDimensions);
91+
92+
initializer.Initialize(telemetry);
93+
94+
Assert.Equal(4, telemetry.Properties.Count);
95+
Assert.Equal("jobName", telemetry.Properties["JobName"]);
96+
Assert.Equal("instanceName", telemetry.Properties["InstanceName"]);
97+
Assert.Equal("b", telemetry.Properties["a"]);
98+
Assert.Equal("d", telemetry.Properties["c"]);
99+
}
100+
}
101+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 Microsoft.ApplicationInsights.Extensibility;
9+
10+
namespace NuGet.Services.Logging.Tests
11+
{
12+
public class TestableTelemetry : ITelemetry, ISupportProperties
13+
{
14+
public DateTimeOffset Timestamp { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
15+
16+
public TelemetryContext Context => throw new NotImplementedException();
17+
18+
public IExtension Extension { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
19+
20+
public string Sequence { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
21+
22+
public IDictionary<string, string> Properties { get; } = new Dictionary<string, string>();
23+
24+
public ITelemetry DeepClone()
25+
{
26+
throw new NotImplementedException();
27+
}
28+
29+
public void Sanitize()
30+
{
31+
throw new NotImplementedException();
32+
}
33+
34+
public void SerializeData(ISerializationWriter serializationWriter)
35+
{
36+
throw new NotImplementedException();
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)