ExperimentalThe function to wrap with tracing capabilities
Optionaloptions: SpanTraceOptions<Fn>Configuration options for tracing behavior
Configuration options for span tracing in OpenInference.
This interface defines all the available options for customizing how functions are traced, including span naming, tracer selection, span classification, and custom input/output processing. These options are used by tracing decorators and wrapper functions to control the tracing behavior.
Optionalattributes?: AttributesBase attributes to be added to every span created with these options.
These attributes will be merged with any attributes generated by input/output processors and OpenInference semantic attributes. Base attributes are useful for adding consistent metadata like service information, version numbers, environment details, or any other static attributes that should be present on all spans.
Optionalkind?: The OpenInference span kind for semantic categorization in LLM applications.
This provides domain-specific classification for AI/ML operations, helping to organize and understand the different types of operations in an LLM workflow.
Optionalname?: stringCustom name for the span.
If not provided, the name of the decorated function or wrapped function will be used as the span name. This is useful for providing more descriptive or standardized names for operations.
OptionalopenTelemetrySpanKind?: SpanKindThe OpenTelemetry span kind to classify the span's role in a trace.
This determines how the span is categorized in the OpenTelemetry ecosystem and affects how tracing tools display and analyze the span.
OptionalprocessInput?: InputToAttributesFn<Fn>Custom function to process input arguments into span attributes.
This allows for custom serialization and attribute extraction from function arguments. If not provided, the default input processor will be used, which safely JSON-stringifies the arguments.
OptionalprocessOutput?: OutputToAttributesFn<Fn>Custom function to process output values into span attributes.
This allows for custom serialization and attribute extraction from function return values. If not provided, the default output processor will be used, which safely JSON-stringifies the result.
Optionaltracer?: TracerCustom OpenTelemetry tracer instance to use for this span.
If not provided, the global tracer will be used. This allows for using different tracers for different parts of the application or for testing purposes with mock tracers.
A wrapped function with identical signature that creates spans during execution
// Basic function wrapping
const add = (a: number, b: number) => a + b;
const tracedAdd = withSpan(add);
const result = tracedAdd(2, 3); // Creates a span named "add"
// Async function with custom options
const fetchData = async (url: string) => {
const response = await fetch(url);
return response.json();
};
const tracedFetch = withSpan(fetchData, {
name: "api-request",
kind: OpenInferenceSpanKind.LLM
});
// Custom input/output processing with base attributes
const processUser = (user: User) => ({ ...user, processed: true });
const tracedProcess = withSpan(processUser, {
attributes: {
'service.name': 'user-processor',
'service.version': '1.0.0'
},
processInput: (user) => ({ "user.id": user.id }),
processOutput: (result) => ({ "result.processed": result.processed })
});
Wraps a function with openinference tracing capabilities, creating spans for execution monitoring.
This function provides comprehensive tracing for both synchronous and asynchronous functions, automatically handling span lifecycle, input/output processing, error tracking, and promise resolution.
This API is experimental and may change in future versions