liveBook · Manning

Cgo

As explained in Logs and Telemetry

cgo is a Go toolchain facility that enables Go programs to interoperate with C code and libraries.

cgo connects Go plugin code to Fluent Bit’s C core without recompiling Fluent Bit. It maps Go functions to C headers, passes parameters across language boundaries, and helps manage memory at the binding layer. section 10.4 section 10.11

A cgo-based Fluent Bit extension typically involves these pieces:

  • Implement Go functions for each plugin lifecycle phase and expose the required C-compatible entry points. section 10.11 section 10.4
  • Compile the binding into a shared object file with a .so extension that Fluent Bit can load at startup. section 10.4
  • Use Fluent Bit configuration attributes to tailor plugin behavior, while respecting its naming constraints. section 10.11
  • Account for hot reload, which can reload plugin shared objects without fully restarting Fluent Bit. section 10.4
  • Compile separately for each operating system and CPU architecture when distributing the plugin across platforms. section 10.4

Keep database or other domain logic independent from the cgo layer when possible. This permits more agnostic local testing and isolates extension behavior from language-boundary mechanics. Shared input and output logic can be organized separately and included during each plugin build. section 10.4.2 section 10.4.2

Passing output-plugin data across the boundary

An output callback receives context and payload data through C-compatible pointers. The implementation retrieves plugin context, converts configuration into insertion parameters, decodes MessagePack records, converts values into Go types, and sends them to the database. Pointer-based context handling uses cgo C functions so data can pass safely between Go and C. section 10.6.4

Figure 10.2 The straight lines represent development relationships. The curved lines are execution-related relationships. The dashed lines show what artifacts contribute to the creation of the artifact pointed at. The diamonds represent our generated .so (shared object) files. The Dockerfile drives everything.
figure

Use the repository relationship diagram to trace how source files, Dockerfile steps, generated shared objects, and runtime execution relate. section 10.4.2

How Logs and Telemetry covers it

How deep it goes
Explains function mapping, memory transfer, shared-object loading, hot reload, and platform-specific compilation.
Examples
Uses input and output plugins, database logic, shared-object builds, Dockerfile relationships, and an output flush callback.
What it assumes
Assumes Go and C plugin development concepts, while illustrating their boundary through Fluent Bit extension code.
What you can do after
Build Go extensions that Fluent Bit loads as shared objects and update them through hot reload.
How it connects
Connects Go, C, Fluent Bit plugins, shared objects, unsafe pointers, MessagePack, and hot reload.

Also explained in

Compare how 2 books explain cgo →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Logs and Telemetry. The section links point to the parts of the book it draws on.