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

#chunk_size, #close, #composite_response, #defaults, #initialize, #manage_conn, #open, #paths_to_deltas, #point_hash, #raw, #response_results, #send_point, #setup_options, #tags_or_nothing, #write, #write_delta

Methods included from Validators

#uuid?, #wf_account_id?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #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_ingestionpolicy_id?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_monitoredcluster_id?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_permission?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_sampling_value?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_serviceaccount_id?, #wf_source_id?, #wf_string?, #wf_tag?, #wf_ts?, #wf_user_id?, #wf_usergroup_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:



90
91
92
93
94
# File 'lib/wavefront-sdk/distribution.rb', line 90

def array2dist(values)
  values.map do |x, v|
    format('#%<count>i %<value>s', count: x, value: v)
  end.join(' ')
end

#data_formatObject



46
47
48
# File 'lib/wavefront-sdk/distribution.rb', line 46

def data_format
  :histogram
end

#default_portObject



42
43
44
# File 'lib/wavefront-sdk/distribution.rb', line 42

def default_port
  40_000
end

#dist_hash(dist) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/wavefront-sdk/distribution.rb', line 68

def dist_hash(dist)
  dist.dup.tap do |d|
    d[:interval] = distribution_interval(dist)
    d[:ts] = distribution_timestamp(dist)
    d[:value] = array2dist(dist[:value])
    d[:source] ||= HOSTNAME
    d[:tags] = tags_or_nothing(d[:tags])
    d[:opttags] = tags_or_nothing(opts[:tags])
  end
end

#distribution_interval(dist) ⇒ Object



83
84
85
# File 'lib/wavefront-sdk/distribution.rb', line 83

def distribution_interval(dist)
  dist[:interval].to_s.upcase
end

#distribution_timestamp(dist) ⇒ Object



79
80
81
# File 'lib/wavefront-sdk/distribution.rb', line 79

def distribution_timestamp(dist)
  parse_time(dist.fetch(:ts, Time.now))
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.

Parameters:

  • dist (Hash)

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



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

def hash_to_wf(dist)
  logger.log("writer subclass #{writer}", :debug)

  raise unless dist.key?(:interval) && dist.key?(:path)

  format('!%<interval>s %<ts>i %<value>s %<path>s source=%<source>s ' \
         '%<tags>s %<opttags>s', dist_hash(dist)).squeeze(' ').strip
rescue RuntimeError
  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]…]



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

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



34
35
36
# File 'lib/wavefront-sdk/distribution.rb', line 34

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

#validationObject



38
39
40
# File 'lib/wavefront-sdk/distribution.rb', line 38

def validation
  :wf_distribution?
end