Class: BoringMetrics::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/boringmetrics/client.rb

Overview

Main client for the BoringMetrics SDK

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, **config) ⇒ Client

Initialize a new client

Parameters:

  • token (String)

    Your BoringMetrics API token

  • config (Hash)

    Optional configuration options



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/boringmetrics/client.rb', line 15

def initialize(token, **config)
  @config = Configuration.new(token, **config)
  @transport = Transport.new(@config)

  @logs_queue = Concurrent::Array.new
  @logs_mutex = Mutex.new
  @logs_timer = nil

  @lives_queue = Concurrent::Array.new
  @lives_mutex = Mutex.new
  @lives_timer = nil

  @logs = LogMethods.new(self)
  @lives = LiveMethods.new(self)
end

Instance Attribute Details

#livesObject (readonly)

Returns the value of attribute lives.



9
10
11
# File 'lib/boringmetrics/client.rb', line 9

def lives
  @lives
end

#logsObject (readonly)

Returns the value of attribute logs.



9
10
11
# File 'lib/boringmetrics/client.rb', line 9

def logs
  @logs
end

Instance Method Details

#add_log(log) ⇒ void

This method returns an undefined value.

Add a log to the queue

Parameters:

  • log (Hash)

    The log to add



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/boringmetrics/client.rb', line 35

def add_log(log)
  log_with_sent_at = log.dup

  # Support both snake_case and camelCase
  if log_with_sent_at[:sentAt].nil? && log_with_sent_at[:sent_at].nil?
    log_with_sent_at[:sentAt] = Time.now.iso8601
  elsif log_with_sent_at[:sent_at] && log_with_sent_at[:sentAt].nil?
    log_with_sent_at[:sentAt] = log_with_sent_at[:sent_at]
  end

  @logs_mutex.synchronize do
    @logs_queue << log_with_sent_at

    if @logs_queue.size >= @config.logsMaxBatchSize
      flush_logs
    elsif @logs_timer.nil?
      schedule_logs_flush
    end
  end
end

#update_live(update) ⇒ void

This method returns an undefined value.

Update a live metric

Parameters:

  • update (Hash)

    The live update



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/boringmetrics/client.rb', line 60

def update_live(update)
  update_with_sent_at = update.dup

  # Support both snake_case and camelCase
  if update_with_sent_at[:sentAt].nil? && update_with_sent_at[:sent_at].nil?
    update_with_sent_at[:sentAt] = Time.now.iso8601
  elsif update_with_sent_at[:sent_at] && update_with_sent_at[:sentAt].nil?
    update_with_sent_at[:sentAt] = update_with_sent_at[:sent_at]
  end

  @lives_mutex.synchronize do
    @lives_queue << update_with_sent_at

    if @lives_queue.size >= @config.livesMaxBatchSize
      flush_lives
    elsif @lives_timer.nil?
      schedule_lives_flush
    end
  end
end