telemetry-metrics-parser 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 704cd22d5ad019cc1c1db2096ef17b7d57f81a6b0b4809fd56c90fdd0a49dce8
4
- data.tar.gz: 36b3de51e2ecd3efb78c7fe99ec45ea32bcda4edbabbff3cb6c9d27479c78378
3
+ metadata.gz: 156821d84a53d9823378fa29da00d0e3fd89ac078f5462451c7bc4acb1a7066b
4
+ data.tar.gz: 6b2c1697d86683bed555fc543d82733fe1919fbd3fcd515af6b5c810107c05d2
5
5
  SHA512:
6
- metadata.gz: 4396c2b1d44be0907f0aba1c6c165300e6182c50d6f10f6cfa8136c6676f98045fba74f902306f891b3e8dfd97af05f4fe5e7a9392ae4389721faf14fe5eb4e4
7
- data.tar.gz: d9c48cf18ea0099cf3e6f1b230fd16dd3bdbaf4b4314a3a25fbad23edb0838ba438bb2fdcd8982bedb9718e9ee3ae8fe7e146e405c0a68f9bf5650aa6a8d5d15
6
+ metadata.gz: 4310a3f81f543cdc23f552bc87a5a368d1813ec3ec3c2d0a32a6a35cd0eeedfba770d2dbca7c3019e821471a7eb59e35d8bc2edb4049537ae6dec33733006ef8
7
+ data.tar.gz: bcbb5036d57bebac6d56d0edd906e11507196681c1b6a0ec8e5e5b7d939882f9005f08884ca6fa89281357e16a6537648a2db44715a6ee958a7f3b654c9b612f
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
1
  # Telemetry::Metrics::Parser Changelog
2
2
 
3
+ ## v0.2.0
4
+ Adding new line validation methods
5
+
6
+ ## v0.1.1
7
+ * Adding more rspec tests for shellwords
8
+ * Adding in **opts to from_line_protocol method
9
+ * Changing line protocol parser to use shellwords logic by default
10
+
3
11
  ## v0.1.0
4
12
  Initial Version
data/README.md CHANGED
@@ -24,6 +24,11 @@ results[:fields] # => { temperature: 82 }
24
24
  results[:timestamp] # => 1465839830100400200
