Transaction.prototype.toJSON = function()

in lib/instrumentation/transaction.js [271:344]


Transaction.prototype.toJSON = function () {
  var payload = {
    id: this.id,
    trace_id: this.traceId,
    parent_id: this.parentId,
    name: this.name,
    type: this.type || constants.DEFAULT_SPAN_TYPE,
    duration: this._duration,
    timestamp: this.timestamp,
    result: String(this.result),
    sampled: this.sampled,
    context: undefined,
    span_count: {
      started: this._builtSpans - this._droppedSpans,
    },
    outcome: this.outcome,
    faas: this._faas,
  };

  if (this.sampled) {
    payload.context = {
      user: Object.assign(
        {},
        this.req && parsers.getUserContextFromRequest(this.req),
        this._user,
      ),
      tags: this._labels || {},
      custom: this._custom || {},
      service: this._service || {},
      cloud: this._cloud || {},
      message: this._message || {},
    };
    // Only include dropped count when spans have been dropped.
    if (this._droppedSpans > 0) {
      payload.span_count.dropped = this._droppedSpans;
    }

    var conf = this._agent._conf;
    if (this.req) {
      payload.context.request = parsers.getContextFromRequest(
        this.req,
        conf,
        'transactions',
      );
    }
    if (this.res) {
      payload.context.response = parsers.getContextFromResponse(this.res, conf);
    }
  }

  // add sample_rate to transaction
  // https://github.com/elastic/apm/blob/main/specs/agents/tracing-sampling.md
  // Only set sample_rate on transaction payload if a valid trace state
  // variable is set.
  //
  // "If there is no tracestate or no valid es entry with an s attribute,
  //  then the agent must omit sample_rate from non-root transactions and
  //  their spans."
  const sampleRate = this.sampleRate;
  if (sampleRate !== null) {
    payload.sample_rate = sampleRate;
  }

  this._serializeOTel(payload);

  if (this._links.length > 0) {
    payload.links = this._links;
  }

  if (this._droppedSpansStats.size() > 0) {
    payload.dropped_spans_stats = this._droppedSpansStats.encode();
  }
  return payload;
};