Esempio n. 1
0
        ///<summary>
        /// Gets http dependency url from activity tag objects.
        ///</summary>
        internal static string GetDependencyUrl(this AzMonList tagObjects)
        {
            // From spec: one of the following combinations is required in case of client spans:
            // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-client
            // http.url
            // http.scheme, http.host, http.target
            // http.scheme, net.peer.name, net.peer.port, http.target
            // http.scheme, net.peer.ip, net.peer.port, http.target
            string url = null;

            url = tagObjects.GetUrlUsingHttpUrl();
            if (url != null)
            {
                return(url);
            }

            var httpScheme = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpScheme)?.ToString();

            if (!string.IsNullOrWhiteSpace(httpScheme))
            {
                var httpTarget = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpTarget)?.ToString();
                // http.target is required in other three possible combinations
                // If not available then do not proceed.
                if (string.IsNullOrWhiteSpace(httpTarget))
                {
                    return(null);
                }

                string defaultPort = GetDefaultHttpPort(httpScheme);
                url = tagObjects.GetUrlUsingHttpHost(httpScheme, defaultPort, httpTarget);
                if (url != null)
                {
                    return(url);
                }

                var host = tagObjects.GetHostUsingNetPeerAttributes();
                if (!string.IsNullOrWhiteSpace(host))
                {
                    var netPeerPort = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeNetPeerPort)?.ToString();
                    if (!string.IsNullOrWhiteSpace(netPeerPort))
                    {
                        url = tagObjects.GetUrlUsingHostAndPort(httpScheme, host, netPeerPort, defaultPort, httpTarget);
                        return(url);
                    }
                }
            }

            return(url);
        }
Esempio n. 2
0
        ///<summary>
        /// Gets Http dependency target from activity tag objects.
        ///</summary>
        internal static string GetDependencyTarget(this AzMonList tagObjects, PartBType type)
        {
            string target;
            string defaultPort;

            switch (type)
            {
            case PartBType.Http:
                defaultPort = GetDefaultHttpPort(AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpScheme)?.ToString());
                break;

            case PartBType.Db:
                defaultPort = GetDefaultDbPort(AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeDbSystem)?.ToString());
                break;

            default:
                defaultPort = "0";
                break;
            }

            var peerService = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributePeerService)?.ToString();

            if (!string.IsNullOrEmpty(peerService))
            {
                target = peerService;
                return(target);
            }

            if (type == PartBType.Http)
            {
                var httpHost = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpHost)?.ToString();
                if (!string.IsNullOrEmpty(httpHost))
                {
                    string portSection = $":{defaultPort}";
                    if (httpHost.EndsWith(portSection, StringComparison.OrdinalIgnoreCase))
                    {
                        var truncatedHost = httpHost.Substring(0, httpHost.IndexOf(portSection, StringComparison.OrdinalIgnoreCase));
                        target = truncatedHost;
                    }
                    else
                    {
                        target = httpHost;
                    }
                    return(target);
                }
                var httpUrl = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeHttpUrl)?.ToString();
                if (!string.IsNullOrEmpty(httpUrl) && Uri.TryCreate(httpUrl.ToString(), UriKind.RelativeOrAbsolute, out var uri) && uri.IsAbsoluteUri)
                {
                    target = uri.Authority;
                    if (!string.IsNullOrEmpty(target))
                    {
                        return(target);
                    }
                }
            }

            target = tagObjects.GetHostUsingNetPeerAttributes();
            if (!string.IsNullOrEmpty(target))
            {
                var netPeerPort = AzMonList.GetTagValue(ref tagObjects, SemanticConventions.AttributeNetPeerPort)?.ToString();
                if (!string.IsNullOrEmpty(netPeerPort) && netPeerPort != defaultPort)
                {
                    target = $"{target}:{netPeerPort}";
                }
                return(target);
            }

            return(target);
        }