Class: PgEventstore::Middleware::EventTracing

Inherits:
Object
  • Object
show all
Includes:
PgEventstore::Middleware
Defined in:
lib/pg_eventstore/middleware/event_tracing.rb,
sig/pg_eventstore/middleware/event_trace.rbs

Constant Summary collapse

CAUSATION_ID_KEY =

Returns:

  • (String)
"#{Event::SYSTEM_SYMBOL}CaId".freeze
CORRELATION_ID_KEY =

Returns:

  • (String)
"#{Event::SYSTEM_SYMBOL}CoId".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.causation_marker(marker) ⇒ PgEventstore::FeatureMarker

Parameters:

  • marker (String)

Returns:



16
17
18
# File 'lib/pg_eventstore/middleware/event_tracing.rb', line 16

def causation_marker(marker)
  FeatureMarker.new(marker:, purpose: :causation_id, description: 'Causation ID')
end

.correlation_marker(marker) ⇒ PgEventstore::FeatureMarker

Parameters:

  • marker (String)

Returns:



22
23
24
# File 'lib/pg_eventstore/middleware/event_tracing.rb', line 22

def correlation_marker(marker)
  FeatureMarker.new(marker:, purpose: :correlation_id, description: 'Correlation ID')
end

Instance Method Details

#deserialize(event) ⇒ Object

rubocop:disable Style/GuardClause



40
41
42
43
44
45
46
47
48
49
# File 'lib/pg_eventstore/middleware/event_tracing.rb', line 40

def deserialize(event)
  if event.[CAUSATION_ID_KEY]
    event.causation_id = event.[CAUSATION_ID_KEY]
    event.feature_markers.push(self.class.causation_marker(event.causation_id))
  end
  if event.[CORRELATION_ID_KEY]
    event.correlation_id = event.[CORRELATION_ID_KEY]
    event.feature_markers.push(self.class.correlation_marker(event.correlation_id))
  end
end

#serialize(event) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pg_eventstore/middleware/event_tracing.rb', line 27

def serialize(event)
  if event.caused_by
    event.causation_id = event.caused_by.id.dup
    event.[CAUSATION_ID_KEY] = event.causation_id
    event.feature_markers.push(self.class.causation_marker(event.causation_id))
  end

  event.correlation_id = event.caused_by&.correlation_id&.dup || event.correlation_id || SecureRandom.uuid_v7
  event.[CORRELATION_ID_KEY] = event.correlation_id
  event.feature_markers.push(self.class.correlation_marker(event.correlation_id))
end