Esempio n. 1
0
        private static MonitorBase GenerateTelemetryData(Activity activity)
        {
            var telemetryType = activity.GetTelemetryType();
            var monitorTags   = new TagEnumerationState
            {
                PartBTags = AzMonList.Initialize(),
                PartCTags = AzMonList.Initialize()
            };

            activity.EnumerateTags(ref monitorTags);

            MonitorBase telemetry = new MonitorBase
            {
                BaseType = Telemetry_Base_Type_Mapping[telemetryType]
            };

            if (telemetryType == TelemetryType.Request)
            {
                monitorTags.PartBTags.GenerateUrlAndAuthority(out var url, out var urlAuthority);
                var statusCode = AzMonList.GetTagValue(ref monitorTags.PartBTags, SemanticConventions.AttributeHttpStatusCode)?.ToString() ?? "0";
                var success    = activity.GetStatus() != Status.Error;
                var request    = new RequestData(2, activity.Context.SpanId.ToHexString(), activity.Duration.ToString("c", CultureInfo.InvariantCulture), success, statusCode)
                {
                    Name   = activity.DisplayName,
                    Url    = url,
                    Source = urlAuthority
                };

                // TODO: Handle activity.TagObjects, extract well-known tags
                AddPropertiesToTelemetry(request.Properties, monitorTags.PartCTags);
                telemetry.BaseData = request;
            }
            else if (telemetryType == TelemetryType.Dependency)
            {
                var dependency = new RemoteDependencyData(2, activity.DisplayName, activity.Duration.ToString("c", CultureInfo.InvariantCulture))
                {
                    Id      = activity.Context.SpanId.ToHexString(),
                    Success = activity.GetStatus() != Status.Error
                };

                switch (monitorTags.activityType)
                {
                case PartBType.Http:
                    monitorTags.PartBTags.GenerateUrlAndAuthority(out var url, out var urlAuthority);
                    dependency.Data       = url;
                    dependency.Target     = urlAuthority;
                    dependency.Type       = "Http";
                    dependency.ResultCode = AzMonList.GetTagValue(ref monitorTags.PartBTags, SemanticConventions.AttributeHttpStatusCode)?.ToString() ?? "0";
                    break;

                case PartBType.Db:
                    var depDataAndType = AzMonList.GetTagValues(ref monitorTags.PartBTags, SemanticConventions.AttributeDbStatement, SemanticConventions.AttributeDbSystem);
                    dependency.Data = depDataAndType[0]?.ToString();
                    dependency.Type = depDataAndType[1]?.ToString();
                    break;

                case PartBType.Rpc:
                    var depInfo = AzMonList.GetTagValues(ref monitorTags.PartBTags, SemanticConventions.AttributeRpcService, SemanticConventions.AttributeRpcSystem, SemanticConventions.AttributeRpcStatus);
                    dependency.Data       = depInfo[0]?.ToString();
                    dependency.Type       = depInfo[1]?.ToString();
                    dependency.ResultCode = depInfo[2]?.ToString();
                    break;

                case PartBType.Messaging:
                    depDataAndType  = AzMonList.GetTagValues(ref monitorTags.PartBTags, SemanticConventions.AttributeMessagingUrl, SemanticConventions.AttributeMessagingSystem);
                    dependency.Data = depDataAndType[0]?.ToString();
                    dependency.Type = depDataAndType[1]?.ToString();
                    break;
                }

                AddPropertiesToTelemetry(dependency.Properties, monitorTags.PartCTags);
                telemetry.BaseData = dependency;
            }

            return(telemetry);
        }