export function getReadableSpanTransformer()

in packages/opentelemetry-cloud-trace-exporter/src/transform.ts [38:97]


export function getReadableSpanTransformer(
  projectId: string,
  resourceFilter?: RegExp | undefined,
  stringifyArrayAttributes?: boolean
): (span: ReadableSpan) => Span {
  return span => {
    // @todo get dropped attribute count from sdk ReadableSpan
    const attributes = mergeAttributes(
      transformAttributes(
        {
          ...span.attributes,
          [AGENT_LABEL_KEY]: AGENT_LABEL_VALUE,
        },
        stringifyArrayAttributes
      ),
      // Add in special g.co/r resource labels
      transformResourceToAttributes(
        span.resource,
        projectId,
        resourceFilter,
        stringifyArrayAttributes
      )
    );

    const out: Span = {
      attributes,
      displayName: stringToTruncatableString(span.name),
      links: {
        link: span.links.map(getLinkTransformer(stringifyArrayAttributes)),
      },
      endTime: transformTime(span.endTime),
      startTime: transformTime(span.startTime),
      name: `projects/${projectId}/traces/${span.spanContext().traceId}/spans/${
        span.spanContext().spanId
      }`,
      spanKind: transformKind(span.kind),
      spanId: span.spanContext().spanId,
      sameProcessAsParentSpan: {value: !span.spanContext().isRemote},
      status: transformStatus(span.status),
      timeEvents: {
        timeEvent: span.events.map(e => ({
          time: transformTime(e.time),
          annotation: {
            attributes: transformAttributes(
              e.attributes ?? {},
              stringifyArrayAttributes
            ),
            description: stringToTruncatableString(e.name),
          },
        })),
      },
    };

    if (span.parentSpanId) {
      out.parentSpanId = span.parentSpanId;
    }

    return out;
  };
}