Class: Wavefront::Metadata

Inherits:
Object
  • Object
show all
Includes:
Constants, Mixins, Validators
Defined in:
lib/wavefront/metadata.rb

Overview

Because of the way the ‘manage’ API is laid out, this class doesn’t reflect it as clearly as, say the ‘alerts’ class. There is a small amount of duplication in method names too, as it merges a new class and an old one.

Note that the following methods do not do any exception handling. It is up to your client code to decide how to deal with, for example, a RestClient::ResourceNotFound exception.

Constant Summary collapse

DEFAULT_PATH =
'/api/manage/source/'.freeze

Constants included from Constants

Constants::ALERT_FORMATS, Constants::DEFAULT_ALERT_FORMAT, Constants::DEFAULT_FORMAT, Constants::DEFAULT_HOST, Constants::DEFAULT_INFILE_FORMAT, Constants::DEFAULT_OBSOLETE_METRICS, Constants::DEFAULT_PERIOD_SECONDS, Constants::DEFAULT_PREFIX_LENGTH, Constants::DEFAULT_PROXY, Constants::DEFAULT_PROXY_PORT, Constants::DEFAULT_STRICT, Constants::EVENT_LEVELS, Constants::EVENT_STATE_DIR, Constants::FORMATS, Constants::GRANULARITIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validators

#valid_path?, #valid_source?, #valid_string?, #valid_tags?, #valid_ts?, #valid_value?

Methods included from Mixins

#hash_to_qs, #interpolate_schema, #parse_time, #time_to_ms, #uri_concat

Constructor Details

#initialize(token, host = DEFAULT_HOST, debug = false, options = {}) ⇒ Metadata

Returns a new instance of Metadata.



45
46
47
48
49
50
51
52
53
54
# File 'lib/wavefront/metadata.rb', line 45

def initialize(token, host=DEFAULT_HOST, debug=false, options = {})
  #
  # 'host' is the Wavefront API endpoint
  #
  @headers = {'X-AUTH-TOKEN' => token}
  @base_uri = URI::HTTPS.build(:host => host, :path => DEFAULT_PATH)
  @endpoint = host
  @verbose = options[:verbose] || false
  debug(debug)
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



43
44
45
# File 'lib/wavefront/metadata.rb', line 43

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



43
44
45
# File 'lib/wavefront/metadata.rb', line 43

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



43
44
45
# File 'lib/wavefront/metadata.rb', line 43

def host
  @host
end

#verboseObject (readonly)

Returns the value of attribute verbose.



43
44
45
# File 'lib/wavefront/metadata.rb', line 43

def verbose
  @verbose
end

Instance Method Details

#add_tags(hostnames, tags) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/wavefront/metadata.rb', line 85

def add_tags(hostnames, tags)

  # Build and call tagging URI for each host and tag. A
  # backward-compatibility wrapper for set_tag()
  #
  hostnames.each do |hostname|
    tags.each { |tag| set_tag(hostname, tag) }
  end
end

#delete_tag(source, tag) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/wavefront/metadata.rb', line 113

def delete_tag(source, tag)
  #
  # Delete a given tag from a source. Maps to
  # DELETE /api/manage/source/{source}/tags/{tag}
  #
  fail Wavefront::Exception::InvalidSource unless valid_source?(source)
  fail Wavefront::Exception::InvalidString unless valid_string?(tag)
  call_delete(build_uri(uri_concat(source, 'tags', tag)))
end

#delete_tags(source) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/wavefront/metadata.rb', line 104

def delete_tags(source)
  #
  # Delete all tags from a source. Maps to
  # DELETE /api/manage/source/{source}/tags
  #
  fail Wavefront::Exception::InvalidSource unless valid_source?(source)
  call_delete(build_uri(uri_concat(source, 'tags')))
end

#get_tags(hostname = '', limit = 100) ⇒ Object



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
# File 'lib/wavefront/metadata.rb', line 56

def get_tags(hostname='', limit=100)
  #
  # This method is capable of making two distinct API calls,
  # depending on the arguments. It is left here for backward
  # compatibility, but you are recommended to use show_source() and
  # show_sources() instead.
  #
  uri = @base_uri

  unless hostname.empty?
    uri = URI.join(@base_uri.to_s, hostname)
  end

  if limit > 10000
    limit = 10000
  end

  args = {:params => {:limit => limit}}.merge(@headers)

  begin
    response = RestClient.get(uri.to_s, args)
  rescue RestClient::ResourceNotFound
    # If a 404 is returned, we return an empty JSON as this is the expected behaviour for untagged hosts
    response = {}
  end

  response
end

#remove_tags(hostnames, tags, _options = {}) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/wavefront/metadata.rb', line 95

def remove_tags(hostnames, tags, _options = {})
  #
  # A backward-compatibilty wrapper to delete_tag().
  #
  hostnames.each do |hostname|
    tags.each { |tag| delete_tag(hostname, tag) }
  end
end

#set_description(source, desc) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/wavefront/metadata.rb', line 175

def set_description(source, desc)
  #
  # set the description field for a source. Maps to
  # POST /api/manage/source/{source}/description
  #
  fail Wavefront::Exception::InvalidSource unless valid_source?(source)
  fail Wavefront::Exception::InvalidString unless valid_string?(desc)
  call_post(build_uri(uri_concat(source, 'description')), desc)
end

#set_tag(source, tag) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/wavefront/metadata.rb', line 185

def set_tag(source, tag)
  #
  # set a tag on a source. Maps to
  # POST /api/manage/source/{source}/tags/{tag}
  #
  fail Wavefront::Exception::InvalidSource unless valid_source?(source)
  fail Wavefront::Exception::InvalidString unless valid_string?(tag)
  call_post(build_uri(uri_concat(source, 'tags', tag)))
end

#show_source(source) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/wavefront/metadata.rb', line 164

def show_source(source)
  #
  # return information about a single source as a Ruby object. Maps to
  # GET /api/manage/source/{source}.
  #
  # See the Wavefront API docs for the structure of the object.
  #
  fail Wavefront::Exception::InvalidSource unless valid_source?(source)
  JSON.parse(call_get(build_uri(source)))
end

#show_sources(params = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/wavefront/metadata.rb', line 123

def show_sources(params = {})
  #
  # Return a list of sources as a Ruby object. Maps to
  # GET /api/manage/source
  # call it with a hash as described in the Wavefront API docs.
  #
  # See the Wavefront API docs for the format of the returned
  # object.
  #
  # At the time of writing, supported paramaters are:
  #   lastEntityId (string)
  #   desc         (bool)
  #   limit        (int)
  #   pattern      (string)
  #
  # Hash keys should be symbols.
  #
  if params.key?(:lastEntityId) &&
     !params[:lastEntityId].is_a?(String)
    fail TypeError
  end

  if params.key?(:pattern) && !params[:pattern].is_a?(String)
    fail TypeError
  end

  if params.key?(:desc) && ! (params[:desc] == !!params[:desc])
    fail TypeError
  end

  if params.key?(:limit)
    fail TypeError unless params[:limit].is_a?(Numeric)

    if params[:limit] < 0 || params[:limit] >= 10000
      fail Wavefront::Exception::ValueOutOfRange
    end
  end

  JSON.parse(call_get(build_uri(nil, query: hash_to_qs(params))))
end