* Flush spans of a specific trace. * In contrast to SpanBuffer.drain, this method does not flush all traces, but only the one with the given traceId.
(traceId: string)
| 145 | * In contrast to {@link SpanBuffer.drain}, this method does not flush all traces, but only the one with the given traceId. |
| 146 | */ |
| 147 | public flush(traceId: string): void { |
| 148 | const bucket = this._traceBuckets.get(traceId); |
| 149 | if (!bucket) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (!bucket.spans.size) { |
| 154 | // we should never get here, given we always add a span when we create a new bucket |
| 155 | // and delete the bucket once we flush out the trace |
| 156 | this._removeTrace(traceId); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | const spans = Array.from(bucket.spans); |
| 161 | |
| 162 | const segmentSpan = spans[0]?._segmentSpan; |
| 163 | if (!segmentSpan) { |
| 164 | DEBUG_BUILD && debug.warn('No segment span reference found on span JSON, cannot compute DSC'); |
| 165 | this._removeTrace(traceId); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | const dsc = getDynamicSamplingContextFromSpan(segmentSpan); |
| 170 | |
| 171 | const cleanedSpans: SerializedStreamedSpan[] = spans.map(spanJSON => { |
| 172 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 173 | const { _segmentSpan, ...cleanSpanJSON } = spanJSON; |
| 174 | return cleanSpanJSON; |
| 175 | }); |
| 176 | |
| 177 | const envelope = createStreamedSpanEnvelope(cleanedSpans, dsc, this._client); |
| 178 | |
| 179 | DEBUG_BUILD && debug.log(`Sending span envelope for trace ${traceId} with ${cleanedSpans.length} spans`); |
| 180 | |
| 181 | this._client.sendEnvelope(envelope).then(null, reason => { |
| 182 | DEBUG_BUILD && debug.error('Error while sending streamed span envelope:', reason); |
| 183 | }); |
| 184 | |
| 185 | this._removeTrace(traceId); |
| 186 | } |
| 187 | |
| 188 | private _removeTrace(traceId: string): void { |
| 189 | const bucket = this._traceBuckets.get(traceId); |
no test coverage detected