private void onSpanEnd()

in apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelSpan.java [146:252]


    private void onSpanEnd(SpanImpl s) {

        Map<String, Object> attributes = s.getOtelAttributes();

        String type = null;
        String subType = null;

        String netPeerIp = (String) attributes.get("net.peer.ip");
        String netPeerName = (String) attributes.get("net.peer.name");
        Long netPortLong = (Long) attributes.get("net.peer.port");
        int netPort = -1;
        if (null != netPortLong && netPortLong > 0L) {
            netPort = netPortLong.intValue();
        }

        String netPeer = netPeerName != null ? netPeerName : netPeerIp;

        String httpUrl = (String) attributes.get("http.url");
        String httpScheme = (String) attributes.get("http.scheme");
        String dbSystem = (String) attributes.get("db.system");
        String messagingSystem = (String) attributes.get("messaging.system");
        String rpcSystem = (String) attributes.get("rpc.system");

        if (null != dbSystem) {
            type = "db";
            subType = dbSystem;
            String dbName = (String) attributes.get("db.name");
            String dbStatement = (String) attributes.get("db.statement");
            String dbUser = (String) attributes.get("db.user");
            s.getContext().getDb()
                .withType(subType)
                .withInstance(dbName)
                .withStatement(dbStatement)
                .withUser(dbUser);
            s.getContext().getServiceTarget()
                .withType(subType)
                .withName(dbName);

        } else if (messagingSystem != null) {
            type = "messaging";
            subType = messagingSystem;
            String messagingDestination = (String) attributes.get("messaging.destination");
            URI messagingUri = parseURI((String) attributes.get("messaging.url"));

            if (netPeer == null && messagingUri != null) {
                netPeer = messagingUri.getHost();
                netPort = messagingUri.getPort();
            }
            s.getContext().getServiceTarget()
                .withType(subType)
                .withName(messagingDestination);

        } else if (rpcSystem != null) {
            type = "external";
            subType = rpcSystem;
            String service = (String) attributes.get("rpc.service");

            s.getContext().getServiceTarget()
                .withType(subType)
                // default service name on rpc.service
                .withName(service)
                // host:port with higher priority
                .withHostPortName(netPeer, netPort)
                .withNameOnlyDestinationResource();

        } else if (httpUrl != null || httpScheme != null) {
            type = "external";
            subType = "http";

            String httpHost = (String) attributes.get("http.host");
            if (null == httpHost) {
                httpHost = netPeer;
            }
            if (httpHost == null && httpUrl != null) {
                URI httpUri = parseURI(httpUrl);
                if (httpUri != null) {
                    httpHost = httpUri.getHost();
                    netPort = httpUri.getPort();
                    httpScheme = httpUri.getScheme();
                }
            }

            netPort = UrlImpl.normalizePort(netPort, httpScheme);

            s.getContext().getServiceTarget()
                .withType(subType)
                .withHostPortName(httpHost, netPort)
                .withNameOnlyDestinationResource();
        }

        if (type == null) {
            type = "unknown";
            if (s.getOtelKind() == OTelSpanKind.INTERNAL) {
                type = "app";
                subType = "internal";
            }
        }

        if (netPeer != null && netPort > 0) {
            s.getContext().getDestination()
                .withAddress(netPeer)
                .withPort(netPort);
        }


        s.withType(type).withSubtype(subType);
    }