Module: BrainzLab::Flux

Defined in:
lib/brainzlab/flux.rb,
lib/brainzlab/flux/buffer.rb,
lib/brainzlab/flux/client.rb,
lib/brainzlab/flux/provisioner.rb

Defined Under Namespace

Classes: Buffer, Client, Provisioner

Class Method Summary collapse

Class Method Details

.bufferObject



166
167
168
# File 'lib/brainzlab/flux.rb', line 166

def buffer
  @buffer ||= Buffer.new(client)
end

.clientObject



162
163
164
# File 'lib/brainzlab/flux.rb', line 162

def client
  @client ||= Client.new(BrainzLab.configuration)
end

.decrement(name, value = 1, tags: {}) ⇒ Object

Counter: Decrement value



81
82
83
# File 'lib/brainzlab/flux.rb', line 81

def decrement(name, value = 1, tags: {})
  increment(name, -value, tags: tags)
end

.distribution(name, value, tags: {}) ⇒ Object

Distribution: Statistical aggregation



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/brainzlab/flux.rb', line 86

def distribution(name, value, tags: {})
  return unless enabled?

  ensure_provisioned!
  return unless BrainzLab.configuration.flux_valid?

  metric = {
    type: 'distribution',
    name: name,
    value: value,
    tags: tags,
    timestamp: Time.now.utc.iso8601(3)
  }

  buffer.add(:metric, metric)
end

.ensure_provisioned!Object

INTERNAL ===



151
152
153
154
155
156
# File 'lib/brainzlab/flux.rb', line 151

def ensure_provisioned!
  return if @provisioned

  @provisioned = true
  provisioner.ensure_project!
end

.flush!Object

Flush any buffered data immediately



145
146
147
# File 'lib/brainzlab/flux.rb', line 145

def flush!
  buffer.flush!
end

.gauge(name, value, tags: {}) ⇒ Object

Gauge: Current value (overwrites)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/brainzlab/flux.rb', line 45

def gauge(name, value, tags: {})
  return unless enabled?

  ensure_provisioned!
  return unless BrainzLab.configuration.flux_valid?

  metric = {
    type: 'gauge',
    name: name,
    value: value,
    tags: tags,
    timestamp: Time.now.utc.iso8601(3)
  }

  buffer.add(:metric, metric)
end

.histogram(name, value, tags: {}) ⇒ Object

Histogram: Alias for distribution (for compatibility with brainzlab-rails)



104
105
106
# File 'lib/brainzlab/flux.rb', line 104

def histogram(name, value, tags: {})
  distribution(name, value, tags: tags)
end

.increment(name, value = 1, tags: {}) ⇒ Object

Counter: Increment value



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/brainzlab/flux.rb', line 63

def increment(name, value = 1, tags: {})
  return unless enabled?

  ensure_provisioned!
  return unless BrainzLab.configuration.flux_valid?

  metric = {
    type: 'counter',
    name: name,
    value: value,
    tags: tags,
    timestamp: Time.now.utc.iso8601(3)
  }

  buffer.add(:metric, metric)
end

.measure(name, tags: {}) ⇒ Object

Time a block and record as distribution



134
135
136
137
138
139
140
141
142
# File 'lib/brainzlab/flux.rb', line 134

def measure(name, tags: {})
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  begin
    yield
  ensure
    duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000
    distribution(name, duration_ms, tags: tags.merge(unit: 'ms'))
  end
end

.provisionerObject



158
159
160
# File 'lib/brainzlab/flux.rb', line 158

def provisioner
  @provisioner ||= Provisioner.new(BrainzLab.configuration)
end

.reset!Object



170
171
172
173
174
175
# File 'lib/brainzlab/flux.rb', line 170

def reset!
  @client = nil
  @buffer = nil
  @provisioner = nil
  @provisioned = false
end

.set(name, value, tags: {}) ⇒ Object

Set: Unique count (cardinality)



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/brainzlab/flux.rb', line 114

def set(name, value, tags: {})
  return unless enabled?

  ensure_provisioned!
  return unless BrainzLab.configuration.flux_valid?

  metric = {
    type: 'set',
    name: name,
    value: value.to_s,
    tags: tags,
    timestamp: Time.now.utc.iso8601(3)
  }

  buffer.add(:metric, metric)
end

.timing(name, value_ms, tags: {}) ⇒ Object

Timing: Record duration in milliseconds (alias for distribution)



109
110
111
# File 'lib/brainzlab/flux.rb', line 109

def timing(name, value_ms, tags: {})
  distribution(name, value_ms, tags: tags.merge(unit: 'ms'))
end

.track(name, properties = {}) ⇒ Object

Track a custom event

Parameters:

  • name (String)

    Event name (e.g., ‘user.signup’, ‘order.completed’)

  • properties (Hash) (defaults to: {})

    Event properties



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brainzlab/flux.rb', line 15

def track(name, properties = {})
  return unless enabled?

  ensure_provisioned!
  return unless BrainzLab.configuration.flux_valid?

  event = {
    name: name,
    timestamp: Time.now.utc.iso8601(3),
    properties: properties.except(:user_id, :value, :tags, :session_id),
    user_id: properties[:user_id],
    session_id: properties[:session_id],
    value: properties[:value],
    tags: properties[:tags] || {},
    environment: BrainzLab.configuration.environment,
    service: BrainzLab.configuration.service
  }

  buffer.add(:event, event)
end

.track_for_user(user, name, properties = {}) ⇒ Object

Track event for a specific user



37
38
39
40
# File 'lib/brainzlab/flux.rb', line 37

def track_for_user(user, name, properties = {})
  user_id = user.respond_to?(:id) ? user.id.to_s : user.to_s
  track(name, properties.merge(user_id: user_id))
end