Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport to 5.5] Update Monitoring to support ingesting batched metrics messages with no errors (#4340) #4342

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<PackageVersion Include="Microsoft-WindowsAPICodePack-Shell" Version="1.1.5" />
<PackageVersion Include="Mindscape.Raygun4Net.NetCore" Version="11.0.2" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.11" />
<PackageVersion Include="NServiceBus" Version="9.1.0" />
<PackageVersion Include="NServiceBus.AcceptanceTesting" Version="9.1.0" />
<PackageVersion Include="NServiceBus" Version="9.1.1" />
<PackageVersion Include="NServiceBus.AcceptanceTesting" Version="9.1.1" />
<PackageVersion Include="NServiceBus.AmazonSQS" Version="7.0.1" />
<PackageVersion Include="NServiceBus.CustomChecks" Version="5.0.0" />
<PackageVersion Include="NServiceBus.Extensions.Hosting" Version="3.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace ServiceControl.Monitoring.AcceptanceTests.Tests
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AcceptanceTesting;
using AcceptanceTesting.EndpointTemplates;
using Http.Diagrams;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.Pipeline;
using NUnit.Framework;

class When_ingesting_multiple_metrics_messages : AcceptanceTest
{
[Test]
public async Task Should_not_fail()
{
CustomConfiguration = endpointConfiguration =>
{
endpointConfiguration.Pipeline.Register(typeof(InterceptIngestionBehavior),
"Intercepts ingestion exceptions");
};

var metricReported = false;

var ctx = await Define<Context>()
.WithEndpoint<EndpointWithTimings>(c => c.When(async s =>
{
var tasks = new List<Task>();
for (int i = 0; i < 100; i++)
{
tasks.Add(s.SendLocal(new SampleMessage()));
tasks.Add(s.SendLocal(new AnotherSampleMessage()));
}

await Task.WhenAll(tasks);
}))
.Done(async c =>
{
var result = await this.TryGetMany<MonitoredEndpoint>("/monitored-endpoints?history=1");

metricReported = result.HasResult && result.Items[0].Metrics.TryGetValue("processingTime", out var processingTime) && processingTime?.Average > 0;

return metricReported;
})
.Run();

Assert.IsTrue(metricReported);
Assert.IsEmpty(ctx.Errors);
}

class EndpointWithTimings : EndpointConfigurationBuilder
{
public EndpointWithTimings() =>
EndpointSetup<DefaultServerWithoutAudit>(c =>
{
c.EnableMetrics().SendMetricDataToServiceControl(Settings.DEFAULT_INSTANCE_NAME, TimeSpan.FromSeconds(5));
});

class Handler : IHandleMessages<SampleMessage>
{
public Task Handle(SampleMessage message, IMessageHandlerContext context)
=> Task.Delay(TimeSpan.FromMilliseconds(10), context.CancellationToken);
}

class AnotherHandler : IHandleMessages<AnotherSampleMessage>
{
public Task Handle(AnotherSampleMessage message, IMessageHandlerContext context)
=> Task.Delay(TimeSpan.FromMilliseconds(10), context.CancellationToken);
}
}

class InterceptIngestionBehavior(ScenarioContext scenarioContext) : Behavior<IIncomingPhysicalMessageContext>
{
public override async Task Invoke(IIncomingPhysicalMessageContext context, Func<Task> next)
{
try
{
await next();
}
catch (Exception e)
{
((Context)scenarioContext).Errors.Add(e);
throw;
}
}
}

class MonitoringEndpoint : EndpointConfigurationBuilder
{
public MonitoringEndpoint() => EndpointSetup<DefaultServerWithoutAudit>();
}

class Context : ScenarioContext
{
public List<Exception> Errors { get; set; } = [];
}

class SampleMessage : SampleBaseMessage;

class AnotherSampleMessage : SampleBaseMessage;

class SampleBaseMessage : IMessage;
}
}