-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,19 +143,7 @@ internal MeterProviderSdk( | |
} | ||
|
||
// Setup Listener | ||
if (state.MeterSources.Any(s => WildcardHelper.ContainsWildcard(s))) | ||
{ | ||
var regex = WildcardHelper.GetWildcardRegex(state.MeterSources); | ||
this.shouldListenTo = instrument => regex.IsMatch(instrument.Meter.Name); | ||
} | ||
else if (state.MeterSources.Any()) | ||
{ | ||
var meterSourcesToSubscribe = new HashSet<string>(state.MeterSources, StringComparer.OrdinalIgnoreCase); | ||
this.shouldListenTo = instrument => meterSourcesToSubscribe.Contains(instrument.Meter.Name); | ||
} | ||
|
||
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"Listening to following meters = \"{string.Join(";", state.MeterSources)}\"."); | ||
|
||
this.shouldListenTo = this.GetPredicate(state); | ||
this.listener = new MeterListener(); | ||
var viewConfigCount = this.viewConfigs.Count; | ||
|
||
|
@@ -187,6 +175,36 @@ internal MeterProviderSdk( | |
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent("MeterProvider built successfully."); | ||
} | ||
|
||
private Func<Instrument, bool> GetPredicate(MeterProviderBuilderSdk state) | ||
{ | ||
List<Predicate<Meter>> predicates = new List<Predicate<Meter>>(); | ||
|
||
if (state.MeterSources.Any()) | ||
{ | ||
predicates.Add(this.GetNamePredicate(state)); | ||
} | ||
|
||
predicates.AddRange(state.MeterSelectionPredicates); | ||
|
||
return (instrument) => | ||
{ | ||
bool shouldListen = false; | ||
for (int i = 0; i < predicates.Count && !shouldListen; i++) | ||
{ | ||
try | ||
{ | ||
shouldListen |= predicates[i](instrument.Meter); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetrySdkEventSource.Log.MeterPredicateException(instrument.Meter.Name, ex); | ||
} | ||
} | ||
|
||
return shouldListen; | ||
}; | ||
} | ||
|
||
internal Resource Resource { get; } | ||
Check failure on line 208 in src/OpenTelemetry/Metrics/MeterProviderSdk.cs
|
||
|
||
internal List<object> Instrumentations => this.instrumentations; | ||
|
@@ -531,4 +549,18 @@ private void ApplySpecificationConfigurationKeys(IConfiguration configuration) | |
} | ||
#endif | ||
} | ||
|
||
private Predicate<Meter> GetNamePredicate(MeterProviderBuilderSdk state) | ||
{ | ||
Debug.Assert(state.MeterSources.Any(), "Should only be called when there are name-based source predicates."); | ||
|
||
if (state.MeterSources.Any(s => WildcardHelper.ContainsWildcard(s))) | ||
{ | ||
var regex = WildcardHelper.GetWildcardRegex(state.MeterSources); | ||
return meter => regex.IsMatch(meter.Name); | ||
} | ||
|
||
var meterSourcesToSubscribe = new HashSet<string>(state.MeterSources, StringComparer.OrdinalIgnoreCase); | ||
return meter => meterSourcesToSubscribe.Contains(meter.Name); | ||
} | ||
} |