|
| 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 | +} |
0 commit comments