Class: Wavefront::Distribution

Inherits:
Write
  • Object
show all
Includes:
Mixins
Defined in:
lib/wavefront-sdk/distribution.rb

Overview

Help a user write histogram distributions to a Wavefront proxy

Instance Attribute Summary

Attributes inherited from Write

#creds, #logger, #opts, #writer

Instance Method Summary collapse

Methods included from Mixins

#log, #parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?

Methods inherited from Write

#close, #flush, #initialize, #manage_conn, #open, #paths_to_deltas, #point_array, #raw, #send_point, #setup_options, #write, #write_delta

Methods included from Validators

#wf_alert_id?, #wf_alert_severity?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_source_id?, #wf_string?, #wf_tag?, #wf_ts?, #wf_user_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

This class inherits a constructor from Wavefront::Write

Instance Method Details

#array2dist(values) ⇒ String

Turn an array of arrays into a the values part of a distribution

Returns:



72
73
74
# File 'lib/wavefront-sdk/distribution.rb', line 72

def array2dist(values)
  values.map { |x, v| format('#%i %s', x, v) }.join(' ')
end

#default_portObject



40
41
42
# File 'lib/wavefront-sdk/distribution.rb', line 40

def default_port
  40000
end

#hash_to_wf(dist) ⇒ Object

Convert a validated point to a string conforming to docs.wavefront.com/proxies_histograms.html. No validation is done here.

rubocop:disable Metrics/AbcSize

Parameters:

  • point (Hash)

    a hash describing a distribution. See #write() for the format.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wavefront-sdk/distribution.rb', line 52

def hash_to_wf(dist)
  logger.log("writer subclass #{writer}", :debug)
  raise unless dist.key?(:interval)

  format('!%s %i %s %s source=%s %s %s',
         dist[:interval].to_s.upcase,
         parse_time(dist.fetch(:ts, Time.now)),
         array2dist(dist[:value]),
         dist[:path] || raise,
         dist.fetch(:source, HOSTNAME),
         dist[:tags] && dist[:tags].to_wf_tag,
         opts[:tags] && opts[:tags].to_wf_tag).squeeze(' ').strip
rescue StandardError
  raise Wavefront::Exception::InvalidDistribution
end

#mk_distribution(*args) ⇒ Array[Array]

Make a distribution from a lot of numbers. The returned distribution is an array of arrays ready for dropping into #write. If you need a “real” Wavefront representation of the distribution, use #mk_wf_distribution.

Parameters:

  • args (String, Array)

    numbers. Can be multiple arguments, a whitespace-separated string, or one or more arrays.

Returns:

  • (Array[Array])

    of the form [[1, 3], [4, 5]…]



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wavefront-sdk/distribution.rb', line 20

def mk_distribution(*args)
  flat = args.flatten
  raw = flat.first.is_a?(String) ? flat.first.split : flat

  hash = raw.each_with_object(Hash.new(0)) do |v, sum|
    sum[v] += 1
    sum
  end

  hash.to_a.map { |a, b| [b, a.to_f] }
end

#mk_wf_distribution(*args) ⇒ Object



32
33
34
# File 'lib/wavefront-sdk/distribution.rb', line 32

def mk_wf_distribution(*args)
  array2dist(mk_distribution(args))
end

#validationObject



36
37
38
# File 'lib/wavefront-sdk/distribution.rb', line 36

def validation
  :wf_distribution?
end