示例#1
0
        /// <summary>
        /// Tracks a counter which is aggregated into a MetricTelemetry item.
        /// </summary>
        public static void TrackAggregateMetric(this TelemetryClient telemetryClient, string name, double value, string property1 = null, string property2 = null, string property3 = null)
        {
            if (telemetryClient == null)
            {
                AggregateMetricsEventSource.Log.TelemetryClientRequired();
                return;
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                AggregateMetricsEventSource.Log.TrackAggregateMetricInvalidMetricName();
                return;
            }

            if (aggregationSets == null)
            {
                Initialize();
            }

            Debug.Assert(aggregationSets != null);

            name = GetAggregateMetricName(name);

            Debug.Assert(name.Length > 0 && name.Length <= Constants.NameMaxLength, "Invalid name.");

            int aggregationSetKey = AggregationSet.GetKey(telemetryClient, name);

            AggregationSet aggregationSet = aggregationSets.GetOrAdd(aggregationSetKey, (key) =>
            {
                return(new AggregationSet(telemetryClient, name));
            });

            aggregationSet.AddAggregation(value, property1, property2, property3);
        }
示例#2
0
        /// <summary>
        /// Unregisters an aggregate metric.
        /// </summary>
        public static void UnregisterAggregateMetric(this TelemetryClient telemetryClient, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                AggregateMetricsEventSource.Log.UnregisterAggregateMetricInvalidMetricName();
                return;
            }

            name = GetAggregateMetricName(name);

            int key = AggregationSet.GetKey(telemetryClient, name);

            AggregateMetricProperties properties;

            metricRegistrations.TryRemove(key, out properties);

            AggregationSet aggregationSet;

            aggregationSets.TryRemove(key, out aggregationSet);
        }
示例#3
0
        /// <summary>
        /// Optionally registers an aggregate metric to provide additional aggregation metadata.
        /// </summary>
        public static void RegisterAggregateMetric(this TelemetryClient telemetryClient, string name, string p1Name = null, string p2Name = null, string p3Name = null, PercentileCalculation percentileCalculation = PercentileCalculation.DoNotCalculate)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                AggregateMetricsEventSource.Log.RegisterAggregateMetricInvalidMetricName();
                return;
            }

            name = GetAggregateMetricName(name);

            int registrationKey = AggregationSet.GetKey(telemetryClient, name);

            if (!metricRegistrations.TryAdd(registrationKey, new AggregateMetricProperties()
            {
                P1Name = p1Name,
                P2Name = p2Name,
                P3Name = p3Name,
                PercentileCalculation = percentileCalculation
            }))
            {
                AggregateMetricsEventSource.Log.RegisterAggregateMetricDuplicateMetricName(name);
            }
        }
示例#4
0
        internal static void FlushImpl()
        {
            foreach (KeyValuePair <int, AggregationSet> aggregationSetPair in aggregationSets)
            {
                if (disposing)
                {
                    return;
                }

                AggregationSet aggregationSet = aggregationSetPair.Value;

                ConcurrentDictionary <int, MetricsBag> aggregations = aggregationSet.RemoveAggregations();
                var periodStartTime = DateTimeOffset.Now.Subtract(AggregateMetricsTelemetryModule.FlushInterval);

                foreach (MetricsBag metricsBag in aggregations.Values)
                {
                    if (disposing)
                    {
                        return;
                    }

                    int registrationKey = aggregationSet.Key;

                    string p1Name = null;
                    string p2Name = null;
                    string p3Name = null;
                    var    percentileCalculation = PercentileCalculation.DoNotCalculate;

                    AggregateMetricProperties metricProperties;
                    if (metricRegistrations.TryGetValue(registrationKey, out metricProperties))
                    {
                        if (metricProperties.P1Name != null)
                        {
                            p1Name = metricProperties.P1Name;
                        }

                        if (metricProperties.P2Name != null)
                        {
                            p2Name = metricProperties.P2Name;
                        }

                        if (metricProperties.P3Name != null)
                        {
                            p3Name = metricProperties.P3Name;
                        }

                        percentileCalculation = metricProperties.PercentileCalculation;
                    }

                    AggregationResult aggregation = metricsBag.CalculateAggregation(percentileCalculation);

                    var metric = new MetricTelemetry(aggregationSet.Name, aggregation.Sum)
                    {
                        Timestamp         = periodStartTime,
                        Count             = aggregation.Count,
                        Min               = aggregation.Min,
                        Max               = aggregation.Max,
                        StandardDeviation = aggregation.StdDev
                    };

                    metric.Context.GetInternalContext().SdkVersion = sdkVersion;

                    metric.AddProperties(metricsBag, p1Name, p2Name, p3Name);

                    if (percentileCalculation != PercentileCalculation.DoNotCalculate)
                    {
                        metric.Properties.Add(Constants.P50Name, aggregation.P50.ToString(CultureInfo.InvariantCulture));
                        MetricTelemetry p50Metric = metric.CreateDerivedMetric(Constants.P50Name, aggregation.P50);
                        p50Metric.AddProperties(metricsBag, p1Name, p2Name, p3Name);
                        aggregationSet.TelemetryClient.TrackMetric(p50Metric);

                        metric.Properties.Add(Constants.P75Name, aggregation.P75.ToString(CultureInfo.InvariantCulture));
                        MetricTelemetry p75Metric = metric.CreateDerivedMetric(Constants.P75Name, aggregation.P75);
                        p75Metric.AddProperties(metricsBag, p1Name, p2Name, p3Name);
                        aggregationSet.TelemetryClient.TrackMetric(p75Metric);

                        metric.Properties.Add(Constants.P90Name, aggregation.P90.ToString(CultureInfo.InvariantCulture));
                        MetricTelemetry p90Metric = metric.CreateDerivedMetric(Constants.P90Name, aggregation.P90);
                        p90Metric.AddProperties(metricsBag, p1Name, p2Name, p3Name);
                        aggregationSet.TelemetryClient.TrackMetric(p90Metric);

                        metric.Properties.Add(Constants.P95Name, aggregation.P95.ToString(CultureInfo.InvariantCulture));
                        MetricTelemetry p95Metric = metric.CreateDerivedMetric(Constants.P95Name, aggregation.P95);
                        p95Metric.AddProperties(metricsBag, p1Name, p2Name, p3Name);
                        aggregationSet.TelemetryClient.TrackMetric(p95Metric);

                        metric.Properties.Add(Constants.P99Name, aggregation.P99.ToString(CultureInfo.InvariantCulture));
                        MetricTelemetry p99Metric = metric.CreateDerivedMetric(Constants.P99Name, aggregation.P99);
                        p99Metric.AddProperties(metricsBag, p1Name, p2Name, p3Name);
                        aggregationSet.TelemetryClient.TrackMetric(p99Metric);
                    }

                    aggregationSet.TelemetryClient.TrackMetric(metric);
                }
            }
        }