{"record":{"id":"89a181a7152ae364","repo":"apache/seatunnel","slug":"already-an-mdcfunction","errorCode":null,"errorMessage":"Already an MDCFunction","messagePattern":"Already an MDCFunction","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"seatunnel-api/src/main/java/org/apache/seatunnel/api/tracing/MDCTracer.java","lineNumber":158,"sourceCode":"\n    public static <T> MDCConsumer<T> tracing(MDCContext context, Consumer<T> delegate) {\n        if (delegate instanceof MDCConsumer) {\n            throw new IllegalArgumentException(\"Already an MDCConsumer\");\n        }\n        return new MDCConsumer<>(context, delegate);\n    }\n\n    public static <T, R> MDCFunction<T, R> tracing(Function<T, R> delegate) {\n        return tracing(MDCContext.current(), delegate);\n    }\n\n    public static <T, R> MDCFunction<T, R> tracing(Long jobId, Function<T, R> delegate) {\n        return tracing(MDCContext.of(jobId), delegate);\n    }\n\n    public static <T, R> MDCFunction<T, R> tracing(MDCContext context, Function<T, R> delegate) {\n        if (delegate instanceof MDCFunction) {\n            throw new IllegalArgumentException(\"Already an MDCFunction\");\n        }\n        return new MDCFunction<>(context, delegate);\n    }\n\n    public static <T> MDCPredicate<T> tracing(Predicate<T> delegate) {\n        return tracing(MDCContext.current(), delegate);\n    }\n\n    public static <T> MDCPredicate<T> tracing(Long jobId, Predicate<T> delegate) {\n        return tracing(MDCContext.of(jobId), delegate);\n    }\n\n    public static <T> MDCPredicate<T> tracing(MDCContext context, Predicate<T> delegate) {\n        if (delegate instanceof MDCPredicate) {\n            throw new IllegalArgumentException(\"Already an MDCPredicate\");\n        }\n        return new MDCPredicate<>(context, delegate);\n    }","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-api/src/main/java/org/apache/seatunnel/api/tracing/MDCTracer.java#L140-L176","documentation":"MDCTracer.tracing(context, Function) rejects a delegate already wrapped as MDCFunction with IllegalArgumentException. This prevents nested MDC capture/restore around a single function application, which would restore stale state.","triggerScenarios":"Calling tracing(jobId, function) or tracing(context, function) where function is already an MDCFunction from a previous tracing() call, e.g. mapping functions wrapped by both a transform utility and the caller.","commonSituations":"Transform chains where map() helpers and the engine both trace the same Function; caching traced functions and re-tracing.","solutions":["Trace the Function once at the outermost composition point","Pass through already-traced instances: `if (f instanceof MDCFunction) return f;`","Unwrap the duplicated tracing call in one of the layers"],"exampleFix":"// before\nMDCFunction<T,R> f = MDCTracer.tracing(jobId, MDCTracer.tracing(jobId, fn));\n// after\nMDCFunction<T,R> f = MDCTracer.tracing(jobId, fn);","handlingStrategy":"type-guard","validationCode":"MDCFunction<T,R> safeWrap(Long jobId, Function<T,R> f) {\n    return f instanceof MDCFunction ? (MDCFunction<T,R>) f : MDCTracer.tracing(jobId, f);\n}","typeGuard":"boolean isWrapped(Function<?,?> f) {\n    return f instanceof MDCFunction;\n}","tryCatchPattern":"try {\n    return MDCTracer.tracing(jobId, function);\n} catch (IllegalArgumentException e) {\n    return (MDCFunction<T,R>) function;\n}","preventionTips":["Trace functions once at composition time","Avoid caching traced functions and wrapping them again on reuse","Use an instanceof MDCFunction pass-through guard in mapping helpers"],"tags":["java","mdc","tracing","double-wrap"],"backgroundTag":"invalid-argument-value","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}