25
25
  ```
26
26
 
27
+ #### Validations
28
+ ```ruby
29
+ Telemetry::Metrics::Parser::LineProtocol.line_is_valid?('weather,location=us-midwest temperature=82 1465839830100400200') # true
30
+ Telemetry::Metrics::Parser::LineProtocol.line_is_valid?('weather,location=us-midwest temperature=this_field_is_a_string 1465839830100400200') # false but returned as a string error
31
+ ```
27
32
 
28
33
  Authors
29
34
  ----------
@@ -7,11 +7,11 @@ module Telemetry
7
7
  module LineProtocol
8
8
  extend Telemetry::NumberHelper
9
9
 
10
- def parse(line, use_shellwords: false)
10
+ def parse(line, use_shellwords: true)
11
11
  if use_shellwords
12
- raw_tags, raw_fields, timestamp = Shellwords.split(line)
12
+ raw_tags, raw_fields, timestamp = Shellwords.split(line.strip)
13
13
  else
14
- raw_tags, raw_fields, timestamp = line.split
14
+ raw_tags, raw_fields, timestamp = line.strip.split
15
15
  end
16
16
 
17
17
  {
@@ -33,6 +33,8 @@ module Telemetry
33
33
 
34
34
  def split_string_to_hash(raw_string)
35
35
  results = {}
36
+ return results if raw_string.nil?
37
+
36
38
  raw_string.split(',').each do |string|
37
39
  next unless string.include? '='
38
40
 
@@ -53,6 +55,83 @@ module Telemetry
53
55
  hash.map { |k, v| "#{k}=#{v}" }.join(',').strip.delete_suffix(',')
54
56
  end
55
57
  module_function :hash_to_line
58
+
59
+ def line_is_recent?(timestamp, max_age_sec: 86_400)
60
+ return false unless timestamp.is_a?(Integer)
61
+ return false unless max_age_sec.is_a?(Integer)
62
+
63
+ current_epoch_ns = DateTime.now.strftime('%s%9N').to_i
64
+ timestamp >= current_epoch_ns - (max_age_sec * 1000 * 1000 * 1000 * 3)
65
+ end
66
+ module_function :line_is_recent?
67
+
68
+ def field_is_number?(value)
69
+ return false if value.nil?
70
+ return true if value.is_a?(Integer)
71
+ return true if value.is_a?(Float)
72
+
73
+ %(f i).include?(value[-1])
74
+ end
75
+ module_function :field_is_number?
76
+
77
+ def tag_is_valid?(key, value)
78
+ return false if key.nil? || value.nil?
79
+ return false unless value.chars.detect { |ch| !valid_tag_chars.include?(ch) }.nil?
80
+ return false unless key.to_s.chars.detect { |ch| !valid_tag_chars.include?(ch) }.nil?
81
+
82
+ true
83
+ end
84
+ module_function :tag_is_valid?
85
+
86
+ def node_group_tag?(tags)
87
+ tags[:influxdb_node_group].is_a?(String)
88
+ end
89
+ module_function :node_group_tag?
90
+
91
+ def database_tag?(tags)
92
+ tags[:influxdb_database].is_a?(String)
93
+ end
94
+ module_function :database_tag?
95
+
96
+ def measurement_valid?(measurement)
97
+ return false unless measurement.is_a?(String)
98
+
99
+ measurement.chars.detect { |ch| !valid_measurement_chars.include?(ch) }.nil?
100
+ end
101
+ module_function :measurement_valid?
102
+
103
+ def valid_measurement_chars
104
+ @valid_measurement_chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a + %w[_ - .]
105
+ end
106
+ module_function :valid_measurement_chars
107
+
108
+ def valid_tag_chars
109
+ @valid_tag_chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a + %w[_ - .]
110
+ end
111
+ module_function :valid_tag_chars
112
+
113
+ def line_is_valid?(line) # rubocop:disable Metrics/AbcSize
114
+ line = parse(line) if line.is_a?(String)
115
+ return "line is too old, #{line}" unless line_is_recent?(line[:timestamp])
116
+ return "line is missing influxdb_database, #{line}" unless node_group_tag? line[:tags]
117
+ return "line is missing influxdb_node_group, #{line}" unless database_tag? line[:tags]
118
+ return "measurement name #{line[:measurement]} is not valid" unless measurement_valid?(line[:measurement])
119
+
120
+ line[:fields].each do |field, value|
121
+ next if field_is_number?(value)
122
+
123
+ return "field values must be an Integer or String, #{field} :#{value} #{value.class}"
124
+ end
125
+
126
+ line[:tags].each do |tag, value|
127
+ next if tag_is_valid?(tag, value)
128
+
129
+ return "tags must be a-z0-9_-. but was given #{tag}: #{value}"
130
+ end
131
+
132
+ true
133
+ end
134
+ module_function :line_is_valid?
56
135
  end
57
136
  end
58
137
  end
@@ -1,7 +1,7 @@
1
1
  module Telemetry
2
2
  module Metrics
3
3
  module Parser
4
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.2.0'.freeze
5
5
  end
6
6
  end
7
7
  end
@@ -4,8 +4,8 @@ require 'telemetry/metrics/parser/line_protocol'
4
4
  module Telemetry
5
5
  module Metrics
6
6
  module Parser
7
- def from_line_protocol(line)
8
- Telemetry::Metrics::Parser::LineProtocol.parse(line)
7
+ def from_line_protocol(line, **opts)
8
+ Telemetry::Metrics::Parser::LineProtocol.parse(line, **opts)
9
9
  end
10
10
  module_function :from_line_protocol
11
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telemetry-metrics-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-29 00:00:00.000000000 Z
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A metrics line parser gem for things like influxdb line protocol
14
14
  email:
@@ -55,7 +55,7 @@ metadata:
55
55
  homepage_uri: https://github.com/Optum/telemetry-metrics-parser
56
56
  source_code_uri: https://github.com/Optum/telemetry-metrics-parser
57
57
  wiki_uri: https://github.com/Optum/telemetry-metrics-parser/wiki
58
- post_install_message:
58
+ post_install_message:
59
59
  rdoc_options: []
60
60
  require_paths:
61
61
  - lib
@@ -70,8 +70,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  - !ruby/object:Gem::Version
71
71
  version: '0'
72
72
  requirements: []
73
- rubygems_version: 3.1.6
74
- signing_key:
73
+ rubygems_version: 3.3.11
74
+ signing_key:
75
75
  specification_version: 4
76
76
  summary: Parses common line formats for InfluxDB Line Protocol and turns it into a
77
77
  ruby hash