Ejemplo n.º 1
0
        public static RequestTelemetry ConvertRequestToSdkApi(Telemetry inputTelemetry)
        {
            var result = new RequestTelemetry();

            Request item = inputTelemetry.Request;

            result.Id   = item.Id;
            result.Name = item.Name;

            if (DateTimeOffset.TryParseExact(inputTelemetry.DateTime, "o", CultureInfo.InvariantCulture, DateTimeStyles.None, out var timestamp))
            {
                result.Timestamp = timestamp;
            }

            result.Duration     = item.Duration?.ToTimeSpan() ?? TimeSpan.Zero;
            result.ResponseCode = item.ResponseCode;
            result.Success      = item.Success?.Value;
            result.Source       = item.Source;

            if (Uri.TryCreate(item.Url, UriKind.RelativeOrAbsolute, out Uri url))
            {
                result.Url = url;
            }

            result.Properties.PopulateFromProtobuf(item.Properties);
            result.Metrics.PopulateFromProtobuf(item.Measurements);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);
            AiTelemetryConverter.CopySamplingFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 2
0
        public static PageViewTelemetry ConvertPageViewToSdkApi(Telemetry inputTelemetry)
        {
            var result = new PageViewTelemetry();

            PageView item = inputTelemetry.PageView;

            item.Event = item.Event ?? new Event();

            result.Id = item.Id;

            if (Uri.TryCreate(item.Url, UriKind.RelativeOrAbsolute, out var uri))
            {
                result.Url = uri;
            }

            result.Duration = item.Duration?.ToTimeSpan() ?? TimeSpan.Zero;

            result.Name = item.Event.Name;

            result.Properties.PopulateFromProtobuf(item.Event?.Properties);
            result.Metrics.PopulateFromProtobuf(item.Event?.Measurements);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);
            AiTelemetryConverter.CopySamplingFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 3
