internal MetricPoint(
            AggregatorStore aggregatorStore,
            AggregationType aggType,
            string[] keys,
            object[] values,
            double[] histogramExplicitBounds)
        {
            Debug.Assert(aggregatorStore != null, "AggregatorStore was null.");
            Debug.Assert((keys?.Length ?? 0) == (values?.Length ?? 0), "Key and value array lengths did not match.");
            Debug.Assert(histogramExplicitBounds != null, "Histogram explicit Bounds was null.");

            this.aggType           = aggType;
            this.Tags              = new ReadOnlyTagCollection(keys, values);
            this.runningValue      = default;
            this.snapshotValue     = default;
            this.deltaLastValue    = default;
            this.MetricPointStatus = MetricPointStatus.NoCollectPending;

            if (this.aggType == AggregationType.Histogram)
            {
                this.histogramBuckets = new HistogramBuckets(histogramExplicitBounds);
            }
            else if (this.aggType == AggregationType.HistogramSumCount)
            {
                this.histogramBuckets = new HistogramBuckets(null);
            }
            else
            {
                this.histogramBuckets = null;
            }

            // Note: Intentionally set last because this is used to detect valid MetricPoints.
            this.aggregatorStore = aggregatorStore;
        }
        internal Metric(
            MetricStreamIdentity instrumentIdentity,
            AggregationTemporality temporality,
            int maxMetricPointsPerMetricStream,
            double[] histogramBounds    = null,
            string[] tagKeysInteresting = null)
        {
            this.InstrumentIdentity = instrumentIdentity;

            AggregationType aggType;

            if (instrumentIdentity.InstrumentType == typeof(ObservableCounter <long>) ||
                instrumentIdentity.InstrumentType == typeof(ObservableCounter <int>) ||
                instrumentIdentity.InstrumentType == typeof(ObservableCounter <short>) ||
                instrumentIdentity.InstrumentType == typeof(ObservableCounter <byte>))
            {
                aggType         = AggregationType.LongSumIncomingCumulative;
                this.MetricType = MetricType.LongSum;
            }
            else if (instrumentIdentity.InstrumentType == typeof(Counter <long>) ||
                     instrumentIdentity.InstrumentType == typeof(Counter <int>) ||
                     instrumentIdentity.InstrumentType == typeof(Counter <short>) ||
                     instrumentIdentity.InstrumentType == typeof(Counter <byte>))
            {
                aggType         = AggregationType.LongSumIncomingDelta;
                this.MetricType = MetricType.LongSum;
            }
            else if (instrumentIdentity.InstrumentType == typeof(Counter <double>) ||
                     instrumentIdentity.InstrumentType == typeof(Counter <float>))
            {
                aggType         = AggregationType.DoubleSumIncomingDelta;
                this.MetricType = MetricType.DoubleSum;
            }
            else if (instrumentIdentity.InstrumentType == typeof(ObservableCounter <double>) ||
                     instrumentIdentity.InstrumentType == typeof(ObservableCounter <float>))
            {
                aggType         = AggregationType.DoubleSumIncomingCumulative;
                this.MetricType = MetricType.DoubleSum;
            }
            else if (instrumentIdentity.InstrumentType == typeof(ObservableGauge <double>) ||
                     instrumentIdentity.InstrumentType == typeof(ObservableGauge <float>))
            {
                aggType         = AggregationType.DoubleGauge;
                this.MetricType = MetricType.DoubleGauge;
            }
            else if (instrumentIdentity.InstrumentType == typeof(ObservableGauge <long>) ||
                     instrumentIdentity.InstrumentType == typeof(ObservableGauge <int>) ||
                     instrumentIdentity.InstrumentType == typeof(ObservableGauge <short>) ||
                     instrumentIdentity.InstrumentType == typeof(ObservableGauge <byte>))
            {
                aggType         = AggregationType.LongGauge;
                this.MetricType = MetricType.LongGauge;
            }
            else if (instrumentIdentity.InstrumentType == typeof(Histogram <long>) ||
                     instrumentIdentity.InstrumentType == typeof(Histogram <int>) ||
                     instrumentIdentity.InstrumentType == typeof(Histogram <short>) ||
                     instrumentIdentity.InstrumentType == typeof(Histogram <byte>) ||
                     instrumentIdentity.InstrumentType == typeof(Histogram <float>) ||
                     instrumentIdentity.InstrumentType == typeof(Histogram <double>))
            {
                this.MetricType = MetricType.Histogram;

                if (histogramBounds != null &&
                    histogramBounds.Length == 0)
                {
                    aggType = AggregationType.HistogramSumCount;
                }
                else
                {
                    aggType = AggregationType.Histogram;
                }
            }
            else
            {
                throw new NotSupportedException($"Unsupported Instrument Type: {instrumentIdentity.InstrumentType.FullName}");
            }

            this.aggStore           = new AggregatorStore(instrumentIdentity.InstrumentName, aggType, temporality, maxMetricPointsPerMetricStream, histogramBounds ?? DefaultHistogramBounds, tagKeysInteresting);
            this.Temporality        = temporality;
            this.InstrumentDisposed = false;
        }
