struct
Logit::Event
- Logit::Event
- Struct
- Value
- Object
Overview
A structured log event with OpenTelemetry-compatible fields.
Events are the core data structure passed to backends for logging. Each event contains:
- Trace context (trace_id, span_id, parent_span_id)
- Timing information (timestamp, duration)
- Source location (file, line, method, class)
- Structured attributes
- Exception information (if applicable)
Events are created automatically by the instrumentation system. You
typically interact with events through the Span API or by implementing
custom formatters/backends.
OpenTelemetry Semantic Conventions
Events provide helper methods for setting OpenTelemetry semantic attributes:
if span = Logit::Span.current?
# HTTP attributes
span.attributes.set("http.method", "POST")
span.attributes.set("http.route", "/api/users")
span.attributes.set("http.status_code", 200_i64)
# Database attributes
span.attributes.set("db.system", "postgresql")
span.attributes.set("db.statement", "SELECT * FROM users")
end
JSON Serialization
Events serialize to JSON in an OpenTelemetry-compatible format:
{
"trace_id": "abc123...",
"span_id": "def456...",
"timestamp": "2024-01-15T10:30:00.000000Z",
"duration_ms": 42,
"name": "find_user",
"level": "info",
"code": {
"file": "user_service.cr",
"line": 15,
"function": "find_user",
"namespace": "UserService"
},
"attributes": { ... }
}
Defined in:
logit/events/attributes.crlogit/events/event.cr
Constructors
-
.new(trace_id : String, span_id : String, name : String, level : Logit::LogLevel, code_file : String, code_line : Int32, method_name : String, class_name : String, parent_span_id : Nil | String = nil)
Creates a new event with the given parameters.
Instance Method Summary
-
#attributes : Event::Attributes
Structured attributes attached to this event.
-
#attributes=(attributes : Event::Attributes)
Structured attributes attached to this event.
-
#class_name : String
Fully-qualified class name containing the instrumented method.
-
#class_name=(class_name : String)
Fully-qualified class name containing the instrumented method.
-
#code_file : String
Source file where the instrumented method is defined.
-
#code_file=(code_file : String)
Source file where the instrumented method is defined.
-
#code_line : Int32
Line number where the instrumented method is defined.
-
#code_line=(code_line : Int32)
Line number where the instrumented method is defined.
-
#duration_ms : Int64
Duration of the operation in milliseconds.
-
#duration_ms=(duration_ms : Int64)
Duration of the operation in milliseconds.
-
#exception : ExceptionInfo | Nil
Exception information if an error occurred.
-
#exception=(exception : ExceptionInfo | Nil)
Exception information if an error occurred.
-
#level : LogLevel
Log level of this event.
-
#level=(level : LogLevel)
Log level of this event.
-
#method_name : String
Name of the instrumented method.
-
#method_name=(method_name : String)
Name of the instrumented method.
-
#name : String
Name of this event (typically the method name).
-
#name=(name : String)
Name of this event (typically the method name).
-
#parent_span_id : String | Nil
Span ID of the parent span, or nil if this is a root span.
-
#parent_span_id=(parent_span_id : String | Nil)
Span ID of the parent span, or nil if this is a root span.
-
#set_code_function(function : String) : Nil
Code attributes
- #set_code_namespace(namespace : String) : Nil
- #set_db_name(name : String) : Nil
- #set_db_operation(operation : String) : Nil
- #set_db_statement(statement : String) : Nil
-
#set_db_system(system : String) : Nil
Database attributes
- #set_exception_message(message : String) : Nil
-
#set_exception_type(type : String) : Nil
Exception attributes
-
#set_http_method(method : String) : Nil
Sets the HTTP request method (e.g., "GET", "POST").
- #set_http_request_body_size(size : Int64) : Nil
- #set_http_route(route : String) : Nil
- #set_http_status_code(code : Int32) : Nil
-
#set_service_name(name : String) : Nil
Service attributes
- #set_service_version(version : String) : Nil
-
#set_user_id(id : String | Int64) : Nil
User attributes
- #set_user_role(role : String) : Nil
-
#span_events : Array(Logit::SpanEvent)
Span events that occurred during the operation.
-
#span_events=(span_events : Array(Logit::SpanEvent))
Span events that occurred during the operation.
-
#span_id : String
Unique identifier for the span that generated this event.
-
#span_id=(span_id : String)
Unique identifier for the span that generated this event.
-
#status : Status
Status of the operation (Ok or Error).
-
#status=(status : Status)
Status of the operation (Ok or Error).
-
#timestamp : Time
When this event was created.
-
#timestamp=(timestamp : Time)
When this event was created.
-
#to_json(json : JSON::Builder) : Nil
Serialize to JSON
- #to_json : String
-
#trace_id : String
W3C trace ID (128-bit hex string) shared across all spans in a trace.
-
#trace_id=(trace_id : String)
W3C trace ID (128-bit hex string) shared across all spans in a trace.
Constructor Detail
Creates a new event with the given parameters.
Instance Method Detail
Fully-qualified class name containing the instrumented method.
Span ID of the parent span, or nil if this is a root span.
Span events that occurred during the operation.
These are intermediate logs attached to the span, similar to OpenTelemetry's Span Events.
Span events that occurred during the operation.
These are intermediate logs attached to the span, similar to OpenTelemetry's Span Events.
W3C trace ID (128-bit hex string) shared across all spans in a trace.