graphite-metric 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/graphite-metric/plaintext.rb +12 -4
- data/lib/graphite-metric/raw.rb +4 -7
- data/lib/graphite-metric/util.rb +12 -8
- data/lib/graphite-metric/version.rb +1 -1
- data/test/fixtures/metric_with_functions.txt +1 -0
- data/test/graphite-metric/plaintext_test.rb +51 -0
- data/test/graphite-metric/raw_test.rb +5 -0
- data/test/graphite-metric/util_test.rb +11 -49
- data/test/test_helper.rb +19 -0
- metadata +4 -2
@@ -1,9 +1,5 @@
|
|
1
|
-
require 'graphite-metric/util'
|
2
|
-
|
3
1
|
module GraphiteMetric
|
4
2
|
Plaintext = Struct.new(:key, :value, :timestamp) do
|
5
|
-
extend Util
|
6
|
-
|
7
3
|
def initialize(*args)
|
8
4
|
super
|
9
5
|
self[:timestamp] ||= Time.now.utc.to_i
|
@@ -12,5 +8,17 @@ module GraphiteMetric
|
|
12
8
|
def to_s
|
13
9
|
[key, value, timestamp].join(" ")
|
14
10
|
end
|
11
|
+
|
12
|
+
def self.from_hash(hash)
|
13
|
+
metric = new
|
14
|
+
members.each { |member| metric[member] = hash[member] if hash.has_key?(member) }
|
15
|
+
metric
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_array(array)
|
19
|
+
array.inject([]) do |result, hash|
|
20
|
+
result << from_hash(hash)
|
21
|
+
end
|
22
|
+
end
|
15
23
|
end
|
16
24
|
end
|
data/lib/graphite-metric/raw.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
require 'graphite-metric/util'
|
2
|
+
|
1
3
|
module GraphiteMetric
|
2
4
|
class Raw
|
5
|
+
include Util
|
3
6
|
attr_reader :raw, :metrics
|
4
7
|
|
5
8
|
def initialize(raw)
|
@@ -35,7 +38,7 @@ module GraphiteMetric
|
|
35
38
|
@raw.split("\n").each do |raw|
|
36
39
|
raw_headers, raw_metrics = raw.split("|")
|
37
40
|
|
38
|
-
metric_name, from, to, step = raw_headers
|
41
|
+
metric_name, from, to, step = extract_headers(raw_headers)
|
39
42
|
|
40
43
|
current_step = 0
|
41
44
|
raw_metrics.split(",").each do |raw_metric|
|
@@ -49,11 +52,5 @@ module GraphiteMetric
|
|
49
52
|
end
|
50
53
|
self
|
51
54
|
end
|
52
|
-
|
53
|
-
def float(raw_metric)
|
54
|
-
Float(raw_metric)
|
55
|
-
rescue
|
56
|
-
0
|
57
|
-
end
|
58
55
|
end
|
59
56
|
end
|
data/lib/graphite-metric/util.rb
CHANGED
@@ -2,16 +2,20 @@ module GraphiteMetric
|
|
2
2
|
module Util
|
3
3
|
extend self
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def float(raw_metric)
|
6
|
+
Float(raw_metric)
|
7
|
+
rescue
|
8
|
+
0
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def extract_headers(raw_headers)
|
12
|
+
all_splits = raw_headers.split(",")
|
13
|
+
step = all_splits.pop
|
14
|
+
to = all_splits.pop
|
15
|
+
from = all_splits.pop
|
16
|
+
metric_name = all_splits.join(",")
|
17
|
+
|
18
|
+
[metric_name, from, to, step]
|
15
19
|
end
|
16
20
|
end
|
17
21
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
scale(summarize(my.metric, "5min"),0.2),1337772900,1337773500,300|19.6,23.6
|
@@ -12,5 +12,56 @@ module GraphiteMetric
|
|
12
12
|
it "uses the graphite plaintext format when converted to string" do
|
13
13
|
"#{Plaintext.new("visitors", 2)}".must_equal "visitors 2 #{utc_now}"
|
14
14
|
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
@timestamp = Time.now.to_i - 3600
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "from hash" do
|
21
|
+
before do
|
22
|
+
@hash = {
|
23
|
+
:key => "visitors",
|
24
|
+
:value => 2,
|
25
|
+
:timestamp => @timestamp
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can be built from a hash" do
|
30
|
+
"#{Plaintext.from_hash(@hash)}".must_equal "visitors 2 #{@timestamp}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "from array (of hashes)" do
|
35
|
+
before do
|
36
|
+
@array = [
|
37
|
+
{
|
38
|
+
:key => "visitors",
|
39
|
+
:value => 1,
|
40
|
+
:timestamp => @timestamp
|
41
|
+
},
|
42
|
+
{
|
43
|
+
:key => "visitors",
|
44
|
+
:value => 5
|
45
|
+
}
|
46
|
+
]
|
47
|
+
@graphite_metrics = Plaintext.from_array(@array)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns an array" do
|
51
|
+
@graphite_metrics.must_be_instance_of Array
|
52
|
+
@graphite_metrics.size.must_equal 2
|
53
|
+
end
|
54
|
+
|
55
|
+
it "each element is a GraphiteMetric::Plaintext" do
|
56
|
+
@graphite_metrics.each { |graphite_metric| graphite_metric.must_be_instance_of GraphiteMetric::Plaintext }
|
57
|
+
end
|
58
|
+
|
59
|
+
it "converts into graphite plaintext format" do
|
60
|
+
@graphite_metrics.map(&:to_s).must_equal([
|
61
|
+
"visitors 1 #{@timestamp}",
|
62
|
+
"visitors 5 #{utc_now}"
|
63
|
+
])
|
64
|
+
end
|
65
|
+
end
|
15
66
|
end
|
16
67
|
end
|
@@ -34,5 +34,10 @@ module GraphiteMetric
|
|
34
34
|
gmr = GraphiteMetric::Raw.new(multiple_metrics_raw)
|
35
35
|
gmr.grouped_metrics.must_equal multiple_metrics_grouped
|
36
36
|
end
|
37
|
+
|
38
|
+
it "understands metrics with functions" do
|
39
|
+
gmr = GraphiteMetric::Raw.new(metric_with_functions_raw)
|
40
|
+
gmr.metrics.must_equal metric_with_functions_converted
|
41
|
+
end
|
37
42
|
end
|
38
43
|
end
|
@@ -1,60 +1,22 @@
|
|
1
1
|
require_relative '../test_helper'
|
2
|
-
require 'graphite-metric/
|
2
|
+
require 'graphite-metric/util'
|
3
3
|
|
4
4
|
include TestHelpers
|
5
5
|
|
6
6
|
module GraphiteMetric
|
7
|
-
describe
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
before do
|
14
|
-
@hash = {
|
15
|
-
:key => "visitors",
|
16
|
-
:value => 2,
|
17
|
-
:timestamp => @timestamp
|
18
|
-
}
|
19
|
-
end
|
20
|
-
|
21
|
-
it "can be built from a hash" do
|
22
|
-
"#{Plaintext.from_hash(@hash)}".must_equal "visitors 2 #{@timestamp}"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "from array (of hashes)" do
|
27
|
-
before do
|
28
|
-
@array = [
|
29
|
-
{
|
30
|
-
:key => "visitors",
|
31
|
-
:value => 1,
|
32
|
-
:timestamp => @timestamp
|
33
|
-
},
|
34
|
-
{
|
35
|
-
:key => "visitors",
|
36
|
-
:value => 5
|
37
|
-
}
|
38
|
-
]
|
39
|
-
@graphite_metrics = Plaintext.from_array(@array)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "returns an array" do
|
43
|
-
@graphite_metrics.must_be_instance_of Array
|
44
|
-
@graphite_metrics.size.must_equal 2
|
7
|
+
describe Util do
|
8
|
+
describe "extract_raw_headers" do
|
9
|
+
it "simple metrics" do
|
10
|
+
Util.extract_headers("my.metric,1336559725,1336559845,60").must_equal(
|
11
|
+
["my.metric", "1336559725", "1336559845", "60"]
|
12
|
+
)
|
45
13
|
end
|
46
14
|
|
47
|
-
it "
|
48
|
-
|
15
|
+
it "metrics with functions" do
|
16
|
+
Util.extract_headers("scale(summarize(my.metric,\"5min\"),0.2),1337772900,1337773500,300").must_equal(
|
17
|
+
["scale(summarize(my.metric,\"5min\"),0.2)", "1337772900", "1337773500", "300"]
|
18
|
+
)
|
49
19
|
end
|
50
|
-
|
51
|
-
it "converts into graphite plaintext format" do
|
52
|
-
@graphite_metrics.map(&:to_s).must_equal([
|
53
|
-
"visitors 1 #{@timestamp}",
|
54
|
-
"visitors 5 #{utc_now}"
|
55
|
-
])
|
56
|
-
end
|
57
|
-
|
58
20
|
end
|
59
21
|
end
|
60
22
|
end
|
data/test/test_helper.rb
CHANGED
@@ -103,4 +103,23 @@ module TestHelpers
|
|
103
103
|
]
|
104
104
|
}
|
105
105
|
end
|
106
|
+
|
107
|
+
def metric_with_functions_raw
|
108
|
+
@metric_with_functions_raw ||= File.read("#{BASE_PATH}/test/fixtures/metric_with_functions.txt")
|
109
|
+
end
|
110
|
+
|
111
|
+
def metric_with_functions_converted
|
112
|
+
[
|
113
|
+
{
|
114
|
+
:key => "scale(summarize(my.metric, \"5min\"),0.2)",
|
115
|
+
:timestamp => 1337772900,
|
116
|
+
:value => 19.6
|
117
|
+
},
|
118
|
+
{
|
119
|
+
:key => "scale(summarize(my.metric, \"5min\"),0.2)",
|
120
|
+
:timestamp => 1337773200,
|
121
|
+
:value => 23.6
|
122
|
+
}
|
123
|
+
]
|
124
|
+
end
|
106
125
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphite-metric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: guard-minitest
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- lib/graphite-metric/raw.rb
|
96
96
|
- lib/graphite-metric/util.rb
|
97
97
|
- lib/graphite-metric/version.rb
|
98
|
+
- test/fixtures/metric_with_functions.txt
|
98
99
|
- test/fixtures/multiple_metrics.txt
|
99
100
|
- test/fixtures/single_metric.txt
|
100
101
|
- test/graphite-metric/plaintext_test.rb
|
@@ -126,6 +127,7 @@ signing_key:
|
|
126
127
|
specification_version: 3
|
127
128
|
summary: Generates strings that graphite understands. Loads raw graphite data.
|
128
129
|
test_files:
|
130
|
+
- test/fixtures/metric_with_functions.txt
|
129
131
|
- test/fixtures/multiple_metrics.txt
|
130
132
|
- test/fixtures/single_metric.txt
|
131
133
|
- test/graphite-metric/plaintext_test.rb
|