Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for parsing service check and event datagrams.
- Loading branch information
1 parent
2be2dec
commit 3ba96e5
Showing
5 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# frozen_string_literal: true | ||
|
||
# The Datagram class parses and inspects a StatsD datagrams | ||
# | ||
# @note This class is part of the new Client implementation that is intended | ||
# to become the new default in the next major release of this library. | ||
class StatsD::Instrument::DogStatsDDatagram < StatsD::Instrument::Datagram | ||
def name | ||
@name ||= case type | ||
when :_e then parsed_datagram[:name].gsub('\n', "\n") | ||
else super | ||
end | ||
end | ||
|
||
def value | ||
@value ||= case type | ||
when :_sc then Integer(parsed_datagram[:value]) | ||
when :_e then parsed_datagram[:value].gsub('\n', "\n") | ||
else super | ||
end | ||
end | ||
|
||
def hostname | ||
parsed_datagram[:hostname] | ||
end | ||
|
||
def timestamp | ||
Time.at(Integer(parsed_datagram[:timestamp])).utc | ||
end | ||
|
||
def aggregation_key | ||
parsed_datagram[:aggregation_key] | ||
end | ||
|
||
def source_type_name | ||
parsed_datagram[:source_type_name] | ||
end | ||
|
||
def priority | ||
parsed_datagram[:priority] | ||
end | ||
|
||
def alert_type | ||
parsed_datagram[:alert_type] | ||
end | ||
|
||
def message | ||
parsed_datagram[:message] | ||
end | ||
|
||
protected | ||
|
||
def parsed_datagram | ||
@parsed ||= if (match_info = PARSER.match(@source)) | ||
match_info | ||
else | ||
raise ArgumentError, "Invalid DogStatsD datagram: #{@source}" | ||
end | ||
end | ||
|
||
SERVICE_CHECK_PARSER = %r{ | ||
\A | ||
(?<type>_sc)\|(?<name>[^\|]+)\|(?<value>\d+) | ||
(?:\|h:(?<hostname>[^\|]+))? | ||
(?:\|d:(?<timestamp>\d+))? | ||
(?:\|\#(?<tags>(?:[^\|,]+(?:,[^\|,]+)*)))? | ||
(?:\|m:(?<message>[^\|]+))? | ||
\n? # In some implementations, the datagram may include a trailing newline. | ||
\z | ||
}x | ||
|
||
# |k:my-key|p:low|s:source|t:success| | ||
EVENT_PARSER = %r{ | ||
\A | ||
(?<type>_e)\{\d+\,\d+\}:(?<name>[^\|]+)\|(?<value>[^\|]+) | ||
(?:\|h:(?<hostname>[^\|]+))? | ||
(?:\|d:(?<timestamp>\d+))? | ||
(?:\|k:(?<aggregation_key>[^\|]+))? | ||
(?:\|p:(?<priority>[^\|]+))? | ||
(?:\|s:(?<source_type_name>[^\|]+))? | ||
(?:\|t:(?<alert_type>[^\|]+))? | ||
(?:\|\#(?<tags>(?:[^\|,]+(?:,[^\|,]+)*)))? | ||
\n? # In some implementations, the datagram may include a trailing newline. | ||
\z | ||
}x | ||
|
||
PARSER = Regexp.union(StatsD::Instrument::Datagram::PARSER, SERVICE_CHECK_PARSER, EVENT_PARSER) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters