Skip to content

Event

struct

Defined in src/logit/events/attributes.cr:4

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": { ... }
}

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)

View source

Creates a new event with the given parameters.


Instance Methods

#attributes

View source

Structured attributes attached to this event.


#class_name

View source

Fully-qualified class name containing the instrumented method.


#code_file

View source

Source file where the instrumented method is defined.


#code_line

View source

Line number where the instrumented method is defined.


#duration_ms

View source

Duration of the operation in milliseconds.


#exception

View source

Exception information if an error occurred.


#level

View source

Log level of this event.


#method_name

View source

Name of the instrumented method.


#name

View source

Name of this event (typically the method name).


#parent_span_id

View source

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


#set_code_function(function : String) : Nil

View source

Code attributes


#set_db_system(system : String) : Nil

View source

Database attributes


#set_exception_type(type : String) : Nil

View source

Exception attributes


#set_http_method(method : String) : Nil

View source

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


#set_service_name(name : String) : Nil

View source

Service attributes


#set_user_id(id : String | Int64) : Nil

View source

User attributes


#span_events

View source

Span events that occurred during the operation.

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


#span_id

View source

Unique identifier for the span that generated this event.


#status

View source

Status of the operation (Ok or Error).


#timestamp

View source

When this event was created.


#to_json(json : JSON::Builder) : Nil

View source

Serialize to JSON


#trace_id

View source

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


Nested Types

  • Attributes -

    Type-safe structured attribute storage for log events.