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

Added wildcard support for meter sources. #2459

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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
47 changes: 42 additions & 5 deletions src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text.RegularExpressions;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;

Expand Down Expand Up @@ -87,10 +88,40 @@ internal MeterProviderSdk(
}

// Setup Listener
var meterSourcesToSubscribe = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
foreach (var name in meterSources)
var wildcardMode = false;
Regex regex = null;
Func<Instrument, bool> shouldListenTo = null;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
if (meterSources.Any())
{
meterSourcesToSubscribe[name] = true;
foreach (var meterSource in meterSources)
{
if (meterSource.Contains('*'))
{
wildcardMode = true;

shouldListenTo = instrument =>
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
return regex.IsMatch(instrument.Meter.Name);
};

regex = GetWildcardRegex(meterSources);
break;
}
}

if (!wildcardMode)
{
var meterSourcesToSubscribe = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var meterSource in meterSources)
{
meterSourcesToSubscribe.Add(meterSource);
}

shouldListenTo = instrument =>
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
return meterSourcesToSubscribe.Contains(instrument.Meter.Name);
};
}
}

this.listener = new MeterListener();
Expand All @@ -99,7 +130,7 @@ internal MeterProviderSdk(
{
this.listener.InstrumentPublished = (instrument, listener) =>
{
if (meterSourcesToSubscribe.ContainsKey(instrument.Meter.Name))
if (shouldListenTo(instrument))
{
// Creating list with initial capacity as the maximum
// possible size, to avoid any array resize/copy internally.
Expand Down Expand Up @@ -197,7 +228,7 @@ internal MeterProviderSdk(
{
this.listener.InstrumentPublished = (instrument, listener) =>
{
if (meterSourcesToSubscribe.ContainsKey(instrument.Meter.Name))
if (shouldListenTo(instrument))
{
var metricName = instrument.Name;
Metric metric = null;
Expand Down Expand Up @@ -243,6 +274,12 @@ internal MeterProviderSdk(

this.listener.MeasurementsCompleted = (instrument, state) => this.MeasurementsCompleted(instrument, state);
this.listener.Start();

static Regex GetWildcardRegex(IEnumerable<string> collection)
{
var pattern = '^' + string.Join("|", from name in collection select "(?:" + Regex.Escape(name).Replace("\\*", ".*") + ')') + '$';
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
}

internal Resource Resource { get; }
Expand Down
176 changes: 176 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,182 @@ void ProcessExport(Batch<Metric> batch)
Assert.Equal(1, metricCount);
}

#pragma warning disable SA1201 // Elements should appear in the correct order
public static IEnumerable<object[]> ViewConfigs =>
#pragma warning restore SA1201 // Elements should appear in the correct order
new List<object[]>
{
new object[] { "myGauge1", "newName" },
};

[Theory]
[MemberData(nameof(ViewConfigs))]
public void MeterSourcesWildcardSupportMatchTest(string instrumentName, string newName)
{
var meterNames = new[]
{
"AbcCompany.XyzProduct.ComponentA",
"AbcCompany.XyzProduct.ComponentB",
"SomeCompany.SomeProduct.SomeComponent",
};

using var meter1 = new Meter(meterNames[0]);
using var meter2 = new Meter(meterNames[1]);
using var meter3 = new Meter(meterNames[2]);

var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("AbcCompany.XyzProduct.*")
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
.AddInMemoryExporter(exportedItems)
.AddView(instrumentName, newName)
.Build();

var measurement = new Measurement<int>(100, new("name", "apple"), new("color", "red"));
meter1.CreateObservableGauge("myGauge1", () => measurement);
meter2.CreateObservableGauge("myGauge2", () => measurement);
meter3.CreateObservableGauge("myGauge3", () => measurement);

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

// the third element "SomeCompany.SomeProduct.SomeComponent" will not be subscribed.
Assert.True(exportedItems.Count == 2);
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal("newName", exportedItems[0].Name);
Assert.Equal("myGauge2", exportedItems[1].Name);
}

[Theory]
[MemberData(nameof(ViewConfigs))]
public void MeterSourcesWildcardSupportTwoPatterns(string instrumentName, string newName)
{
var meterNames = new[]
{
"AbcCompany.XyzProduct.ComponentA",
"AbcCompany.XyzProduct.ComponentB",
"DefCompany.AbcProduct.ComponentC",
"DefCompany.XyzProduct.ComponentC",
};

using var meter1 = new Meter(meterNames[0]);
using var meter2 = new Meter(meterNames[1]);
using var meter3 = new Meter(meterNames[2]);
using var meter4 = new Meter(meterNames[3]);

var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("AbcCompany.XyzProduct.*")
.AddMeter("DefCompany.*.ComponentC")
.AddInMemoryExporter(exportedItems)
.AddView(instrumentName, newName)
.Build();

var measurement = new Measurement<int>(100, new("name", "apple"), new("color", "red"));
meter1.CreateObservableGauge("myGauge1", () => measurement);
meter2.CreateObservableGauge("myGauge2", () => measurement);
meter3.CreateObservableGauge("myGauge3", () => measurement);
meter4.CreateObservableGauge("myGauge4", () => measurement);

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 4);
Assert.Equal("newName", exportedItems[0].Name);
Assert.Equal("myGauge2", exportedItems[1].Name);
Assert.Equal("myGauge3", exportedItems[2].Name);
Assert.Equal("myGauge4", exportedItems[3].Name);
}

[Theory]
[MemberData(nameof(ViewConfigs))]
public void MeterSourcesWildcardSupportCaseInsensitive(string instrumentName, string newName)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
var meterNames = new[]
{
"abcCompany.xYZProduct.compoNeNta",
"AbcCompany.XyzProduct.ComponentB",
};

using var meter1 = new Meter(meterNames[0]);
using var meter2 = new Meter(meterNames[1]);

var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("AbcCompany.XyzProduct.*")
.AddInMemoryExporter(exportedItems)
.AddView(instrumentName, newName)
.Build();

var measurement = new Measurement<int>(100, new("name", "apple"), new("color", "red"));
meter1.CreateObservableGauge("myGauge1", () => measurement);
meter2.CreateObservableGauge("myGauge2", () => measurement);

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 2);
Assert.Equal("newName", exportedItems[0].Name);
Assert.Equal("myGauge2", exportedItems[1].Name);
}

[Theory]
[MemberData(nameof(ViewConfigs))]
public void MeterSourcesWildcardSupportNoAddMeter(string instrumentName, string newName)
{
var meterNames = new[]
{
"AbcCompany.XyzProduct.ComponentA",
"AbcCompany.XyzProduct.ComponentB",
};

using var meter1 = new Meter(meterNames[0]);
using var meter2 = new Meter(meterNames[1]);

var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddInMemoryExporter(exportedItems)
.AddView(instrumentName, newName)
.Build();

var measurement = new Measurement<int>(100, new("name", "apple"), new("color", "red"));

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 0);
}

[Theory]
[MemberData(nameof(ViewConfigs))]
public void MeterSourcesWildcardSupportMixWithNoWildcardmeter(string instrumentName, string newName)
{
var meterNames = new[]
{
"AbcCompany.XyzProduct.ComponentA",
"AbcCompany.XyzProduct.ComponentB",
"SomeCompany.SomeProduct.SomeComponent",
};

using var meter1 = new Meter(meterNames[0]);
using var meter2 = new Meter(meterNames[1]);
using var meter3 = new Meter(meterNames[2]);

var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("AbcCompany.XyzProduct.*")
.AddMeter("SomeCompany.SomeProduct.SomeComponent")
.AddInMemoryExporter(exportedItems)
.AddView(instrumentName, newName)
.Build();

var measurement = new Measurement<int>(100, new("name", "apple"), new("color", "red"));
meter1.CreateObservableGauge("myGauge1", () => measurement);
meter2.CreateObservableGauge("myGauge2", () => measurement);
meter3.CreateObservableGauge("myGauge3", () => measurement);

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 3);
Assert.Equal("newName", exportedItems[0].Name);
Assert.Equal("myGauge2", exportedItems[1].Name);
Assert.Equal("myGauge3", exportedItems[2].Name);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down