示例#1
0
 internal MeterProviderBuilder AddView(string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
 {
     if (instrumentName.IndexOf('*') != -1)
     {
         var pattern = '^' + Regex.Escape(instrumentName).Replace("\\*", ".*");
         var regex   = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
         return(this.AddView(instrument => regex.IsMatch(instrument.Name) ? metricStreamConfiguration : null));
     }
     else
     {
         return(this.AddView(instrument => instrument.Name.Equals(instrumentName, StringComparison.OrdinalIgnoreCase) ? metricStreamConfiguration : null));
     }
 }
示例#2
0
        public MetricStreamIdentity(Instrument instrument, MetricStreamConfiguration metricStreamConfiguration)
        {
            this.MeterName             = instrument.Meter.Name;
            this.MeterVersion          = instrument.Meter.Version ?? string.Empty;
            this.InstrumentName        = metricStreamConfiguration?.Name ?? instrument.Name;
            this.Unit                  = instrument.Unit ?? string.Empty;
            this.Description           = metricStreamConfiguration?.Description ?? instrument.Description ?? string.Empty;
            this.InstrumentType        = instrument.GetType();
            this.ViewId                = metricStreamConfiguration?.ViewId;
            this.MetricStreamName      = $"{this.MeterName}.{this.MeterVersion}.{this.InstrumentName}";
            this.TagKeys               = metricStreamConfiguration?.CopiedTagKeys;
            this.HistogramBucketBounds = (metricStreamConfiguration as ExplicitBucketHistogramConfiguration)?.CopiedBoundaries;

            unchecked
            {
                var hash = 17;
                hash = (hash * 31) + this.InstrumentType.GetHashCode();
                hash = (hash * 31) + this.MeterName.GetHashCode();
                hash = (hash * 31) + this.MeterVersion.GetHashCode();
                hash = (hash * 31) + this.InstrumentName.GetHashCode();
                hash = this.Unit == null ? hash : (hash * 31) + this.Unit.GetHashCode();
                hash = this.Description == null ? hash : (hash * 31) + this.Description.GetHashCode();
                hash = !this.ViewId.HasValue ? hash : (hash * 31) + this.ViewId.Value;
                hash = this.TagKeys == null ? hash : (hash * 31) + StringArrayComparer.GetHashCode(this.TagKeys);
                if (this.HistogramBucketBounds != null)
                {
                    var len = this.HistogramBucketBounds.Length;
                    for (var i = 0; i < len; ++i)
                    {
                        hash = (hash * 31) + this.HistogramBucketBounds[i].GetHashCode();
                    }
                }

                this.hashCode = hash;
            }
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
        {
            if (metricStreamConfiguration == null)
            {
                throw new ArgumentNullException($"Metric stream configuration cannot be null.", nameof(metricStreamConfiguration));
            }

            if (!MeterProviderBuilderSdk.IsValidViewName(metricStreamConfiguration.Name))
            {
                throw new ArgumentException($"Custom view name {metricStreamConfiguration.Name} is invalid.", nameof(metricStreamConfiguration.Name));
            }

            if (metricStreamConfiguration is ExplicitBucketHistogramConfiguration histogramConfiguration)
            {
                // Validate histogram boundaries
                if (histogramConfiguration.Boundaries != null && !IsSortedAndDistinct(histogramConfiguration.Boundaries))
                {
                    throw new ArgumentException($"Histogram boundaries must be in ascending order with distinct values", nameof(histogramConfiguration.Boundaries));
                }
            }

            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration));
            }

            return(meterProviderBuilder);
        }
示例#4
0
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
        {
            if (metricStreamConfiguration == null)
            {
                throw new ArgumentNullException($"Metric stream configuration cannot be null.", nameof(metricStreamConfiguration));
            }

            if (!MeterProviderBuilderSdk.IsValidViewName(metricStreamConfiguration.Name))
            {
                throw new ArgumentException($"Custom view name {metricStreamConfiguration.Name} is invalid.", nameof(metricStreamConfiguration.Name));
            }

            if (metricStreamConfiguration.Name != null && instrumentName.IndexOf('*') != -1)
            {
                throw new ArgumentException(
                          $"Instrument selection criteria is invalid. Instrument name '{instrumentName}' " +
                          $"contains a wildcard character. This is not allowed when using a view to " +
                          $"rename a metric stream as it would lead to conflicting metric stream names.",
                          nameof(instrumentName));
            }

            if (metricStreamConfiguration is ExplicitBucketHistogramConfiguration histogramConfiguration)
            {
                // Validate histogram boundaries
                if (histogramConfiguration.Boundaries != null && !IsSortedAndDistinct(histogramConfiguration.Boundaries))
                {
                    throw new ArgumentException($"Histogram boundaries must be in ascending order with distinct values", nameof(histogramConfiguration.Boundaries));
                }
            }

            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
        {
            Guard.ThrowIfNull(metricStreamConfiguration);

            if (metricStreamConfiguration.Name != null && instrumentName.IndexOf('*') != -1)
            {
                throw new ArgumentException(
                          $"Instrument selection criteria is invalid. Instrument name '{instrumentName}' " +
                          $"contains a wildcard character. This is not allowed when using a view to " +
                          $"rename a metric stream as it would lead to conflicting metric stream names.",
                          nameof(instrumentName));
            }

            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration));
            }

            return(meterProviderBuilder);
        }