This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from nscott/feature/tcp-sink
Add the TCP sink. Add support for log group name and log stream name. Add the Units reference.
- Loading branch information
Showing
15 changed files
with
418 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ gem 'rubocop' | |
group :test do | ||
gem 'mocha' | ||
gem 'pry' | ||
gem 'tcp-client' | ||
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
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
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
require 'aws-embedded-metrics-customink/sinks/logger' | ||
require 'aws-embedded-metrics-customink/sinks/stdout' | ||
require 'aws-embedded-metrics-customink/sinks/tcp' | ||
|
||
require 'aws-embedded-metrics-customink/sinks/sink_error' |
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,9 @@ | ||
module Aws | ||
module Embedded | ||
module Metrics | ||
module Sinks | ||
class Error < StandardError; end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
gem 'tcp-client' | ||
require 'tcp-client' | ||
|
||
module Aws | ||
module Embedded | ||
module Metrics | ||
module Sinks | ||
# | ||
# Create a sink that will communicate to a CloudWatch Log Agent over a TCP connection. | ||
# | ||
# See https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Generation_CloudWatch_Agent.html | ||
# for configuration information | ||
class Tcp | ||
AWS_EMF_AGENT_ENDPOINT_ENV_VAR = 'AWS_EMF_AGENT_ENDPOINT' | ||
attr_reader :queue | ||
attr :client_opts | ||
|
||
# | ||
# Create a new TCP sink. It will use the +AWS_EMF_AGENT_ENDPOINT+ environment variable by default to | ||
# connect to a CloudWatch Metric Agent. | ||
# | ||
# @param conn_str [String] A connection string, formatted like 'tcp:https://127.0.0.1:25888'. | ||
# @param conn_timeout_secs [Numeric] The number of seconds before timing out the connection to the agent. | ||
# @param write_timeout_secs [Numeric] The number of seconds to wait before timing out a write. | ||
# @param logger [Logger] A standard Ruby logger to propagate warnings and errors. | ||
# Suggested to use Rails.logger. | ||
def initialize(conn_str: ENV.fetch(AWS_EMF_AGENT_ENDPOINT_ENV_VAR, nil), | ||
conn_timeout_secs: 10, | ||
write_timeout_secs: 10, | ||
logger: nil) | ||
if conn_str.nil? | ||
raise Sinks::Error, "Must specify a connection string or set environment variable #{AWS_EMF_AGENT_ENDPOINT_ENV_VAR}" | ||
end | ||
|
||
@logger = logger | ||
@cw_agent_uri = URI.parse(conn_str) | ||
if @cw_agent_uri.scheme != 'tcp' || !@cw_agent_uri.host || !@cw_agent_uri.port | ||
raise Sinks::Error, "Expected connection string to be in format tcp:https://<host>:<port>, got '#{conn_str}'" | ||
end | ||
|
||
@client_opts = TCPClient::Configuration.create( | ||
buffered: true, | ||
keep_alive: true, | ||
reverse_lookup: true, | ||
connect_timeout: conn_timeout_secs, | ||
write_timeout: write_timeout_secs | ||
) | ||
@conn = nil | ||
end | ||
|
||
def log_warn(msg) | ||
@logger&.warn(msg) | ||
end | ||
|
||
def log_err(msg) | ||
@logger&.error(msg) | ||
end | ||
|
||
def create_conn(host, port, opts) | ||
TCPClient.open("#{host}:#{port}", opts) | ||
end | ||
|
||
def connection | ||
@conn = create_conn(@cw_agent_uri.host, @cw_agent_uri.port, @client_opts) if @conn.nil? || @conn.closed? | ||
@conn | ||
end | ||
|
||
def send_message(message) | ||
retries = 2 | ||
conn = nil | ||
begin | ||
conn = connection | ||
conn.write(message) | ||
rescue Errno::ECONNREFUSED | ||
conn.close unless conn.nil? || conn.closed? | ||
log_warn("Could not connect to CloudWatch Agent at #{@cw_agent_uri.scheme}:https://#{@cw_agent_uri.host}:#{@cw_agent_uri.port}") | ||
retries -= 1 | ||
retry if retries >= 0 | ||
rescue StandardError => e | ||
log_err("#{e.class}: #{e.message}: #{e.backtrace.join("\n")}") | ||
end | ||
end | ||
|
||
def accept(message) | ||
send_message("#{JSON.dump(message)}\n") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Aws | ||
module Embedded | ||
module Metrics | ||
class Units | ||
# Time | ||
SECONDS = 'Seconds' | ||
MICROSECONDS = 'Microseconds' | ||
MILLISECONDS = 'Milliseconds' | ||
|
||
# Size | ||
BYTES = 'Bytes' | ||
KILOBYTES = 'Kilobytes' | ||
MEGABYTES = 'Megabytes' | ||
GIGABYTES = 'Gigabytes' | ||
TERABYTES = 'Terabytes' | ||
|
||
BITS = 'Bits' | ||
KILOBITS = 'Kilobits' | ||
MEGABITS = 'Megabits' | ||
GIGABITS = 'Gigabits' | ||
TERABITS = 'Terabits' | ||
|
||
# Simple units | ||
PERCENT = 'Percent' | ||
COUNT = 'Count' | ||
|
||
# Size over time | ||
BYTES_SECOND = "#{BYTES}/Second" | ||
KILOBYTES_SECOND = "#{KILOBYTES}/Second" | ||
MEGABYTES_SECOND = "#{MEGABYTES}/Second" | ||
GIGABYTES_SECOND = "#{GIGABYTES}/Second" | ||
TERABYTES_SECOND = "#{TERABYTES}/Second" | ||
|
||
BITS_SECOND = "#{BITS}/Second" | ||
KILOBITS_SECOND = "#{KILOBITS}/Second" | ||
MEGABITS_SECOND = "#{MEGABITS}/Second" | ||
GIGABITS_SECOND = "#{GIGABITS}/Second" | ||
TERABITS_SECOND = "#{TERABITS}/Second" | ||
|
||
COUNT_SECOND = "#{COUNT}/Second" | ||
|
||
# Unit-less | ||
NONE = 'None' | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Aws | ||
module Embedded | ||
module Metrics | ||
VERSION = '0.6.0'.freeze | ||
VERSION = '0.7.0'.freeze | ||
end | ||
end | ||
end |
Oops, something went wrong.