Transforming spans prior to export #2817
-
👋 ProblemSome string-valued span attributes contain information that should not be logged. My Node.js application is exporting to Collector Contrib. There are two processors that stand out as relevant:
Both are works in progress, and as such neither is included in the current Collector Contrib, 0.46.0. Working solutionI thought I'd try making a --- a 2022-03-03 11:16:29.000000000 -0800
+++ b 2022-03-03 11:16:43.000000000 -0800
@@ -58,7 +58,7 @@
// prevent downstream exporter calls from generating spans
context.with(suppressTracing(context.active()), () => {
- this._exporter.export([span], result => {
+ this._exporter.export([redactSpan(span)], result => {
if (result.code !== ExportResultCode.SUCCESS) {
globalErrorHandler(
result.error ?? I first attempt at function redactSpan(span: ReadableSpan): ReadableSpan {
return {
...span,
attributes: redactAttributes(span.attributes),
};
} but I guess the own && enumerable properties copied by the spread operator didn't suffice; these spans subsequently error:
The following works, though! function redactSpan(span: ReadableSpan): ReadableSpan {
const result = Object.create(span);
result.attributes = redactAttributes(span.attributes);
return result;
} Questions
Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Yes, this would be the most pratical way to do this.
You could also do the following: |
Beta Was this translation helpful? Give feedback.
-
You could also create your own |
Beta Was this translation helpful? Give feedback.
-
The suggested approach to create my own I opted for the simpler approach from the OP with a custom const redactAttributes: string[] = ['graphql.execute.variables', 'graphql.resolver.args'];
class RedactingSpanProcessor extends SimpleSpanProcessor {
onEnd(span: ReadableSpan): void {
const attributes = span.attributes;
for (const key of redactAttributes) {
const value = attributes[key];
if (value) {
const redacted = redact(value);
span.attributes[key] = redacted;
}
}
// call underlying SimpleSpanProcessor:
super.onEnd(span);
}
} Put whatever logic you want in your |
Beta Was this translation helpful? Give feedback.
You could also create your own
Span
,Tracer
andTracerProvider
classes by extending the corresponding ones for sdk-trace-base which filters attributes before they are actually stored in the span.This has the advantage that all export chains export the same span not only a specific exporter.