Class: Funktor::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/funktor/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimension) ⇒ Counter

Returns a new instance of Counter.



5
6
7
# File 'lib/funktor/counter.rb', line 5

def initialize(dimension)
  @dimension = dimension
end

Instance Attribute Details

#dimensionObject

Returns the value of attribute dimension.



3
4
5
# File 'lib/funktor/counter.rb', line 3

def dimension
  @dimension
end

Instance Method Details

#incr(job) ⇒ Object



9
10
11
# File 'lib/funktor/counter.rb', line 9

def incr(job)
  put_metric_to_stdout(job)
end

#metric_hash(job) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/funktor/counter.rb', line 24

def metric_hash(job)
  {
    "_aws": {
      "Timestamp": Time.now.strftime('%s%3N').to_i,
      "CloudWatchMetrics": [
        {
          "Namespace": metric_namespace,
          "Dimensions": [["WorkerClassName"], ["Queue"]],
          "Metrics": [ # CPU, Memory, Duration, etc...
                       {
                         "Name": dimension,
                         "Unit": "Count"
                       }
          ]
        }
      ]
    },
    "WorkerClassName": job.worker_class_name_for_metrics,
    "Queue": job.queue,
    "#{dimension}": 1
    #"count": value,
    #"requestId": "989ffbf8-9ace-4817-a57c-e4dd734019ee"
  }
  #data[key] = value
end

#metric_namespaceObject



20
21
22
# File 'lib/funktor/counter.rb', line 20

def metric_namespace
  [ENV['FUNKTOR_APP_NAME'], ENV['SERVERLESS_STAGE']].join('-')
end

#put_metric_to_stdout(job) ⇒ Object



13
14
15
16
17
18
# File 'lib/funktor/counter.rb', line 13

def put_metric_to_stdout(job)
  # NOTE : We use raw puts here instead of Funktor.logger.something to avoid getting extra
  # timestamps or log level information in the log line. We need this specific format to
  # be the only thing in the line so that CloudWatch can parse the logs and use the data.
  Funktor.raw_logger.unknown Funktor.dump_json(metric_hash(job))
end