Skip to content

Commit c41966d

Browse files
committed
delete unused rot.zig file, rework examples
Signed-off-by: inge4pres <[email protected]>
1 parent 463406b commit c41966d

File tree

5 files changed

+121
-281
lines changed

5 files changed

+121
-281
lines changed

build.zig

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ pub fn build(b: *std.Build) !void {
2929
// Examples step
3030
const examples_step = b.step("examples", "Build and run all example executables");
3131

32-
// Basic example showing HTTP and JVM attribute usage
32+
// Example files
3333
const example_files: []const []const u8 = &.{
34-
"basic_usage.zig",
34+
"basic.zig",
35+
"advanced.zig",
3536
};
3637

3738
// Create executable for each example
3839
for (example_files) |example| {
3940
const example_exe = b.addExecutable(.{
4041
.name = std.mem.trim(u8, example, ".zig"),
41-
.root_source_file = try b.path("examples").join(b.allocator, example),
42-
.target = target,
43-
.optimize = optimize,
42+
.root_module = b.createModule(.{
43+
.root_source_file = try b.path("examples").join(b.allocator, example),
44+
.target = target,
45+
.optimize = .ReleaseSafe,
46+
}),
4447
});
4548

4649
// Add the semconv module to each example
@@ -53,4 +56,10 @@ pub fn build(b: *std.Build) !void {
5356
examples_step.dependOn(&example_exe.step);
5457
examples_step.dependOn(&run_examples.step);
5558
}
59+
60+
// Add a step to generate the code via Weaver
61+
const script_path = try b.path("scripts").join(b.allocator, "generate-consts-from-spec.sh");
62+
const bash_generator = b.addSystemCommand(&.{script_path.getPath(b)});
63+
const gen_step = b.step("generate", "Generate code via Weaver");
64+
gen_step.dependOn(&bash_generator.step);
5665
}

examples/advanced.zig

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//! Advanced example showing how to use the type system to filter
2+
//! attributes and metrics based on stability levels
3+
4+
const std = @import("std");
5+
const semconv = @import("opentelemetry-semconv");
6+
7+
pub fn main() !void {
8+
// Example 1: Using stability information to filter attributes
9+
10+
// Stable attribute
11+
const http_method = semconv.attribute.http_request_method;
12+
std.debug.assert(http_method.base.stability == .stable);
13+
14+
// Experimental attribute
15+
const android_state = semconv.attribute.android_app_state;
16+
std.debug.assert(android_state.base.stability == .development);
17+
18+
// Example 2: Checking requirement levels
19+
const service_name = semconv.resource.service_name;
20+
std.debug.assert(service_name.requirement_level == .required);
21+
22+
// Example 3: Working with enum attributes and their values
23+
const http_method_enum = semconv.attribute.http_request_method;
24+
std.debug.assert(std.mem.eql(u8, http_method_enum.base.name, "http.request.method"));
25+
26+
const methods = [_]@TypeOf(semconv.attribute.http_request_methodValue.get){
27+
semconv.attribute.http_request_methodValue.get,
28+
semconv.attribute.http_request_methodValue.post,
29+
semconv.attribute.http_request_methodValue.put,
30+
semconv.attribute.http_request_methodValue.delete,
31+
};
32+
33+
std.debug.assert(std.mem.eql(u8, methods[0].toString(), "GET"));
34+
std.debug.assert(std.mem.eql(u8, methods[1].toString(), "POST"));
35+
std.debug.assert(std.mem.eql(u8, methods[2].toString(), "PUT"));
36+
std.debug.assert(std.mem.eql(u8, methods[3].toString(), "DELETE"));
37+
38+
// Example 4: Accessing metric metadata
39+
const http_duration = semconv.metric.http_server_request_duration;
40+
std.debug.assert(std.mem.eql(u8, http_duration.name, "http.server.request.duration"));
41+
std.debug.assert(http_duration.instrument == .histogram);
42+
std.debug.assert(std.mem.eql(u8, http_duration.unit, "s"));
43+
std.debug.assert(http_duration.stability == .stable);
44+
std.debug.assert(http_duration.value_type == .double);
45+
46+
// Example 5: Runtime stability check
47+
48+
// Check stability of enum attributes (with .base field)
49+
const http_method_attr = semconv.attribute.http_request_method;
50+
std.debug.assert(http_method_attr.base.stability == .stable);
51+
52+
// Check stability of regular attributes (without .base field)
53+
const http_status_attr = semconv.attribute.http_response_status_code;
54+
std.debug.assert(http_status_attr.stability == .stable);
55+
56+
const http_route_attr = semconv.attribute.http_route;
57+
std.debug.assert(http_route_attr.stability == .stable);
58+
59+
// Example 6: Working with enum vs simple attributes
60+
61+
// Enum attributes have both .base and well_known_values
62+
std.debug.assert(std.mem.eql(u8, semconv.attribute.jvm_memory_type.base.name, "jvm.memory.type"));
63+
std.debug.assert(std.mem.eql(u8, semconv.attribute.jvm_memory_typeValue.heap.toString(), "heap"));
64+
std.debug.assert(std.mem.eql(u8, semconv.attribute.jvm_memory_typeValue.non_heap.toString(), "non_heap"));
65+
66+
// Simple attributes only have the attribute fields directly
67+
const db_name = semconv.attribute.db_name;
68+
std.debug.assert(std.mem.eql(u8, db_name.name, "db.name"));
69+
std.debug.assert(db_name.stability == .development);
70+
}

examples/basic.zig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Basic example showing how to access attributes and metrics
2+
//! from the OpenTelemetry semantic conventions
3+
4+
const std = @import("std");
5+
const semconv = @import("opentelemetry-semconv");
6+
7+
pub fn main() !void {
8+
// Accessing attributes from the attribute namespace
9+
const http_method = semconv.attribute.http_request_method;
10+
std.debug.assert(std.mem.eql(u8, http_method.base.name, "http.request.method"));
11+
std.debug.assert(http_method.base.stability == .stable);
12+
13+
// Attribute with enum values
14+
const method_get = semconv.attribute.http_request_methodValue.get;
15+
std.debug.assert(std.mem.eql(u8, method_get.toString(), "GET"));
16+
17+
const http_status = semconv.attribute.http_response_status_code;
18+
std.debug.assert(std.mem.eql(u8, http_status.name, "http.response.status_code"));
19+
std.debug.assert(http_status.stability == .stable);
20+
21+
// Accessing metrics from the metric namespace
22+
const http_server_duration = semconv.metric.http_server_request_duration;
23+
std.debug.assert(std.mem.eql(u8, http_server_duration.name, "http.server.request.duration"));
24+
std.debug.assert(http_server_duration.instrument == .histogram);
25+
std.debug.assert(std.mem.eql(u8, http_server_duration.unit, "s"));
26+
std.debug.assert(http_server_duration.stability == .stable);
27+
28+
// Accessing resource attributes from the resource namespace
29+
const service_name = semconv.resource.service_name;
30+
std.debug.assert(std.mem.eql(u8, service_name.name, "service.name"));
31+
std.debug.assert(service_name.requirement_level == .required);
32+
33+
// Accessing trace attributes from the trace namespace
34+
const db_operation_name = semconv.trace.db_operation_name;
35+
std.debug.assert(std.mem.eql(u8, db_operation_name.name, "db.operation.name"));
36+
std.debug.assert(db_operation_name.stability == .stable);
37+
}

examples/basic_usage.zig

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)