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::DASH_FORMATS, Constants::DEFAULT_ALERT_FORMAT, Constants::DEFAULT_DASH_FORMAT, Constants::DEFAULT_FORMAT, Constants::DEFAULT_HOST, Constants::DEFAULT_INFILE_FORMAT, Constants::DEFAULT_OBSOLETE_METRICS, Constants::DEFAULT_OPTS, Constants::DEFAULT_PERIOD_SECONDS, Constants::DEFAULT_PREFIX_LENGTH, Constants::DEFAULT_PROXY, Constants::DEFAULT_PROXY_PORT, Constants::DEFAULT_SOURCE_FORMAT, Constants::DEFAULT_STRICT, Constants::EVENT_LEVELS, Constants::EVENT_STATE_DIR, Constants::FORMATS, Constants::GRANULARITIES, Constants::SOURCE_FORMATS

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

#call_delete, #call_get, #call_post, #hash_to_qs, #interpolate_schema, #load_file, #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
55
# 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
  @noop = options[:noop] || 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

#noopObject (readonly)

Returns the value of attribute noop.



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

def noop
  @noop
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



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

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



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

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



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

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



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

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



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

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



179
180
181
182
183
184
185
186
187
# File 'lib/wavefront/metadata.rb', line 179

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



189
190
191
192
193
194
195
196
197
# File 'lib/wavefront/metadata.rb', line 189

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



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/wavefront/metadata.rb', line 166

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)
  resp = call_get(build_uri(source)) || '{}'

  JSON.parse(resp)
end

#show_sources(params = {}) ⇒ Object



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

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

  resp = call_get(build_uri(nil, query: hash_to_qs(params))) || '{}'
  JSON.parse(resp)
end