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

fix(otel): Avoid concurrency issues when reporting metrics #2239

Merged
merged 6 commits into from
Mar 4, 2023
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
tomkerkhove marked this conversation as resolved.
Show resolved Hide resolved
using System.Threading.Tasks;
using GuardNet;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -46,9 +47,9 @@ public async Task ReportMetricAsync(string metricName, string metricDescription,
}

private readonly ConcurrentDictionary<string, ObservableGauge<double>> _gauges = new ConcurrentDictionary<string, ObservableGauge<double>>();
private readonly ConcurrentDictionary<string, HashSet<Measurement<double>>> _measurements = new ConcurrentDictionary<string, HashSet<Measurement<double>>>();
private readonly ConcurrentDictionary<string, Channel<Measurement<double>>> _measurements = new ConcurrentDictionary<string, Channel<Measurement<double>>>();

public Task ReportMetricAsync(string metricName, string metricDescription, double metricValue, Dictionary<string, string> labels)
public async Task ReportMetricAsync(string metricName, string metricDescription, double metricValue, Dictionary<string, string> labels)
{
Guard.NotNullOrEmpty(metricName, nameof(metricName));

Expand All @@ -60,28 +61,41 @@ public Task ReportMetricAsync(string metricName, string metricDescription, doubl

var composedTags = labels.Select(kvp => new KeyValuePair<string, object?>(kvp.Key, kvp.Value)).ToArray();
var newMeasurement = new Measurement<double>(metricValue, composedTags);
_measurements[metricName].Add(newMeasurement);

var channelWriter = _measurements[metricName].Writer;
await channelWriter.WriteAsync(newMeasurement);

_logger.LogTrace("Metric {MetricName} with value {MetricValue} was pushed to OpenTelemetry Collector", metricName, metricValue);

return Task.CompletedTask;
}

private void InitializeNewMetric(string metricName, string metricDescription)
{
var gauge = azureMonitorMeter.CreateObservableGauge<double>(metricName, description: metricDescription, observeValues: () => ReportMeasurementsForMetric(metricName));
var gauge = azureMonitorMeter.CreateObservableGauge<double>(metricName, description: metricDescription, observeValues: () => ReportMeasurementsForMetricAsync(metricName).Result);
_gauges.TryAdd(metricName, gauge);

_measurements.TryAdd(metricName, new HashSet<Measurement<double>>());
_measurements.TryAdd(metricName, CreateNewMeasurementChannel());
}

private IEnumerable<Measurement<double>> ReportMeasurementsForMetric(string metricName)
private async Task<IEnumerable<Measurement<double>>> ReportMeasurementsForMetricAsync(string metricName)
{
var recordedMeasurements = _measurements[metricName];

var measurementsToReport = Interlocked.Exchange(ref recordedMeasurements, new HashSet<Measurement<double>>());
List<Measurement<double>> measurementsToReport = new List<Measurement<double>>();
var channel = _measurements[metricName];

var totalCount = channel.Reader.Count;
var readItems = 0;
do
{
var item = await channel.Reader.ReadAsync();
measurementsToReport.Add(item);
readItems++;
}
while (readItems < totalCount);

return measurementsToReport;
}

static Channel<Measurement<double>> CreateNewMeasurementChannel()
{
return Channel.CreateUnbounded<Measurement<double>>();
}
}
}