forked from huginn/huginn
-
Notifications
You must be signed in to change notification settings - Fork 0
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 huginn#6 in SQ/huginn from samsa to master
* commit '45d95128b487d11ba514ac2aa5abe0345f9a5f39': Curl instructions for spamming errors. SamsaConsumerAgent Add every_1s to our list of changes. Speed up delayed worker to match every_1s schedule.
- Loading branch information
Showing
7 changed files
with
289 additions
and
2 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
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,79 @@ | ||
require 'squareup/logging/events/ext/logging_event.pb' | ||
|
||
module Agents | ||
class SamsaErrorAgent < Agent | ||
cannot_receive_events! | ||
|
||
describe <<-MD | ||
TODO | ||
MD | ||
|
||
event_description <<-MD | ||
TODO | ||
MD | ||
|
||
default_schedule "every_1s" | ||
|
||
def validate_options | ||
unless options['dc'].present? && | ||
options['env'].present? && | ||
options['app'].present? | ||
errors.add(:base, "dc, env, and app are required fields") | ||
end | ||
end | ||
|
||
def default_options | ||
{ | ||
'dc' => 'sjc1', | ||
'env' => 'staging', | ||
'app' => 'exemplar', | ||
} | ||
end | ||
|
||
def check | ||
topic_prefix = "logging.event.%s.%s.%s" % | ||
options.values_at(:app, :env, :dc) | ||
|
||
SamsaConsumer.topics.grep(/^#{topic_prefix}/).each do |topic| | ||
host = topic.split('.')[-2..-1].reverse.join('.') + '.square' | ||
key = "offset.#{topic}" | ||
offset = memory.fetch(key, :latest_offset) | ||
sc = SamsaConsumer.new(topic, offset) | ||
|
||
sc.consume do |msg| | ||
e = msg.ext_logging_event.log4j_logging_event | ||
|
||
next unless e.level == 'ERROR' | ||
|
||
create_event :payload => { | ||
:app => options[:app], | ||
:dc => options[:dc], | ||
:env => options[:env], | ||
:host => host, | ||
:message => e.logging_event_message, | ||
:backtrace => e.throwable_str_rep.to_a.join("\n"), | ||
:request => mdc_value(e, 'com.squareup.logging.HttpRequest'), | ||
:action => mdc_value(e, 'com.squareup.logging.ActionName'), | ||
:user_token => mdc_value(e, 'com.squareup.logging.user_token'), | ||
} | ||
end | ||
memory[key] = sc.next_offset | ||
end | ||
rescue => e | ||
$stderr.puts e.message | ||
$stderr.puts e.backtrace | ||
$stderr.puts | ||
end | ||
|
||
def working? | ||
true | ||
end | ||
|
||
def mdc_value(e, key) | ||
tuple = e.mdc.detect {|x| | ||
x.key == key | ||
} | ||
tuple ? tuple.value : '' | ||
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,168 @@ | ||
require 'poseidon' | ||
require 'sq/protos' | ||
require 'squareup/samsa/common.pb' | ||
|
||
require 'poseidon' | ||
require 'snappy' | ||
require 'stringio' | ||
|
||
# Helps consume events from Samsa and unpack the protos | ||
class SamsaConsumer | ||
attr_reader :consumer | ||
|
||
def initialize(topic, offset) | ||
@consumer = build_consumer(topic, offset) | ||
end | ||
|
||
# Consumes events and yields a single message at a time | ||
# @param opts [true,false] :once If true, exits after first message | ||
# @yieldparam [Squareup::Samsa::Common::SamsaMessage] | ||
def each(opts = {}, &block) | ||
each_batch(opts) { |batch| batch.each { |m| yield m } } | ||
end | ||
|
||
# Consumes events and yields a batch | ||
# @yieldparam [Array<Squareup::Samsa::Common::SamsaMessage>] | ||
def each_batch(opts = {}, &block) | ||
loop do | ||
messages = consumer.fetch | ||
|
||
yield messages.map { |m| decode(m) } | ||
|
||
return if opts[:once] | ||
end | ||
end | ||
|
||
def consume(&block) | ||
consumer.fetch.map {|m| decode(m) }.each(&block) | ||
end | ||
|
||
def next_offset | ||
consumer.next_offset | ||
end | ||
|
||
def self.topics | ||
connection = Poseidon::Connection.new('localhost', 4321, Socket.gethostname) | ||
connection.topic_metadata([]).topics.map(&:name) | ||
end | ||
|
||
protected | ||
|
||
def decode(message) | ||
raw_proto = message.value | ||
::Squareup::Samsa::Common::SamsaMessage.decode(raw_proto) | ||
end | ||
|
||
def brokers | ||
if Rails.env.production? | ||
['samsa-kafkaserver.vip.sjc1b.square:27330'] | ||
else # see script/samsa_tunnels | ||
['localhost:4321'] | ||
end | ||
end | ||
|
||
def consumer_name | ||
hostname = Socket.gethostname | ||
"huginn-#{hostname}" | ||
end | ||
|
||
def build_consumer(topic, offset) | ||
Poseidon::PartitionConsumer.consumer_for_partition( | ||
consumer_name, brokers, topic, 0, offset | ||
) | ||
end | ||
end | ||
|
||
# Remove once either of these PRs is merged upstream: | ||
# - https://github.com/bpot/poseidon/pull/28 | ||
# - https://github.com/bpot/poseidon/pull/52 | ||
module Poseidon | ||
module Compression | ||
module SnappyCodec | ||
def self.decompress(s) | ||
io = StringIO.new(s) | ||
io.set_encoding("ASCII-8BIT") | ||
::Snappy::Reader.new(io).read | ||
end | ||
end | ||
end | ||
end | ||
|
||
require 'poseidon' | ||
|
||
# The current version of poseidon uses #slice not #byteslice, which gets super | ||
# cranky with our binary protobuf payloads at Square. | ||
# Remove once these changes are merged: | ||
# - https://github.com/bpot/poseidon/pull/28 | ||
module Poseidon | ||
module Protocol | ||
class ResponseBuffer | ||
def initialize(response) | ||
@s = response | ||
@pos = 0 | ||
end | ||
|
||
def int8 | ||
byte = @s.byteslice(@pos, 1).unpack("C").first | ||
@pos += 1 | ||
byte | ||
end | ||
|
||
def int16 | ||
short = @s.byteslice(@pos, 2).unpack("s>").first | ||
@pos += 2 | ||
short | ||
end | ||
|
||
def int32 | ||
int = @s.byteslice(@pos, 4).unpack("l>").first | ||
@pos += 4 | ||
int | ||
end | ||
|
||
def int64 | ||
long = @s.byteslice(@pos, 8).unpack("q>").first | ||
@pos += 8 | ||
long | ||
end | ||
|
||
def string | ||
len = int16 | ||
string = @s.byteslice(@pos, len) | ||
@pos += len | ||
string | ||
end | ||
|
||
def read(bytes) | ||
data = @s.byteslice(@pos, bytes) | ||
@pos += bytes | ||
data | ||
end | ||
|
||
def peek(bytes) | ||
@s.byteslice(@pos, bytes) | ||
end | ||
|
||
def bytes | ||
n = int32 | ||
if n == -1 | ||
return nil | ||
else | ||
read(n) | ||
end | ||
end | ||
|
||
def bytes_remaining | ||
@s.size - @pos | ||
end | ||
|
||
def eof? | ||
@pos == @s.size | ||
end | ||
|
||
def to_s | ||
@s | ||
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
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,8 @@ | ||
#!/bin/bash | ||
|
||
LOCAL_PORT=4321 | ||
SAMSA_PORT=27330 | ||
|
||
echo "Opening SSH tunnel to Samsa/Kafka, ^C me when you're done." | ||
exec ssh -L $LOCAL_PORT:samsa-kafkaserver.stage.vip.sjc1.square:$SAMSA_PORT \ | ||
aia12.sjc1.square -N |