Exemplo n.º 1
0
        public void SendActivity(Activity activity)
        {
            if (activity == null)
            {
                return;
            }

            EventTelemetry activityTelemetry = new EventTelemetry(activity.Name);

            activityTelemetry.Properties[ItemSourceLabelName] = ItemSourceLabelValue;

            activityTelemetry.Timestamp = activity.IsStatusFinal ? activity.EndTime.ToUniversalTime() : DateTimeOffset.UtcNow;
            ActivitySerializer.AddActivityData(activity, activityTelemetry.Properties, activityTelemetry.Metrics);

            // We need to only log exceptions once per fault, togehter with the activity that was faulted explicitly.
            if (activity.Status == ActivityStatus.Faulted && activity.InitialFaultActivity == activity)
            {
                Exception exception = activity.FaultException;
                if (exception != null)
                {
                    ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(exception);
                    exceptionTelemetry.Properties[ItemSourceLabelName] = ItemSourceLabelValue;

                    ActivitySerializer.AddActivityMetadataForExceptions(activity, exceptionTelemetry.Properties);

                    _applicationInsightsClient.TrackException(exceptionTelemetry);
                }
            }

            _applicationInsightsClient.TrackEvent(activityTelemetry);
        }
        public void ProcessActivity(Activity activity, out bool continueProcessing)
        {
            Util.EnsureNotNull(activity, nameof(activity));
            continueProcessing = true;

            if (!_activitySelector(activity))
            {
                return;
            }

            double?metricValue = _measurementValueExtractor(activity);

            if (metricValue == null)
            {
                return;
            }

            string[] dimensionValues = new string[_dimensionLabelExtractors.Length];
            for (int i = 0; i < _dimensionLabelExtractors.Length; i++)
            {
                string labelValue = _dimensionLabelExtractors[i](activity);
                dimensionValues[i] = Util.SpellNull(labelValue);
            }

            MetricSeries dataSeries;
            bool         canGetSeries = _outputMetric.TryGetDataSeries(out dataSeries, true, dimensionValues);

            if (!canGetSeries)
            {
                // Cannot get series. Probably reached dimension cap.

                // Consider attempting to use the dimensionless time series (like below) instead of the following error log:
                // canGetSeries = metric.TryGetDataSeries(out dataSeries);
                // if (! canGetSeries)
                // {
                //     create detailLabels and detailMeasures and throw ActivityInsightsException exception like below
                // }

                var detailLabels   = new Dictionary <string, string>();
                var detailMeasures = new Dictionary <string, double>();
                ActivitySerializer.AddActivityCoreMetadata(activity, detailLabels, detailMeasures);

                detailLabels["ActivityProcessor.Name"]             = this.Name;
                detailLabels["ActivityProcessor.Type"]             = this.GetType().Name;
                detailLabels["OutputMetric.Namespace"]             = _outputMetric.Identifier.MetricNamespace;
                detailLabels["OutputMetric.Name"]                  = _outputMetric.Identifier.MetricId;
                detailLabels["OutputMetric.DimensionNames"]        = Util.FormatAsArray(_outputMetric.Identifier.GetDimensionNames());
                detailLabels["OutputMetric.LookupDimensionValues"] = Util.FormatAsArray(dimensionValues);

                detailMeasures["OutputMetric.Value"] = metricValue.Value;

                throw new ActivityInsightsException("Activity processor failed to get Get Data Series for output metric (see details).", detailLabels, detailMeasures);
            }

            dataSeries.TrackValue(metricValue);
        }