0
        public static AvailabilityTelemetry ConvertAvailabilityToSdkApi(Telemetry inputTelemetry)
        {
            var result = new AvailabilityTelemetry();

            Availability item = inputTelemetry.Availability;

            result.Id   = item.Id;
            result.Name = item.Name;

            if (DateTimeOffset.TryParseExact(inputTelemetry.DateTime, "o", CultureInfo.InvariantCulture, DateTimeStyles.None, out var timestamp))
            {
                result.Timestamp = timestamp;
            }

            result.Duration    = item.Duration?.ToTimeSpan() ?? TimeSpan.Zero;
            result.RunLocation = item.RunLocation;
            result.Success     = item.Success;
            result.Message     = item.Message;

            result.Properties.PopulateFromProtobuf(item.Properties);
            result.Metrics.PopulateFromProtobuf(item.Measurements);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 4
0
        public static DependencyTelemetry ConvertDependencyToSdkApi(Telemetry inputTelemetry)
        {
            var result = new DependencyTelemetry();

            Dependency item = inputTelemetry.Dependency;

            result.Type   = item.Type;
            result.Target = item.Target;
            result.Name   = item.Name;
            result.Data   = item.Data;

            if (DateTimeOffset.TryParseExact(inputTelemetry.DateTime, "o", CultureInfo.InvariantCulture, DateTimeStyles.None, out var timestamp))
            {
                result.Timestamp = timestamp;
            }

            result.Duration   = item.Duration?.ToTimeSpan() ?? TimeSpan.Zero;
            result.ResultCode = item.ResultCode;
            result.Success    = item.Success?.Value ?? true;
            result.Id         = item.Id;

            result.Properties.PopulateFromProtobuf(item.Properties);
            result.Metrics.PopulateFromProtobuf(item.Measurements);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);
            AiTelemetryConverter.CopySamplingFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 5
0
        private static void CopyCommonFields(Telemetry inputTelemetry, ITelemetry telemetry)
        {
            telemetry.Sequence = inputTelemetry.SequenceNumber;
            if (DateTimeOffset.TryParseExact(inputTelemetry.DateTime, "o", CultureInfo.InvariantCulture, DateTimeStyles.None, out var timestamp))
            {
                telemetry.Timestamp = timestamp;
            }
            telemetry.Context.InstrumentationKey = inputTelemetry.InstrumentationKey;

            AiTelemetryConverter.PopulateContext(inputTelemetry, telemetry);
        }
Ejemplo n.º 6
0
        public static EventTelemetry ConvertEventToSdkApi(Telemetry inputTelemetry)
        {
            var result = new EventTelemetry();

            result.Name = inputTelemetry.Event.Name;
            result.Properties.PopulateFromProtobuf(inputTelemetry.Event.Properties);
            result.Metrics.PopulateFromProtobuf(inputTelemetry.Event.Measurements);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);
            AiTelemetryConverter.CopySamplingFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 7
0
        public static MetricTelemetry ConvertMetricToSdkApi(Telemetry inputTelemetry)
        {
            // only one metric in the list is currently supported by Application Insights storage. If multiple data points were sent only the first one will be used
            if (!inputTelemetry.Metric.Metrics.Any())
            {
                throw new ArgumentException("Metrics list can't be empty");
            }

            DataPoint firstMetric = inputTelemetry.Metric.Metrics[0];

            MetricTelemetry result;

            switch (firstMetric.Kind)
            {
            case DataPointType.Measurement:
                result = new MetricTelemetry(firstMetric.Name, firstMetric.Value);
                break;

            case DataPointType.Aggregation:
                firstMetric.Count = firstMetric.Count ?? new Google.Protobuf.WellKnownTypes.Int32Value()
                {
                    Value = 1
                };
                firstMetric.Min = firstMetric.Min ?? new Google.Protobuf.WellKnownTypes.DoubleValue()
                {
                    Value = firstMetric.Value
                };
                firstMetric.Max = firstMetric.Max ?? new Google.Protobuf.WellKnownTypes.DoubleValue()
                {
                    Value = firstMetric.Value
                };
                firstMetric.StdDev = firstMetric.StdDev ?? new Google.Protobuf.WellKnownTypes.DoubleValue()
                {
                    Value = 0
                };

                result = new MetricTelemetry(firstMetric.Name, firstMetric.Count.Value, firstMetric.Value, firstMetric.Min.Value, firstMetric.Max.Value, firstMetric.StdDev.Value);
                break;

            default:
                throw new ArgumentOutOfRangeException(FormattableString.Invariant($"Unknown aggregation Kind: {firstMetric.Kind}"));
            }

            result.MetricNamespace = firstMetric.Ns;

            result.Properties.PopulateFromProtobuf(inputTelemetry.Metric.Properties);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 8
0
        public static TraceTelemetry ConvertTraceToSdkApi(Telemetry inputTelemetry)
        {
            var result = new TraceTelemetry();

            result.Message = inputTelemetry.Message.Message_;

            result.SeverityLevel = AiTelemetryConverter.ConvertSeverityLevel(inputTelemetry.Message.SeverityLevel);

            result.Properties.PopulateFromProtobuf(inputTelemetry.Message.Properties);

            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);
            AiTelemetryConverter.CopySamplingFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 9
0
        public static ExceptionTelemetry ConvertExceptionToSdkApi(Telemetry inputTelemetry)
        {
            //!!! make sure we're onboarded onto a release version of AI SDK
            var item = inputTelemetry.Exception;

            var result = new ExceptionTelemetry(
                item.Exceptions.Select(ed => new ExceptionDetailsInfo(ed.Id, ed.OuterId, ed.TypeName, ed.Message, ed.HasFullStack?.Value ?? true, ed.Stack,
                                                                      ed.ParsedStack.Select(f => new Microsoft.ApplicationInsights.DataContracts.StackFrame(f.Assembly, f.FileName, f.Level, f.Line, f.Method)))),
                AiTelemetryConverter.ConvertSeverityLevel(item.SeverityLevel), item.ProblemId, item.Properties, item.Measurements);


            AiTelemetryConverter.CopyCommonFields(inputTelemetry, result);
            AiTelemetryConverter.CopySamplingFields(inputTelemetry, result);

            return(result);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Processes an incoming telemetry batch for AI channel.
        /// </summary>
        /// <remarks>This method may be called from multiple threads concurrently.</remarks>
        private void OnAiBatchReceived(TelemetryBatch batch, ServerCallContext callContext)
        {
            try
            {
                // send incoming telemetry items to the telemetryClient
                foreach (Telemetry telemetry in batch.Items)
                {
                    ITelemetry convertedTelemetry = null;

                    try
                    {
                        Diagnostics.LogTrace($"AI message received: {batch.Items.Count} items, first item: {batch.Items.First().InstrumentationKey}");

                        switch (telemetry.DataCase)
                        {
                        case Telemetry.DataOneofCase.Event:
                            convertedTelemetry = AiTelemetryConverter.ConvertEventToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.Message:
                            convertedTelemetry = AiTelemetryConverter.ConvertTraceToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.Metric:
                            convertedTelemetry = AiTelemetryConverter.ConvertMetricToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.Exception:
                            convertedTelemetry = AiTelemetryConverter.ConvertExceptionToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.Dependency:
                            convertedTelemetry = AiTelemetryConverter.ConvertDependencyToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.Availability:
                            convertedTelemetry = AiTelemetryConverter.ConvertAvailabilityToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.PageView:
                            convertedTelemetry = AiTelemetryConverter.ConvertPageViewToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.Request:
                            convertedTelemetry = AiTelemetryConverter.ConvertRequestToSdkApi(telemetry);
                            break;

                        case Telemetry.DataOneofCase.None:
                            throw new ArgumentException(
                                      FormattableString.Invariant($"Empty AI telemetry item encountered"));

                        default:
                            throw new ArgumentException(
                                      FormattableString.Invariant($"Unknown AI telemetry item type encountered"));
                        }
                    }
                    catch (Exception e)
                    {
                        // an unexpected issue during conversion
                        // log and carry on
                        Diagnostics.LogError(
                            FormattableString.Invariant(
                                $"Could not convert an incoming AI telemetry item. {e.ToString()}"));
                    }

                    try
                    {
                        if (convertedTelemetry != null)
                        {
                            this.telemetryClient.Track(convertedTelemetry);
                        }
                    }
                    catch (Exception e)
                    {
                        // an unexpected issue while tracking an item
                        // log and carry on
                        Diagnostics.LogError(
                            FormattableString.Invariant(
                                $"Could not track an incoming AI telemetry item. {e.ToString()}"));
                    }
                }
            }
            catch (Exception e)
            {
                // an unexpected issue while processing the batch
                // log and carry on
                Diagnostics.LogError(
                    FormattableString.Invariant(
                        $"Could not process an incoming AI telemetry batch. {e.ToString()}"));
            }
        }