Class: Fastlane::Helper::AppSizeMetricsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb

Overview

A helper class to build an App Size Metrics payload and send it to a server (or write it to disk)

The payload generated (and sent) by this helper conforms to the API for grouped metrics described in github.com/Automattic/apps-metrics

Instance Method Summary collapse

Constructor Details

#initialize(metadata = {}) ⇒ AppSizeMetricsHelper

Returns a new instance of AppSizeMetricsHelper.

Parameters:

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

    Metadata common to all the metrics. Can be any arbitrary set of key/value pairs.



15
16
17
18
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 15

def initialize( = {})
  self. = 
  @metrics = []
end

Instance Method Details

#add_metric(name:, value:, metadata: nil) ⇒ Object

Adds a single metric to the group of metrics

Parameters:

  • name (String)

    The metric name

  • value (Integer)

    The metric value

  • metadata (Hash) (defaults to: nil)

    The arbitrary dictionary of metadata to associate to that metric entry



34
35
36
37
38
39
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 34

def add_metric(name:, value:, metadata: nil)
  metric = { name: name, value: value }
   = &.compact || {} # Remove nil values if any (and use empty Hash if nil was provided)
  metric[:meta] = .map { |meta_key, meta_value| { name: meta_key.to_s, value: meta_value } } unless .empty?
  @metrics.append(metric)
end

#metadata=(hash) ⇒ Object

Sets the metadata common to the whole group of metrics in the payload being built by this helper instance

Parameters:

  • hash (Hash)

    The metadata common to all the metrics of the payload built by that helper instance. Can be any arbitrary set of key/value pairs



24
25
26
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 24

def metadata=(hash)
  @metadata = (hash.compact || {}).map { |key, value| { name: key.to_s, value: value } }
end

#send_metrics(to:, api_token:, use_gzip: true) ⇒ Integer

Send the metrics to the given App Metrics endpoint.

Must conform to the API described in github.com/Automattic/apps-metrics/wiki/Queue-Group-of-Metrics

Parameters:

  • to (String, URI)

    The URL of the App Metrics service, or a ‘file://` URL to write the payload to disk

  • api_token (String)

    The API bearer token to use to register the metric.

Returns:

  • (Integer)

    the HTTP response code



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 56

def send_metrics(to:, api_token:, use_gzip: true)
  uri = URI(to)
  json_payload = use_gzip ? Zlib.gzip(to_h.to_json) : to_h.to_json

  # Allow using a `file:` URI for debugging
  if uri.is_a?(URI::File)
    UI.message("Writing metrics payload to file #{uri.path} (instead of sending it to a remote API endpoint)")
    File.write(uri.path, json_payload)
    return 201 # To make it easy at call site to check for pseudo-status code 201 even in non-HTTP cases
  end

  UI.message("Sending metrics to #{uri}...")
  headers = {
    Authorization: "Bearer #{api_token}",
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
  headers[:'Content-Encoding'] = 'gzip' if use_gzip

  request = Net::HTTP::Post.new(uri, headers)
  request.body = json_payload

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end

  if response.is_a?(Net::HTTPSuccess)
    UI.success("Metrics sent. (#{response.code} #{response.message})")
  else
    UI.error("Metrics failed to send. Received: #{response.code} #{response.message}")
    UI.message("Request was #{request.method} to #{request.uri}")
    UI.message("Request headers were: #{headers}")
    UI.message("Request body was #{request.body.length} bytes")
    UI.message("Response was #{response.body}")
  end
  response.code.to_i
end

#to_hObject



41
42
43
44
45
46
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 41

def to_h
  {
    meta: @metadata,
    metrics: @metrics
  }
end