@opentelemetry/baggage-span-processor
Version:
OpenTelemetry Baggage Span Processor for Node.js
85 lines • 3.04 kB
JavaScript
;
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaggageSpanProcessor = void 0;
const api_1 = require("@opentelemetry/api");
/**
* BaggageSpanProcessor is a {@link SpanProcessor} that reads entries stored in {@link Baggage}
* from the parent context and adds the baggage entries' keys and
* values to the span as attributes on span start.
*
* Add this span processor to a tracer provider.
*
* Keys and values added to Baggage will appear on subsequent child
* spans for a trace within this service *and* be propagated to external
* services in accordance with any configured propagation formats
* configured. If the external services also have a Baggage span
* processor, the keys and values will appear in those child spans as
* well.
*
* ⚠ Warning ⚠️
*
* Do not put sensitive information in Baggage.
*
* To repeat: a consequence of adding data to Baggage is that the keys and
* values will appear in all outgoing HTTP headers from the application.
*/
class BaggageSpanProcessor {
_keyPredicate;
/**
* Constructs a new BaggageSpanProcessor instance.
* @param keyPredicate A predicate that determines whether a baggage key-value pair should be added to new spans as a span attribute.
*/
constructor(keyPredicate) {
this._keyPredicate = keyPredicate;
}
/**
* Forces to export all finished spans
*/
forceFlush() {
// no-op
return Promise.resolve();
}
/**
* Called when a {@link Span} is started, if the `span.isRecording()`
* returns true.
* @param span the Span that just started.
*/
onStart(span, parentContext) {
(api_1.propagation.getBaggage(parentContext)?.getAllEntries() ?? [])
.filter(entry => this._keyPredicate(entry[0]))
.forEach(entry => span.setAttribute(entry[0], entry[1].value));
}
/**
* Called when a {@link ReadableSpan} is ended, if the `span.isRecording()`
* returns true.
* @param span the Span that just ended.
*/
onEnd(_) {
// no-op
}
/**
* Shuts down the processor. Called when SDK is shut down. This is an
* opportunity for processor to do any cleanup required.
*/
shutdown() {
// no-op
return Promise.resolve();
}
}
exports.BaggageSpanProcessor = BaggageSpanProcessor;
//# sourceMappingURL=baggage-span-processor.js.map