Class: Aws::Embedded::Metrics::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-embedded-metrics-customink/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(sink = Config.config.sink) ⇒ Logger

Returns a new instance of Logger.



6
7
8
9
10
11
12
13
14
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 6

def initialize(sink = Config.config.sink)
  @sink = sink
  @log_group_name = Config.config.log_group_name
  @log_stream_name = Config.config.log_stream_name
  @namespace = Config.config.namespace
  @dimensions = Concurrent::Array.new
  @metrics = Concurrent::Array.new
  @properties = Concurrent::Hash.new
end

Instance Method Details

#benchmarkObject



26
27
28
29
30
31
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 26

def benchmark
  value = nil
  seconds = Benchmark.realtime { value = yield }
  milliseconds = (seconds * 1000).to_i
  [value, milliseconds]
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 50

def empty?
  [@dimensions, @metrics, @properties].all?(&:empty?)
end

#flushObject



22
23
24
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 22

def flush
  @sink.accept(message) unless empty?
end

#messageObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 54

def message
  aws = {
    'Timestamp' => timestamp,
    'CloudWatchMetrics' => [{
      'Namespace' => @namespace,
      'Dimensions' => [@dimensions.map(&:keys).flatten],
      'Metrics' => @metrics
    }]
  }

  aws['LogGroupName'] = @log_group_name if @log_group_name
  aws['LogStreamName'] = @log_stream_name if @log_stream_name

  {
    '_aws' => aws
  }.tap do |m|
    @dimensions.each { |dim| m.merge!(dim) }
    m.merge!(@properties)
  end
end

#metricsObject



16
17
18
19
20
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 16

def metrics
  yield(self)
ensure
  flush
end

#put_dimension(name, value) ⇒ Object



33
34
35
36
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 33

def put_dimension(name, value)
  @dimensions << { name => value }
  self
end

#put_metric(name, value, unit = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 38

def put_metric(name, value, unit = nil)
  @metrics << { 'Name' => name }.tap do |m|
    m['Unit'] = unit if unit
  end
  set_property name, value
end

#set_property(name, value) ⇒ Object



45
46
47
48
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 45

def set_property(name, value)
  @properties[name] = value
  self
end

#timestampObject



75
76
77
# File 'lib/aws-embedded-metrics-customink/logger.rb', line 75

def timestamp
  Time.now.strftime('%s%3N').to_i
end