static TupleHelper() { _size = LabelsHelper.GetSize <TTuple>(); _parser = LabelsHelper.GenerateParser <TTuple>(); FormatReducer = LabelsHelper.MakeReducer <TTuple, string[]>(); HashCodeReducer = LabelsHelper.MakeReducer <TTuple, int>(); }
/// <summary> /// Create Summary /// </summary> /// <param name="name">Name.</param> /// <param name="help">Help text.</param> /// <param name="options">Metric flags</param> /// <param name="labels">Array of label names.</param> /// <param name="objectives">.</param> /// <param name="maxAge"></param> /// <param name="ageBuckets"></param> /// <param name="bufCap"></param> public IMetricFamily <ISummary, TLabels> CreateSummary <TLabels>( string name, string help, TLabels labels, IReadOnlyList <QuantileEpsilonPair> objectives = null, TimeSpan?maxAge = null, int?ageBuckets = null, int?bufCap = null, MetricFlags options = MetricFlags.Default) #if HasITuple where TLabels : struct, ITuple, IEquatable <TLabels> #else where TLabels : struct, IEquatable <TLabels> #endif { var metric = TryGetByName <IMetricFamily <ISummary, TLabels> >(name); if (metric == null) { var configuration = new SummaryConfiguration(name, help, LabelsHelper.ToArray(labels), options, objectives, maxAge, ageBuckets, bufCap); metric = CreateSummaryInternal <TLabels>(configuration); } ValidateLabelNames(metric.LabelNames, labels); return(metric); }
private IEnumerable <KeyValuePair <TLabels, TMetric> > EnumerateLabelled() { if (_labelledMetrics == null) { yield break; } foreach (var labelled in _labelledMetrics) { yield return(new KeyValuePair <TLabels, TMetric>(LabelsHelper.FromArray <TLabels>(labelled.Value.LabelValues), labelled.Value)); } }
public MetricFamily(TConfig configuration, MetricType metricType, Func <TConfig, IReadOnlyList <string>, TImplementation> instanceFactory) { _metricType = metricType; _configuration = configuration; _metricNames = new[] { _configuration.Name }; _instanceFactory = instanceFactory; _unlabelled = _instanceFactory(_configuration, default); LabelNames = LabelsHelper.FromArray <TLabels>(configuration.LabelNames); if (configuration.LabelNames.Count > 0) { _labelledMetrics = new ConcurrentDictionary <int, TImplementation>(); } }
public TMetric RemoveLabelled(TLabels labels) { if (_labelledMetrics == null) { throw new InvalidOperationException("Metric family does not have any labels"); } var key = LabelsHelper.GetHashCode(labels); _labelledMetrics.TryRemove(key, out var removed); return(removed); }
public TMetric WithLabels(TLabels labels) { if (_labelledMetrics == null) { throw new InvalidOperationException("Metric family does not have any labels"); } var key = LabelsHelper.GetHashCode(labels); if (_labelledMetrics.TryGetValue(key, out var metric)) { return(metric); } metric = CreateLabelled(LabelsHelper.ToArray(labels)); return(_labelledMetrics.GetOrAdd(key, metric)); }
/// <summary> /// Create Counter. /// </summary> /// <param name="name">Metric name.</param> /// <param name="help">Help text.</param> /// <param name="options">Metric flags</param> /// <param name="labels">Label names</param> public IMetricFamily <ICounter, TLabels> CreateCounter <TLabels>(string name, string help, TLabels labels, MetricFlags options = MetricFlags.Default) #if HasITuple where TLabels : struct, ITuple, IEquatable <TLabels> #else where TLabels : struct, IEquatable <TLabels> #endif { var metric = TryGetByName <IMetricFamily <ICounter, TLabels> >(name); if (metric == null) { var configuration = new MetricConfiguration(name, help, LabelsHelper.ToArray(labels), options); metric = CreateCounterInternal <TLabels>(configuration); } ValidateLabelNames(metric.LabelNames, labels); return(metric); }
TMetric IMetricFamily <TMetric> .RemoveLabelled(params string[] labels) { if (_labelledMetrics == null) { throw new InvalidOperationException("Metric family does not have any labels"); } if (labels.Length != _configuration.LabelNames.Count) { throw new ArgumentException("Wrong number of labels"); } var key = LabelsHelper.GetHashCode(labels); _labelledMetrics.TryRemove(key, out var removed); return(removed); }
/// <summary> /// Create Counter. /// </summary> /// <param name="name">Metric name.</param> /// <param name="help">Help text.</param> /// <param name="includeTimestamp">Include unix timestamp for metric.</param> /// <param name="labelNames">Label names</param> public IMetricFamily <ICounter, TLabels> CreateCounter <TLabels>(string name, string help, TLabels labelNames, bool includeTimestamp = false) #if HasITuple where TLabels : struct, ITuple, IEquatable <TLabels> #else where TLabels : struct, IEquatable <TLabels> #endif { var metric = TryGetByName <IMetricFamily <ICounter, TLabels> >(name); if (metric == null) { var configuration = new MetricConfiguration(name, help, LabelsHelper.ToArray(labelNames), includeTimestamp); metric = CreateCounterInternal <TLabels>(configuration); } else { ValidateLabelNames(metric.LabelNames, labelNames); } return(metric); }
TMetric IMetricFamily <TMetric> .WithLabels(params string[] labels) { if (_labelledMetrics == null) { throw new InvalidOperationException("Metric family does not have any labels"); } if (labels.Length != _configuration.LabelNames.Count) { throw new ArgumentException("Wrong number of labels"); } var key = LabelsHelper.GetHashCode(labels); if (_labelledMetrics.TryGetValue(key, out var metric)) { return(metric); } metric = CreateLabelled(labels); return(_labelledMetrics.GetOrAdd(key, metric)); }