Conduit Processor SDK
This repository contains the Go software development kit for implementing a
processor for Conduit.
Note: if you'd like to use another language for writing processors, feel free to
open an issue and request a processor SDK for a specific language.
Quick Start
Create a new folder and initialize a fresh go module:
go mod init example.com/conduit-processor-example
Add the processor SDK dependency:
go get github.com/conduitio/conduit-processor-sdk
You can now create a new processor by implementing the
Processor
interface. For more details about that, check our documentation for
Building Standalone Processors.
On the other hand, if the processor is very simple and can be reduced to a single function (e.g.
no configuration needed), then we can use sdk.NewProcessorFunc(), as below:
//go:build wasm
package main
import (
sdk "github.com/conduitio/conduit-processor-sdk"
)
func main() {
sdk.Run(sdk.NewProcessorFunc(
sdk.Specification{Name: "example-processor"},
func(ctx context.Context, rec opencdc.Record) (opencdc.Record, error) {
// do something with the record
return rec, nil
},
))
}
With this, you are set to build your processor. Note that we are building the
processor as a WebAssembly module, so you need to set GOARCH and GOOS:
GOARCH=wasm GOOS=wasip1 go build -o example-processor.wasm main.go
The produced example-processor.wasm file can be used as a processor in
Conduit. Copy it to the processors directory of your Conduit instance and
configure it in the processors section of the pipeline configuration file.
Schema encoding
Before Process, the SDK decodes a record's key and payload with the schema the
record carries. After Process, it encodes them again. Data that still fits the
carried schema is encoded with it. When a processor reshapes the data (for
example, it adds a field), the SDK has to register a new schema version under the
carried subject. Two parameters, present on every processor, choose how:
| Parameter |
Values |
Default |
sdk.schema.encode.payload.strategy |
infer, extend |
infer |
sdk.schema.encode.key.strategy |
infer, extend |
infer |
infer extracts a schema from the values. Avro int becomes long, date
becomes timestamp-micros, decimals become decimal(31,10), and enums and named
types are lost. before and after share one schema, inferred so that it
fits both: an update where a nullable column goes from a value to NULL (or
back) encodes both sides. A field that before has and after lacks, at any
depth, fails the record instead of disappearing from before. This happens
with field.exclude on .Payload.After.x for an update with a full before
image. Remove the field from both parts.
extend keeps the carried definition of every field still present, adds new
top-level fields as nullable with a null default, and leaves out removed
top-level fields. A nil or zero-length raw part is passed through unencoded,
with its metadata unchanged. A part whose fields were all removed ({}) gets a
record schema with zero fields, registered as a new version of the carried
subject; it encodes to zero bytes, so downstream it is an empty value that
still carries a schema ID. A value that does not fit its carried type fails the
record (ErrSchemaExtendMismatch), and a carried schema that cannot be extended
fails it with ErrSchemaExtendUnsupported.
extend fails closed in some cases where infer succeeds by widening or
truncating:
before and after with different top-level fields.
- An added
time.Time finer than a microsecond (for example time.Now() on
Linux): the added field is inferred as timestamp-micros, which cannot hold
nanoseconds. Truncate the value in the processor
(t.Truncate(time.Microsecond)) or attach a schema.
- A kept field whose carried type changed, or a key added to or removed from a
nested record.
- A kept field that refers to a named type defined only in a removed field.
- One payload part structured and the other still non-empty raw bytes, which
cannot be re-encoded with the extended schema.
extend is opt-in in v0.22. In v0.23 infer warns when it widens a carried
schema, and in v0.24 extend becomes the default. See
the design document.
FAQ
Why do I need to specify GOARCH and GOOS?
Conduit uses WebAssembly to run standalone processors.
This means that you need to build your processor as a WebAssembly module. You can
do this by setting the environment variables GOARCH=wasm and GOOS=wasip1 when
running go build. This will produce a WebAssembly module that can be used as a
processor in Conduit.
How do I use a processor?
To use a standalone WASM processor in Conduit, the following two steps need to be
done:
Copying the WebAssembly module to the processors directory of your Conduit
instance. By default, that's a directory called processors that is in the same
directory as Conduit. The directory can be changed with the -processors.path flag.
An example directory structure would be:
.
├── conduit
└── processors
└── example-processor.wasm
Use the processor in the processors section of the pipeline configuration file.
using the name the processor defines in its specifications. For example:
processors:
- id: my-example-processor
plugin: example-processor
settings:
field: 'foo'
value: 'bar'
How do I log in a processor?
You can get a zerolog.Logger instance
from the context using the
sdk.Logger
function. This logger is pre-configured to append logs in the format expected by
Conduit.
Keep in mind that logging in the hot path (i.e. in the Process method) can have
a significant impact on the performance of your processor, therefore we recommend
to use the Trace level for logs that are not essential for the operation of the
processor.
Example:
func (p *ExampleProcessor) Process(ctx context.Context, records []opencdc.Record) []sdk.ProcessedRecord {
logger := sdk.Logger(ctx)
logger.Trace().Msg("Processing records")
// ...
}