Ejemplo n.º 3
0
        internal Metric(
            Instrument instrument,
            AggregationTemporality temporality,
            string metricName,
            string metricDescription,
            double[] histogramBounds    = null,
            string[] tagKeysInteresting = null)
        {
            this.Name        = metricName;
            this.Description = metricDescription ?? string.Empty;
            this.Unit        = instrument.Unit ?? string.Empty;
            this.Meter       = instrument.Meter;
            AggregationType aggType = default;

            if (instrument.GetType() == typeof(ObservableCounter <long>) ||
                instrument.GetType() == typeof(ObservableCounter <int>) ||
                instrument.GetType() == typeof(ObservableCounter <short>) ||
                instrument.GetType() == typeof(ObservableCounter <byte>))
            {
                aggType         = AggregationType.LongSumIncomingCumulative;
                this.MetricType = MetricType.LongSum;
            }
            else if (instrument.GetType() == typeof(Counter <long>) ||
                     instrument.GetType() == typeof(Counter <int>) ||
                     instrument.GetType() == typeof(Counter <short>) ||
                     instrument.GetType() == typeof(Counter <byte>))
            {
                aggType         = AggregationType.LongSumIncomingDelta;
                this.MetricType = MetricType.LongSum;
            }
            else if (instrument.GetType() == typeof(Counter <double>) ||
                     instrument.GetType() == typeof(Counter <float>))
            {
                aggType         = AggregationType.DoubleSumIncomingDelta;
                this.MetricType = MetricType.DoubleSum;
            }
            else if (instrument.GetType() == typeof(ObservableCounter <double>) ||
                     instrument.GetType() == typeof(ObservableCounter <float>))
            {
                aggType         = AggregationType.DoubleSumIncomingCumulative;
                this.MetricType = MetricType.DoubleSum;
            }
            else if (instrument.GetType() == typeof(ObservableGauge <double>) ||
                     instrument.GetType() == typeof(ObservableGauge <float>))
            {
                aggType         = AggregationType.DoubleGauge;
                this.MetricType = MetricType.DoubleGauge;
            }
            else if (instrument.GetType() == typeof(ObservableGauge <long>) ||
                     instrument.GetType() == typeof(ObservableGauge <int>) ||
                     instrument.GetType() == typeof(ObservableGauge <short>) ||
                     instrument.GetType() == typeof(ObservableGauge <byte>))
            {
                aggType         = AggregationType.LongGauge;
                this.MetricType = MetricType.LongGauge;
            }
            else if (instrument.GetType() == typeof(Histogram <long>) ||
                     instrument.GetType() == typeof(Histogram <int>) ||
                     instrument.GetType() == typeof(Histogram <short>) ||
                     instrument.GetType() == typeof(Histogram <byte>) ||
                     instrument.GetType() == typeof(Histogram <float>) ||
                     instrument.GetType() == typeof(Histogram <double>))
            {
                this.MetricType = MetricType.Histogram;

                if (histogramBounds != null &&
                    histogramBounds.Length == 0)
                {
                    aggType = AggregationType.HistogramSumCount;
                }
                else
                {
                    aggType = AggregationType.Histogram;
                }
            }
            else
            {
                // TODO: Log and assign some invalid Enum.
            }

            this.aggStore           = new AggregatorStore(aggType, temporality, histogramBounds ?? DefaultHistogramBounds, tagKeysInteresting);
            this.Temporality        = temporality;
            this.InstrumentDisposed = false;
        }
        internal MeterProviderSdk(
            Resource resource,
            IEnumerable <string> meterSources,
            List <MeterProviderBuilderSdk.InstrumentationFactory> instrumentationFactories,
            MetricProcessor[] metricProcessors)
        {
            this.Resource = resource;

            // TODO: Replace with single CompositeProcessor.
            this.metricProcessors.AddRange(metricProcessors);

            foreach (var processor in this.metricProcessors)
            {
                processor.SetGetMetricFunction(this.Collect);
                processor.SetParentProvider(this);
            }

            if (instrumentationFactories.Any())
            {
                foreach (var instrumentationFactory in instrumentationFactories)
                {
                    this.instrumentations.Add(instrumentationFactory.Factory());
                }
            }

            // Setup Listener
            var meterSourcesToSubscribe = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

            foreach (var name in meterSources)
            {
                meterSourcesToSubscribe[name] = true;
            }

            this.listener = new MeterListener()
            {
                InstrumentPublished = (instrument, listener) =>
                {
                    if (meterSourcesToSubscribe.ContainsKey(instrument.Meter.Name))
                    {
                        var aggregatorStore = new AggregatorStore(instrument);

                        // Lock to prevent new instrument (aggregatorstore)
                        // from being added while Collect is going on.
                        lock (this.collectLock)
                        {
                            this.AggregatorStores.TryAdd(aggregatorStore, true);
                            listener.EnableMeasurementEvents(instrument, aggregatorStore);
                        }
                    }
                },
                MeasurementsCompleted = (instrument, state) => this.MeasurementsCompleted(instrument, state),
            };

            // Everything double
            this.listener.SetMeasurementEventCallback <double>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
            this.listener.SetMeasurementEventCallback <float>((i, m, l, c) => this.MeasurementRecorded(i, (double)m, l, c));

            // Everything long
            this.listener.SetMeasurementEventCallback <long>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
            this.listener.SetMeasurementEventCallback <int>((i, m, l, c) => this.MeasurementRecorded(i, (long)m, l, c));
            this.listener.SetMeasurementEventCallback <short>((i, m, l, c) => this.MeasurementRecorded(i, (long)m, l, c));
            this.listener.SetMeasurementEventCallback <byte>((i, m, l, c) => this.MeasurementRecorded(i, (long)m, l, c));

            this.listener.Start();
        }