struct Logit::Event

Overview

A structured log event with OpenTelemetry-compatible fields.

Events are the core data structure passed to backends for logging. Each event contains:

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.cr
logit/events/event.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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.


[View source]

Instance Method Detail

def attributes : Event::Attributes #

Structured attributes attached to this event.


[View source]
def attributes=(attributes : Event::Attributes) #

Structured attributes attached to this event.


[View source]
def class_name : String #

Fully-qualified class name containing the instrumented method.


[View source]
def class_name=(class_name : String) #

Fully-qualified class name containing the instrumented method.


[View source]
def code_file : String #

Source file where the instrumented method is defined.


[View source]
def code_file=(code_file : String) #

Source file where the instrumented method is defined.


[View source]
def code_line : Int32 #

Line number where the instrumented method is defined.


[View source]
def code_line=(code_line : Int32) #

Line number where the instrumented method is defined.


[View source]
def duration_ms : Int64 #

Duration of the operation in milliseconds.


[View source]
def duration_ms=(duration_ms : Int64) #

Duration of the operation in milliseconds.


[View source]
def exception : ExceptionInfo | Nil #

Exception information if an error occurred.


[View source]
def exception=(exception : ExceptionInfo | Nil) #

Exception information if an error occurred.


[View source]
def level : LogLevel #

Log level of this event.


[View source]
def level=(level : LogLevel) #

Log level of this event.


[View source]
def method_name : String #

Name of the instrumented method.


[View source]
def method_name=(method_name : String) #

Name of the instrumented method.


[View source]
def name : String #

Name of this event (typically the method name).


[View source]
def name=(name : String) #

Name of this event (typically the method name).


[View source]
def parent_span_id : String | Nil #

Span ID of the parent span, or nil if this is a root span.


[View source]
def parent_span_id=(parent_span_id : String | Nil) #

Span ID of the parent span, or nil if this is a root span.


[View source]
def set_code_function(function : String) : Nil #

Code attributes


[View source]
def set_code_namespace(namespace : String) : Nil #

[View source]
def set_db_name(name : String) : Nil #

[View source]
def set_db_operation(operation : String) : Nil #

[View source]
def set_db_statement(statement : String) : Nil #

[View source]
def set_db_system(system : String) : Nil #

Database attributes


[View source]
def set_exception_message(message : String) : Nil #

[View source]
def set_exception_type(type : String) : Nil #

Exception attributes


[View source]
def set_http_method(method : String) : Nil #

Sets the HTTP request method (e.g., "GET", "POST").


[View source]
def set_http_request_body_size(size : Int64) : Nil #

[View source]
def set_http_route(route : String) : Nil #

[View source]
def set_http_status_code(code : Int32) : Nil #

[View source]
def set_service_name(name : String) : Nil #

Service attributes


[View source]
def set_service_version(version : String) : Nil #

[View source]
def set_user_id(id : String | Int64) : Nil #

User attributes


[View source]
def set_user_role(role : String) : Nil #

[View source]
def span_events : Array(Logit::SpanEvent) #

Span events that occurred during the operation.

These are intermediate logs attached to the span, similar to OpenTelemetry's Span Events.


[View source]
def span_events=(span_events : Array(Logit::SpanEvent)) #

Span events that occurred during the operation.

These are intermediate logs attached to the span, similar to OpenTelemetry's Span Events.


[View source]
def span_id : String #

Unique identifier for the span that generated this event.


[View source]
def span_id=(span_id : String) #

Unique identifier for the span that generated this event.


[View source]
def status : Status #

Status of the operation (Ok or Error).


[View source]
def status=(status : Status) #

Status of the operation (Ok or Error).


[View source]
def timestamp : Time #

When this event was created.


[View source]
def timestamp=(timestamp : Time) #

When this event was created.


[View source]
def to_json(json : JSON::Builder) : Nil #

Serialize to JSON


[View source]
def to_json : String #

[View source]
def trace_id : String #

W3C trace ID (128-bit hex string) shared across all spans in a trace.


[View source]
def trace_id=(trace_id : String) #

W3C trace ID (128-bit hex string) shared across all spans in a trace.


[View source]