Class: Wavefront::Writer::Core

Inherits:
Object
  • Object
show all
Includes:
Validators
Defined in:
lib/wavefront-sdk/writers/core.rb

Overview

Abstract class extended by the other writers. Methods required whatever mechanism actually sends the points.

A point is defined as a hash with the following keys: path [String] metric path. (mandatory) value [Numeric] value of metric. Numeric. Mandatory. ts [Time, Integer] timestamp for point. Defaults to

current UTC time.

source [String] originating source of metric. Defaults to

the local hostname.

tags [Hash] key:value point tags which will be applied in

addition to any tags defined in the #initialize()
method.

Direct Known Subclasses

Api, Http, Proxy, Socket

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validators

#uuid?, #wf_account_id?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #wf_aws_external_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_metricspolicy_id?, #wf_monitoredapplication_id?, #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_role_id?, #wf_sampling_value?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_serviceaccount_id?, #wf_source_id?, #wf_spansamplingpolicy_id?, #wf_string?, #wf_tag?, #wf_trace?, #wf_ts?, #wf_user_id?, #wf_usergroup_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

#initialize(calling_class) ⇒ Core

Returns a new instance of Core.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wavefront-sdk/writers/core.rb', line 30

def initialize(calling_class)
  @calling_class = calling_class
  @creds         = calling_class.creds
  @opts          = calling_class.opts
  @logger        = calling_class.logger
  @manage_conn   = calling_class.manage_conn
  @summary       = Wavefront::Writer::Summary.new

  validate_credentials(creds) if respond_to?(:validate_credentials)
  post_initialize(creds, opts) if respond_to?(:post_initialize)
end

Instance Attribute Details

#calling_classObject (readonly)

Returns the value of attribute calling_class.



26
27
28
# File 'lib/wavefront-sdk/writers/core.rb', line 26

def calling_class
  @calling_class
end

#connObject (readonly)

Returns the value of attribute conn.



26
27
28
# File 'lib/wavefront-sdk/writers/core.rb', line 26

def conn
  @conn
end

#credsObject (readonly)

Returns the value of attribute creds.



26
27
28
# File 'lib/wavefront-sdk/writers/core.rb', line 26

def creds
  @creds
end

#loggerObject (readonly)

Returns the value of attribute logger.



26
27
28
# File 'lib/wavefront-sdk/writers/core.rb', line 26

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



26
27
28
# File 'lib/wavefront-sdk/writers/core.rb', line 26

def opts
  @opts
end

#summaryObject (readonly)

Returns the value of attribute summary.



26
27
28
# File 'lib/wavefront-sdk/writers/core.rb', line 26

def summary
  @summary
end

Instance Method Details

#chunk_sizeInteger

We divide metrics up into manageable chunks and send them in batches. This dictates how large those bundles are. You can override the value with the chunk_size option

Returns:

  • (Integer)


153
154
155
# File 'lib/wavefront-sdk/writers/core.rb', line 153

def chunk_size
  1000
end

#do_write(points, openclose, _prefix) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wavefront-sdk/writers/core.rb', line 59

def do_write(points, openclose, _prefix)
  open if openclose && respond_to?(:open)

  begin
    write_loop(points)
  ensure
    close if openclose && respond_to?(:close)
  end

  respond
end

#hash_to_wf(point) ⇒ String

Wrapper around calling_class’s #_hash_to_wf to facilitate verbosity/debugging. (The actual work is done in the calling class because it is not always on the same data type.)

Parameters:

  • point (Hash)

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

Returns:



100
101
102
103
104
# File 'lib/wavefront-sdk/writers/core.rb', line 100

def hash_to_wf(point)
  wf_point = calling_class.hash_to_wf(point)
  logger.log(wf_point, :debug)
  wf_point
end

#log_invalid_point(rawpoint, exception) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/wavefront-sdk/writers/core.rb', line 140

def log_invalid_point(rawpoint, exception)
  logger.log('Invalid point, skipping.', :warn)
  logger.log(exception.class, :warn)
  logger.log(format('Invalid point: %<rawpoint>s (%<message>s)',
                    rawpoint: rawpoint,
                    message: exception.to_s), :debug)
end

#prefix_points(points, prefix = nil) ⇒ Array

Prefix points with a given string

Parameters:

  • points (Array, Hash)

    one or more points

  • prefix (String) (defaults to: nil)

    prefix to apply to every point

Returns:



111
112
113
114
115
116
# File 'lib/wavefront-sdk/writers/core.rb', line 111

def prefix_points(points, prefix = nil)
  ret = [points].flatten
  return ret unless prefix

  ret.map { |pt| pt.tap { |p| p[:path] = "#{prefix}.#{p[:path]}" } }
end

#respondObject



71
72
73
74
75
76
# File 'lib/wavefront-sdk/writers/core.rb', line 71

def respond
  Wavefront::Response.new(
    { status: { result: summary.result, message: nil, code: nil },
      response: summary.to_h }.to_json, nil, opts
  )
end

#screen_points(points) ⇒ Array

Filter invalid points out of an array of points

Parameters:

  • points (Array, Hash)

    one or more points

Returns:



122
123
124
125
126
# File 'lib/wavefront-sdk/writers/core.rb', line 122

def screen_points(points)
  return points if opts[:novalidate]

  [points].flatten.select { |p| valid_point?(p) }
end

#send_point(point) ⇒ Object

Call the inheriting class’s #_send_point method, and handle the summary



81
82
83
84
85
86
87
88
89
90
# File 'lib/wavefront-sdk/writers/core.rb', line 81

def send_point(point)
  _send_point(point)
  summary.sent += 1
  true
rescue StandardError => e
  summary.unsent += 1
  logger.log('Failed to send point.', :warn)
  logger.log(e.to_s, :debug)
  false
end

#valid_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wavefront-sdk/writers/core.rb', line 128

def valid_point?(point)
  send(calling_class.validation, point)
rescue Wavefront::Exception::InvalidMetricName,
       Wavefront::Exception::InvalidMetricValue,
       Wavefront::Exception::InvalidTimestamp,
       Wavefront::Exception::InvalidSourceId,
       Wavefront::Exception::InvalidTag => e
  log_invalid_point(point, e)
  summary.rejected += 1
  false
end

#write(points = [], openclose = manage_conn, prefix = nil) ⇒ Wavefront::Response

Send multiple points to Wavefront.

Parameters:

  • points (Array[Hash]) (defaults to: [])

    an array of points.

  • openclose (Bool) (defaults to: manage_conn)

    if this is false, you must have already opened a connection to the proxy. If it is true, a connection will be opened for you, used, and closed.

  • prefix (String) (defaults to: nil)

    prefix all metrics with this string. No trailing dot is required.

Returns:

Raises:

  • any unhandled point validation error is passed through



53
54
55
56
57
# File 'lib/wavefront-sdk/writers/core.rb', line 53

def write(points = [], openclose = manage_conn, prefix = nil)
  points = screen_points(points)
  points = prefix_points(points, prefix)
  do_write(points, openclose, prefix)
end