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

Commit

Permalink
Moved DeploymentIdTelemetryEnricher to NuGet.Services.Logging (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierdecoster authored Jan 6, 2020
1 parent 14bc609 commit 6333cbd
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/NuGet.Services.Logging/NuGet.Services.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@
<ItemGroup>
<Compile Include="ApplicationInsights.cs" />
<Compile Include="ApplicationInsightsConfiguration.cs" />
<Compile Include="DeploymentLabelEnricher.cs" />
<Compile Include="TelemetryInitializers\DeploymentIdTelemetryEnricher.cs" />
<Compile Include="TelemetryInitializers\DeploymentLabelEnricher.cs" />
<Compile Include="DurationMetric.cs" />
<Compile Include="ExceptionTelemetryProcessor.cs" />
<Compile Include="TelemetryProcessors\ExceptionTelemetryProcessor.cs" />
<Compile Include="Extensions\DiagnosticsTelemetryModuleExtensions.cs" />
<Compile Include="LoggingSetup.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.*.cs" />
<Compile Include="RequestTelemetryProcessor.cs" />
<Compile Include="TelemetryProcessors\RequestTelemetryProcessor.cs" />
<Compile Include="Extensions\TelemetryClientExtensions.cs" />
<Compile Include="TelemetryClientWrapper.cs" />
<Compile Include="TelemetryContextInitializer.cs" />
<Compile Include="TelemetryInitializers\SupportPropertiesTelemetryInitializer.cs" />
<Compile Include="TelemetryInitializers\TelemetryContextInitializer.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MicroBuild.Core">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

namespace NuGet.Services.Logging
{
public class DeploymentIdTelemetryEnricher : SupportPropertiesTelemetryInitializer
{
public DeploymentIdTelemetryEnricher(string deploymentId)
: base("CloudDeploymentId", deploymentId)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

namespace NuGet.Services.Logging
{
public class DeploymentLabelEnricher : SupportPropertiesTelemetryInitializer
{
public DeploymentLabelEnricher(string deploymentLabel)
: base("DeploymentLabel", deploymentLabel)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@

namespace NuGet.Services.Logging
{
public class DeploymentLabelEnricher : ITelemetryInitializer
public abstract class SupportPropertiesTelemetryInitializer
: ITelemetryInitializer
{
private const string DeploymentLabel = "DeploymentLabel";
private readonly string _deploymentLabel;

public DeploymentLabelEnricher(string deploymentLabel)
protected SupportPropertiesTelemetryInitializer(string propertyName, string propertyValue)
{
_deploymentLabel = deploymentLabel ?? throw new ArgumentNullException(nameof(deploymentLabel));
PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
PropertyValue = propertyValue ?? throw new ArgumentNullException(nameof(propertyValue));
}

public string PropertyName { get; }
public string PropertyValue { get; }

public void Initialize(ITelemetry telemetry)
{
if (!(telemetry is ISupportProperties itemTelemetry))
{
return;
}

if (itemTelemetry.Properties.ContainsKey(DeploymentLabel))
if (itemTelemetry.Properties.ContainsKey(PropertyName))
{
return;
}

itemTelemetry.Properties.Add(DeploymentLabel, _deploymentLabel);
itemTelemetry.Properties.Add(PropertyName, PropertyValue);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DeploymentLabelEnricherTests
public void ConstructorThrowsWhenDeploymentLabelNull()
{
var ex = Assert.Throws<ArgumentNullException>(() => new DeploymentLabelEnricher(deploymentLabel: null));
Assert.Equal("deploymentLabel", ex.ParamName);
Assert.Equal("propertyValue", ex.ParamName);
}

[Fact]
Expand Down

0 comments on commit 6333cbd

Please sign in to comment.