_spanCompatMapping()

in lib/opentelemetry-bridge/OTelSpan.js [221:322]


  _spanCompatMapping() {
    const attrs = this._span._otelAttributes;
    const otelKind = otel.SpanKind[this._span._otelKind]; // map from string to int form
    if (!attrs) {
      if (otelKind === otel.SpanKind.INTERNAL) {
        this._span.type = 'app';
        this._span.subtype = 'internal';
      } else {
        this._span.type = 'unknown';
      }
      return;
    }

    let type;
    let subtype;
    let serviceTargetType = null;
    let serviceTargetName = null;

    const httpPortFromScheme = function (scheme) {
      if (scheme === 'http') {
        return 80;
      } else if (scheme === 'https') {
        return 443;
      }
      return -1;
    };

    // Extracts 'host:port' from URL.
    const parseNetName = function (url) {
      let u;
      try {
        u = new URL(url); // https://developer.mozilla.org/en-US/docs/Web/API/URL
      } catch (_err) {
        return undefined;
      }
      if (u.port !== '') {
        return u.host; // host:port already in URL
      } else {
        var port = httpPortFromScheme(
          u.protocol.substring(0, u.protocol.length - 1),
        );
        return port > 0 ? u.hostname + ':' + port : u.hostname;
      }
    };

    let netPort = attrs['net.peer.port'] || -1;
    const netPeer = attrs['net.peer.name'] || attrs['net.peer.ip'];
    let netName = netPeer; // netName includes port, if provided
    if (netName && netPort > 0) {
      netName += ':' + netPort;
    }

    if (attrs['db.system']) {
      type = 'db';
      subtype = attrs['db.system'];
      serviceTargetType = subtype;
      serviceTargetName = attrs['db.name'] || null;
    } else if (attrs['messaging.system']) {
      type = 'messaging';
      subtype = attrs['messaging.system'];
      if (!netName && attrs['messaging.url']) {
        netName = parseNetName(attrs['messaging.url']);
      }
      serviceTargetType = subtype;
      serviceTargetName = attrs['messaging.destination'] || null;
    } else if (attrs['rpc.system']) {
      type = 'external';
      subtype = attrs['rpc.system'];
      serviceTargetType = subtype;
      serviceTargetName = netName || attrs['rpc.service'] || null;
    } else if (attrs['http.url'] || attrs['http.scheme']) {
      type = 'external';
      subtype = 'http';
      serviceTargetType = 'http';
      const httpHost = attrs['http.host'] || netPeer;
      if (httpHost) {
        if (netPort < 0) {
          netPort = httpPortFromScheme(attrs['http.scheme']);
        }
        serviceTargetName = netPort < 0 ? httpHost : httpHost + ':' + netPort;
      } else if (attrs['http.url']) {
        serviceTargetName = parseNetName(attrs['http.url']);
      }
    }

    if (type === undefined) {
      if (otelKind === otel.SpanKind.INTERNAL) {
        type = 'app';
        subtype = 'internal';
      } else {
        type = 'unknown';
      }
    }

    this._span.type = type;
    if (subtype) {
      this._span.subtype = subtype;
    }
    if (serviceTargetType || serviceTargetName) {
      this._span.setServiceTarget(serviceTargetType, serviceTargetName);
    }
  }