Skip to content

Commit

Permalink
Merge pull request #76 from Particular/gatekeeping-backport-2
Browse files Browse the repository at this point in the history
Fix for plugin not sending metric data to ServiceControl (2.1)
  • Loading branch information
WilliamBZA authored Nov 20, 2019
2 parents 2d93165 + 3bbfd5d commit e0314c2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static void SendMetricDataToServiceControl(this MetricsOptions options, s
reporting.ServiceControlMetricsAddress = serviceControlMetricsAddress;
reporting.ServiceControlReportingInterval = interval;
reporting.EndpointInstanceIdOverride = instanceId;

options.RegisterObservers(context => reporting.CreateReporters());
}

/// <summary>
Expand Down
17 changes: 10 additions & 7 deletions src/NServiceBus.Metrics.ServiceControl/ReportingFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,18 @@ public ServiceControlRawDataReporting(IBuilder builder, ReportingOptions options

protected override Task OnStart(IMessageSession session)
{
foreach (var metric in metrics)
options.OnCreateReporters(() =>
{
reporters.Add(CreateReporter(metric.Key, metric.Value.Item1, metric.Value.Item2));
}
foreach (var metric in metrics)
{
reporters.Add(CreateReporter(metric.Key, metric.Value.Item1, metric.Value.Item2));
}

foreach (var reporter in reporters)
{
reporter.Start();
}
foreach (var reporter in reporters)
{
reporter.Start();
}
});

return Task.FromResult(0);
}
Expand Down
29 changes: 29 additions & 0 deletions src/NServiceBus.Metrics.ServiceControl/ReportingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ReportingOptions
internal TimeSpan ServiceControlReportingInterval;
internal string EndpointInstanceIdOverride;
public TimeSpan TimeToBeReceived { get; set; } = TimeSpan.FromDays(7);
Action createMetricReporters;
bool createReportersCalled;

internal bool TryGetValidEndpointInstanceIdOverride(out string instanceId)
{
Expand All @@ -24,5 +26,32 @@ internal bool TryGetValidEndpointInstanceIdOverride(out string instanceId)
}

public static ReportingOptions Get(MetricsOptions options) => reporting.GetOrAdd(options, metricsOptions => new ReportingOptions());

internal void CreateReporters()
{
if(createReportersCalled)
{
throw new Exception("CreateReporters has already been called, and can only be called once.");
}

createReportersCalled = true;
if(createMetricReporters != null)
{
createMetricReporters();
createMetricReporters = null;
}
}

internal void OnCreateReporters(Action createMetricReporters)
{
if(createReportersCalled)
{
createMetricReporters();
}
else
{
this.createMetricReporters = createMetricReporters;
}
}
}
}

0 comments on commit e0314c2

Please sign in to comment.