Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,57 @@ const sdk = b.dependency("opentelemetry-sdk", .{
});
```

## C Language Bindings

The SDK provides C-compatible bindings, allowing C programs to use OpenTelemetry instrumentation. The C API covers all three signals: Traces, Metrics, and Logs.

### Using from C

1. **Link with the compiled library**: Build the Zig library and link it with your C project.

2. **Include the header**: Add `include/opentelemetry.h` to your project.

3. **Basic usage example**:

```c
#include "opentelemetry.h"

int main() {
// Create a meter provider
otel_meter_provider_t* provider = otel_meter_provider_create();

// Create an exporter and reader
otel_metric_exporter_t* exporter = otel_metric_exporter_stdout_create();
otel_metric_reader_t* reader = otel_metric_reader_create(exporter);
otel_meter_provider_add_reader(provider, reader);

// Get a meter
otel_meter_t* meter = otel_meter_provider_get_meter(
provider, "my-service", "1.0.0", NULL);

// Create and use a counter
otel_counter_u64_t* counter = otel_meter_create_counter_u64(
meter, "requests", "Total requests", "1");
otel_counter_add_u64(counter, 1, NULL, 0);

// Collect and export metrics
otel_metric_reader_collect(reader);

// Cleanup
otel_meter_provider_shutdown(provider);
return 0;
}
```

### C API Features

- **Opaque handles**: All SDK objects are exposed as opaque handles for type safety
- **Memory management**: The C API manages memory internally using page allocators
- **Error handling**: Functions return status codes (0 for success, negative for errors)
- **Examples**: See `examples/c/` for complete examples of traces, metrics, and logs

For detailed API documentation, refer to `include/opentelemetry.h`.

## Specification Support State

### Signals
Expand All @@ -64,7 +115,9 @@ const sdk = b.dependency("opentelemetry-sdk", .{

## Examples

Check out the [examples](./examples) folder for practical usage examples of traces, metrics, and logs.
Check out the [examples](./examples) folder for practical usage examples:
- `examples/` - Zig examples for traces, metrics, and logs
- `examples/c/` - C language examples demonstrating the C API bindings

## Contributing

Expand Down
56 changes: 55 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,27 @@ pub fn build(b: *std.Build) !void {
},
});

// Static library for the OpenTelemetry SDK C users
const sdk_lib = b.addLibrary(.{
.name = "opentelemetry-sdk",
.root_module = sdk_mod,
.linkage = .static,
.root_module = b.createModule(.{
.root_source_file = b.path("src/c.zig"),
.target = target,
.optimize = optimize,
}),
});
sdk_lib.linkLibrary(zlib_lib); // TODO: remove when 0.16.0 is released

b.installArtifact(sdk_lib);

// Install include headers for C users
b.installDirectory(.{
.source_dir = b.path("include"),
.install_dir = .header,
.install_subdir = "",
});

// Providing a way for the user to request running the unit tests.
const test_step = b.step("test", "Run unit tests");

Expand Down Expand Up @@ -151,6 +164,47 @@ pub fn build(b: *std.Build) !void {
}
}

// C examples
const c_example_step = b.step("examples-c", "Build and run C examples");

const c_examples = [_][]const u8{
"logs",
"metrics",
"trace",
};

for (c_examples) |example_name| {
const c_example_exe = b.addExecutable(.{
.name = example_name,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});

c_example_exe.addCSourceFile(.{
.file = b.path(b.pathJoin(&.{
"examples",
"c",
b.fmt("{s}.c", .{example_name}),
})),
.flags = &.{
"-std=c11",
"-Wall",
"-Wextra",
},
});
c_example_exe.addIncludePath(b.path("include"));
c_example_exe.linkLibrary(sdk_lib);

const run_c_example = b.addRunArtifact(c_example_exe);
c_example_step.dependOn(&run_c_example.step);

// Also install each C example executable
b.installArtifact(c_example_exe);
}

// Benchmarks
const benchmarks_step = b.step("benchmarks", "Build and run all benchmarks");

Expand Down
Loading