Пример #1
0
        public BaseExportingMetricReader(BaseExporter <Metric> exporter)
        {
            Guard.Null(exporter, nameof(exporter));

            this.exporter = exporter;

            var exportorType = exporter.GetType();
            var attributes   = exportorType.GetCustomAttributes(typeof(AggregationTemporalityAttribute), true);

            if (attributes.Length > 0)
            {
                var attr = (AggregationTemporalityAttribute)attributes[attributes.Length - 1];
                this.PreferredAggregationTemporality = attr.Preferred;
                this.SupportedAggregationTemporality = attr.Supported;
            }

            attributes = exportorType.GetCustomAttributes(typeof(ExportModesAttribute), true);
            if (attributes.Length > 0)
            {
                var attr = (ExportModesAttribute)attributes[attributes.Length - 1];
                this.supportedExportModes = attr.Supported;
            }

            if (exporter is IPullMetricExporter pullExporter)
            {
                if (this.supportedExportModes.HasFlag(ExportModes.Push))
                {
                    pullExporter.Collect = this.Collect;
                }
                else
                {
                    pullExporter.Collect = (timeoutMilliseconds) =>
                    {
                        using (PullMetricScope.Begin())
                        {
                            return(this.Collect(timeoutMilliseconds));
                        }
                    };
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseExportingMetricReader"/> class.
        /// </summary>
        /// <param name="exporter">Exporter instance to export Metrics to.</param>
        public BaseExportingMetricReader(BaseExporter <Metric> exporter)
        {
            Guard.ThrowIfNull(exporter);

            this.exporter = exporter;

            var exportorType = exporter.GetType();
            var attributes   = exportorType.GetCustomAttributes(typeof(ExportModesAttribute), true);

            if (attributes.Length > 0)
            {
                var attr = (ExportModesAttribute)attributes[attributes.Length - 1];
                this.supportedExportModes = attr.Supported;
            }

            if (exporter is IPullMetricExporter pullExporter)
            {
                if (this.supportedExportModes.HasFlag(ExportModes.Push))
                {
                    pullExporter.Collect = this.Collect;
                }
                else
                {
                    pullExporter.Collect = (timeoutMilliseconds) =>
                    {
                        using (PullMetricScope.Begin())
                        {
                            return(this.Collect(timeoutMilliseconds));
                        }
                    };
                }
            }

            this.exportCalledMessage    = $"{nameof(BaseExportingMetricReader)} calling {this.Exporter}.{nameof(this.Exporter.Export)} method.";
            this.exportSucceededMessage = $"{this.Exporter}.{nameof(this.Exporter.Export)} succeeded.";
            this.exportFailedMessage    = $"{this.Exporter}.{nameof(this.Exporter.Export)} failed.";
        }