Skip to content

Commit

Permalink
Merge pull request #26 from allegro/feature/application-insights-exte…
Browse files Browse the repository at this point in the history
…nsions

feat: Application Insights Extensions with many features
  • Loading branch information
darthkurak authored Sep 28, 2023
2 parents 453fcba + 8357fde commit c0d6c67
Show file tree
Hide file tree
Showing 54 changed files with 3,419 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/Allegro.Extensions.ApplicationInsights.ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build Allegro.Extensions.ApplicationInsights

on:
push:
branches: [ main ]
paths:
- 'src/Allegro.Extensions.ApplicationInsights/**'
- 'src/*'
pull_request:
branches: [ main ]
paths:
- 'src/Allegro.Extensions.ApplicationInsights/**'
- 'src/*'

jobs:
ci1:
uses: ./.github/workflows/template.yml
with:
projectName: Allegro.Extensions.ApplicationInsights.AspNetCore
solutionName: Allegro.Extensions.ApplicationInsights
ci2:
uses: ./.github/workflows/template.yml
with:
projectName: Allegro.Extensions.ApplicationInsights.Prometheus
solutionName: Allegro.Extensions.ApplicationInsights
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Publish Allegro.Extensions.ApplicationInsights

on:
push:
tags:
- 'Allegro.Extensions.ApplicationInsights_*'

jobs:
publish1:
uses: ./.github/workflows/template.yml
with:
projectName: Allegro.Extensions.ApplicationInsights.AspNetCore
solutionName: Allegro.Extensions.ApplicationInsights
publish: true
tagName: ${{ github.ref_name }}
secrets:
nugetCertificate: ${{ secrets.NUGET_PRIVATE_KEY_P12 }}
nugetCertificatePassword: ${{ secrets.GPG_PRIVATE_KEY_PASSWORD }}
nugetApiKey: ${{ secrets.NUGET_API_KEY }}
publish2:
uses: ./.github/workflows/template.yml
with:
projectName: Allegro.Extensions.ApplicationInsights.Prometheus
solutionName: Allegro.Extensions.ApplicationInsights
publish: true
tagName: ${{ github.ref_name }}
secrets:
nugetCertificate: ${{ secrets.NUGET_PRIVATE_KEY_P12 }}
nugetCertificatePassword: ${{ secrets.GPG_PRIVATE_KEY_PASSWORD }}
nugetApiKey: ${{ secrets.NUGET_API_KEY }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Please refer to [Contributing guideline](CONTRIBUTING.md) for details.

- [Allegro.Extensions.AspNetCore](src/Allegro.Extensions.AspNetCore)
This library contains useful extensions and utilities for ASP.NET Core based projects.
- [Allegro.Extensions.ApplicationInsights](src/Allegro.Extensions.ApplicationInsights)
This library contains useful extensions for Azure Application Insights SDK
- [Allegro.Extensions.Cqrs](src/Allegro.Extensions.Cqrs)
Contains implementation of common tools to support Cqrs pattern based on Handlers approach.
- [Allegro.Extensions.Dapper](src/Allegro.Extensions.Dapper)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.{cs,fs}]
dotnet_diagnostic.CS1591.severity = none
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
using System.Diagnostics.CodeAnalysis;
using FluentAssertions;
using Microsoft.ApplicationInsights.Extensibility;
using Moq;

namespace Allegro.Extensions.ApplicationInsights.AspNetCore.UnitTests;

[SuppressMessage("CSharp Extensions", "CSE001:Required properties initialization", Justification = "SampleConfig is perfectly fine to init with default constructor")]
public class AdaptiveSamplingConfigurationTests
{
[Fact]
public void WhenConfigIsDefaultShouldReturnTelemetryProcessorWithConfigDefaultValues()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();

var samplingTelemetryProcessor = ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(
telemetryProcessorMock.Object,
new SamplingConfig());

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenEvaluationIntervalIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig() { EvaluationInterval = TimeSpan.FromSeconds(5) }
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(5));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenExcludedTypesIsChanged_ShouldChangeTelemetryProcessorValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig() { SamplingExcludedTypes = "Dependency" };

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Dependency");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenInitialSamplingPercentageIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig() { InitialSamplingPercentage = 99 }
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(99);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenMaxSamplingPercentageIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig() { MaxSamplingPercentage = 99 }
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(99);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenMinSamplingPercentageIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig() { MinSamplingPercentage = 0.01 }
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.01);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenMovingAverageRatioIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig() { MovingAverageRatio = 0.1 }
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.1);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenMaxTelemetryItemsPerSecondIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig() { MaxTelemetryItemsPerSecond = 3 }
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(3);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenSamplingPercentageDecreaseTimeoutIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig()
{
SamplingPercentageDecreaseTimeout = TimeSpan.FromMinutes(1)
}
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(1));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(15));
}

[Fact]
public void WhenSamplingPercentageIncreaseTimeoutIsNotNull_ShouldOverwriteTelemetryProcessorDefaultValue()
{
var telemetryProcessorMock = new Mock<ITelemetryProcessor>();
var config = new SamplingConfig()
{
AdaptiveSamplingConfig = new AdaptiveSamplingConfig()
{
SamplingPercentageIncreaseTimeout = TimeSpan.FromMinutes(5)
}
};

var samplingTelemetryProcessor =
ApplicationInsightsExtensions.CreateAdaptiveSamplingProcessor(telemetryProcessorMock.Object, config);

samplingTelemetryProcessor.EvaluationInterval.Should().Be(TimeSpan.FromSeconds(15));
samplingTelemetryProcessor.ExcludedTypes.Should().Be("Event;Exception;Trace;PageView");
samplingTelemetryProcessor.IncludedTypes.Should().BeNull();
samplingTelemetryProcessor.InitialSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MaxSamplingPercentage.Should().Be(100);
samplingTelemetryProcessor.MinSamplingPercentage.Should().Be(0.1);
samplingTelemetryProcessor.MovingAverageRatio.Should().Be(0.25);
samplingTelemetryProcessor.MaxTelemetryItemsPerSecond.Should().Be(5);
samplingTelemetryProcessor.SamplingPercentageDecreaseTimeout.Should().Be(TimeSpan.FromMinutes(2));
samplingTelemetryProcessor.SamplingPercentageIncreaseTimeout.Should().Be(TimeSpan.FromMinutes(5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Update="Meziantou.Analyzer" Version="2.0.85">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Update="SmartAnalyzers.CSharpExtensions.Annotations" Version="4.2.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; compile; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<ProjectReference Include="..\Allegro.Extensions.ApplicationInsights.AspNetCore\Allegro.Extensions.ApplicationInsights.AspNetCore.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit c0d6c67

Please sign in to comment.