Skip to content

Commit 16f0a27

Browse files
committed
improve README
Signed-off-by: inge4pres <[email protected]>
1 parent c41966d commit 16f0a27

File tree

1 file changed

+149
-55
lines changed

1 file changed

+149
-55
lines changed

README.md

Lines changed: 149 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ A strongly-typed semantic convention library for OpenTelemetry instrumentation i
44

55
## Overview
66

7-
This library provides Zig type definitions for OpenTelemetry semantic conventions, allowing you to use semantic convention attributes in a type-safe way in your Zig applications. The library mirrors the structure of the official [OpenTelemetry semantic conventions](https://github.com/open-telemetry/semantic-conventions) repository.
7+
This library provides Zig type definitions for OpenTelemetry semantic conventions, allowing you to use semantic convention attributes, metrics, resources, and traces in a type-safe way in your Zig applications. The library is generated from the official [OpenTelemetry semantic conventions](https://github.com/open-telemetry/semantic-conventions) specification.
88

99
## Features
1010

1111
- **Strongly Typed**: All semantic convention attributes are defined with proper Zig types
12-
- **Comprehensive**: Covers major semantic convention namespaces (HTTP, Network, URL, Server, Client, Error, User Agent, Telemetry)
13-
- **Standards Compliant**: Based on OpenTelemetry specification v1.28.0
12+
- **Comprehensive**: Covers all semantic convention namespaces (attributes, metrics, resources, traces)
13+
- **Standards Compliant**: Based on OpenTelemetry specification v1.36.0
1414
- **Zero Dependencies**: Uses only Zig standard library
15-
- **Well Tested**: Comprehensive test coverage for all modules
15+
- **Auto-generated**: Generated directly from the official semantic conventions specification
16+
- **Rich Metadata**: Includes stability levels, requirement levels, descriptions, and well-known enum values
1617

1718
## Usage
1819

@@ -23,61 +24,101 @@ const std = @import("std");
2324
const semconv = @import("opentelemetry-semconv");
2425
2526
pub fn main() !void {
26-
// Use HTTP semantic conventions
27-
const http_method_attr = semconv.http.Registry.http_request_method;
28-
const method_value = semconv.http.Registry.requestMethodValue.get;
29-
30-
// Use JVM semantic conventions
31-
const jvm_version_attr = semconv.jvm.Registry.jvm_version;
32-
33-
// Here you would actually set the attribute on metrics data point, log/event or trace attributes
34-
std.debug.print("HTTP Method: {s} = {s}\n", .{ http_method_attr.base.name, method_value.toString() });
35-
std.debug.print("JVM Version attribute: {s}\n", .{jvm_version_attr.name});
27+
// Accessing attributes from the attribute namespace
28+
const http_method = semconv.attribute.http_request_method;
29+
std.debug.assert(std.mem.eql(u8, http_method.base.name, "http.request.method"));
30+
std.debug.assert(http_method.base.stability == .stable);
31+
32+
// Attribute with enum values
33+
const method_get = semconv.attribute.http_request_methodValue.get;
34+
std.debug.assert(std.mem.eql(u8, method_get.toString(), "GET"));
35+
36+
// Accessing metrics from the metric namespace
37+
const http_server_duration = semconv.metric.http_server_request_duration;
38+
std.debug.assert(http_server_duration.instrument == .histogram);
39+
std.debug.assert(std.mem.eql(u8, http_server_duration.unit, "s"));
40+
41+
// Accessing resource attributes
42+
const service_name = semconv.resource.service_name;
43+
std.debug.assert(service_name.requirement_level == .required);
44+
45+
// Accessing trace attributes
46+
const db_operation_name = semconv.trace.db_operation_name;
47+
std.debug.assert(db_operation_name.stability == .stable);
3648
}
3749
```
3850

3951
### Available Namespaces
4052

41-
All the namespaces from the official OpenTelemetry semconv are defined, most notably:
53+
The library provides four main namespaces:
54+
55+
- `semconv.attribute` - All attribute definitions
56+
- `semconv.metric` - All metric definitions
57+
- `semconv.resource` - Resource attribute definitions
58+
- `semconv.trace` - Trace-specific attribute definitions
4259

43-
- `semconv.http` - HTTP semantic conventions
44-
- `semconv.network` - Network-level semantic conventions
45-
- `semconv.url` - URL semantic conventions
46-
- `semconv.server` - Server semantic conventions
47-
- `semconv.client` - Client semantic conventions
48-
- `semconv."@error"` - Error semantic conventions
49-
- `semconv.user_agent` - User Agent semantic conventions
50-
- `semconv.telemetry` - Telemetry SDK semantic conventions
60+
### Working with Stability Levels
61+
62+
Use stability information to filter attributes and make informed decisions:
63+
64+
```zig
65+
const semconv = @import("opentelemetry-semconv");
66+
67+
// Check if an attribute is stable
68+
const http_method = semconv.attribute.http_request_method;
69+
if (http_method.base.stability == .stable) {
70+
// Safe to use in production
71+
}
72+
73+
// Filter experimental attributes
74+
const android_state = semconv.attribute.android_app_state;
75+
if (android_state.base.stability == .development) {
76+
// Handle experimental attribute
77+
}
78+
```
5179

52-
### Working with Enums
80+
### Working with Enum Attributes
5381

54-
Many semantic conventions include well-known enum values, for example for `http`:
82+
Many attributes include type-safe enum values:
5583

5684
```zig
5785
const semconv = @import("opentelemetry-semconv");
5886
59-
// Access HTTP request method attribute and enum values
60-
const http_method_attr = semconv.http.Registry.http_request_method;
61-
const get_method = semconv.http.Registry.requestMethodValue.get;
87+
// Access HTTP request method attribute
88+
const http_method = semconv.attribute.http_request_method;
6289
63-
// Check stability and use the attribute
64-
const attribute_name = if (http_method_attr.base.stability == .stable)
65-
http_method_attr.base.name
66-
else
67-
"custom.http.request.method";
90+
// Use well-known enum values
91+
const methods = [_]@TypeOf(semconv.attribute.http_request_methodValue.get){
92+
semconv.attribute.http_request_methodValue.get,
93+
semconv.attribute.http_request_methodValue.post,
94+
semconv.attribute.http_request_methodValue.put,
95+
semconv.attribute.http_request_methodValue.delete,
96+
};
6897
69-
std.debug.print("Attribute: {s}, Method: {s}\n", .{ attribute_name, get_method.toString() });
98+
// Convert to string
99+
const method_str = methods[0].toString(); // "GET"
70100
```
71101

72-
You can use stability levels to discern if an attribute should be in place.
102+
### Working with Metrics
103+
104+
Access metric metadata including instrument type, unit, and value type:
105+
106+
```zig
107+
const semconv = @import("opentelemetry-semconv");
73108
74-
Each attribute comes with examples, and the enums allow type-safe assertions.
109+
const http_duration = semconv.metric.http_server_request_duration;
110+
// Metric name: "http.server.request.duration"
111+
// Instrument type: .histogram
112+
// Unit: "s" (seconds)
113+
// Stability: .stable
114+
// Value type: .double
115+
```
75116

76117
## Building
77118

78119
### As a Dependency
79120

80-
Fetch the library with `zig build fetch --save`, then add to your `build.zig`:
121+
Fetch the library with `zig fetch --save "git+https://zig-o11y/opentelemetry-semconv"`, then add to your `build.zig`:
81122

82123
```zig
83124
const semconv_dep = b.dependency("opentelemetry_semconv", .{});
@@ -87,17 +128,42 @@ exe.root_module.addImport("opentelemetry-semconv", semconv_dep.module("opentelem
87128
### Building Standalone
88129

89130
```bash
90-
# Build (if building as executable)
131+
# Build the library
91132
zig build
133+
134+
# Run tests
135+
zig build test
136+
137+
# Run examples
138+
zig build examples
139+
140+
# Generate code from spec (requires Docker)
141+
zig build generate
142+
```
143+
144+
## Examples
145+
146+
The repository includes two example files:
147+
148+
- [examples/basic.zig](examples/basic.zig) - Shows how to access attributes, metrics, resources, and traces
149+
- [examples/advanced.zig](examples/advanced.zig) - Demonstrates stability filtering, enum handling, and type system usage
150+
151+
Run the examples:
152+
153+
```bash
154+
zig build examples
92155
```
93156

94157
## Architecture
95158

96159
The library is organized as follows:
97160

98-
- `src/types.zig` - Core type definitions for attributes, stability levels, etc.
99-
- `src/*/*.zig` - Individual semantic convention namespace modules
100-
- `src/root.zig` - Main library interface
161+
- [src/lib.zig](src/lib.zig) - Main library interface exporting all namespaces
162+
- [src/types.zig](src/types.zig) - Core type definitions for attributes, metrics, stability levels, etc.
163+
- [src/attribute.zig](src/attribute.zig) - All attribute definitions (auto-generated)
164+
- [src/metric.zig](src/metric.zig) - All metric definitions (auto-generated)
165+
- [src/resource.zig](src/resource.zig) - Resource attribute definitions (auto-generated)
166+
- [src/trace.zig](src/trace.zig) - Trace attribute definitions (auto-generated)
101167

102168
### Type System
103169

@@ -119,28 +185,56 @@ EnumAttribute(EnumType) = struct {
119185
well_known_values: EnumType,
120186
}
121187
122-
// Accessing attributes
123-
const http_method = semconv.http.Registry.http_request_method; // EnumAttribute
124-
const http_body_size = semconv.http.Registry.http_request_body_size; // StringAttribute
188+
// Metric type
189+
Metric = struct {
190+
name: []const u8,
191+
brief: []const u8,
192+
stability: StabilityLevel,
193+
instrument: InstrumentType,
194+
unit: []const u8,
195+
value_type: MetricValueType,
196+
}
197+
```
198+
199+
### Stability Levels
200+
201+
- `.stable` - The attribute is stable and should not change in backward-incompatible ways
202+
- `.development` - The attribute is in development and may change
203+
- `.deprecated` - The attribute is deprecated and should not be used
204+
- `.experimental` - The attribute is experimental (alias for development)
205+
206+
### Requirement Levels
207+
208+
- `.required` - The attribute MUST be provided
209+
- `.recommended` - The attribute SHOULD be provided when available
210+
- `.opt_in` - The attribute MAY be provided but is not required or recommended by default
211+
- `.conditionally_required` - The attribute is required under specific conditions
212+
213+
## Code Generation
214+
215+
This library is auto-generated from the OpenTelemetry semantic conventions specification using [Weaver](https://github.com/open-telemetry/weaver).
216+
217+
To regenerate the code:
218+
219+
```bash
220+
zig build generate
125221
```
126222

223+
This will run the generation script that downloads the latest semantic conventions and generates the Zig code.
224+
127225
## Testing
128226

129-
Run the test suite:
227+
Run the tests:
130228

131229
```bash
132230
zig build test
133231
```
134232

135-
All modules include comprehensive tests covering:
136-
- Attribute definitions and metadata
137-
- Enum value parsing and conversion
138-
- Helper function behavior
139-
- Edge cases and validation
140-
233+
## Contributing
141234

142-
## Compatibility
235+
Contributions are welcome! Please note that most of the code is auto-generated from the OpenTelemetry specification. If you want to add features or fix bugs, focus on:
143236

144-
- **Zig Version**: 0.14.x+
145-
- **OpenTelemetry Spec**: v1.28.0
146-
- **Platforms**: All platforms supported by Zig
237+
- [src/types.zig](src/types.zig) - Core type definitions
238+
- [build.zig](build.zig) - Build configuration
239+
- [scripts/](scripts/) - Code generation scripts and templates
240+
- [examples/](examples/) - Example files

0 commit comments

Comments
 (0)