freddy 1.7.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +31 -0
- data/.rubocop.yml +9 -28
- data/.ruby-gemset +1 -1
- data/.ruby-version +1 -1
- data/Gemfile +3 -4
- data/README.md +5 -10
- data/freddy.gemspec +10 -17
- data/lib/freddy.rb +9 -14
- data/lib/freddy/adapters.rb +3 -28
- data/lib/freddy/adapters/bunny_adapter.rb +19 -2
- data/lib/freddy/consumers.rb +1 -1
- data/lib/freddy/consumers/respond_to_consumer.rb +3 -13
- data/lib/freddy/consumers/response_consumer.rb +2 -4
- data/lib/freddy/consumers/tap_into_consumer.rb +12 -24
- data/lib/freddy/delivery.rb +46 -13
- data/lib/freddy/payload.rb +2 -33
- data/lib/freddy/producers.rb +1 -1
- data/lib/freddy/producers/reply_producer.rb +12 -5
- data/lib/freddy/producers/send_and_forget_producer.rb +5 -11
- data/lib/freddy/producers/send_and_wait_response_producer.rb +19 -33
- data/lib/freddy/request_manager.rb +1 -9
- data/lib/freddy/tracing.rb +37 -0
- data/lib/freddy/version.rb +5 -0
- data/spec/.rubocop.yml +26 -0
- data/spec/freddy/error_response_spec.rb +6 -6
- data/spec/freddy/payload_spec.rb +25 -16
- data/spec/integration/concurrency_spec.rb +8 -12
- data/spec/integration/tracing_spec.rb +15 -32
- data/spec/spec_helper.rb +5 -13
- metadata +28 -15
- data/.npmignore +0 -8
- data/.travis.yml +0 -16
- data/lib/freddy/adapters/march_hare_adapter.rb +0 -64
- data/lib/freddy/trace_carrier.rb +0 -28
- data/spec/freddy/trace_carrier_spec.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ae99c2d347a67110923b71ffac1af46c706d177b39b82c3adaae4db3f686a1d
|
4
|
+
data.tar.gz: 557210126d3c4cdff8f0946ee5b6882e3334126c3cb0ae900df823f6e762fdd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2ee88b1ec7e51523ae6b3851f07b398b9093d90b193712bce172a09764d2a683d0b5209ced2a4a524248d8ffab36074b8a0b65c662ef9844f319424ccb32e93
|
7
|
+
data.tar.gz: ea65f5f23e18c768098c1eec7edc299a146f0fc830a0a57b5e9d1f725b5fe6f0b442b017ecd08b80f6242424a6a0958b57c8514cdf015bca07e4d316e88edc56
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby-version: ['2.7']
|
15
|
+
services:
|
16
|
+
rabbitmq:
|
17
|
+
image: rabbitmq
|
18
|
+
ports:
|
19
|
+
- 5672:5672
|
20
|
+
options: --health-cmd "rabbitmqctl node_health_check" --health-interval 10s --health-timeout 5s --health-retries 5
|
21
|
+
steps:
|
22
|
+
- uses: actions/checkout@v2
|
23
|
+
with:
|
24
|
+
submodules: true
|
25
|
+
- name: Set up Ruby
|
26
|
+
uses: ruby/setup-ruby@v1
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
bundler-cache: true
|
30
|
+
- name: Run lint & tests
|
31
|
+
run: bundle exec rake
|
data/.rubocop.yml
CHANGED
@@ -1,39 +1,23 @@
|
|
1
|
-
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
SuggestExtensions: false
|
2
4
|
|
3
|
-
|
4
|
-
Enabled: no
|
5
|
-
|
6
|
-
Metrics/BlockLength:
|
7
|
-
Enabled: no
|
8
|
-
|
9
|
-
Metrics/LineLength:
|
5
|
+
Layout/LineLength:
|
10
6
|
Max: 120
|
11
7
|
|
12
|
-
|
13
|
-
Enabled: no
|
14
|
-
|
15
|
-
Style/Documentation:
|
16
|
-
Enabled: no
|
17
|
-
|
18
|
-
RSpec/ExampleLength:
|
19
|
-
Enabled: no
|
20
|
-
|
21
|
-
RSpec/MultipleExpectations:
|
8
|
+
Lint/EmptyBlock:
|
22
9
|
Enabled: no
|
23
10
|
|
24
|
-
|
25
|
-
Enabled: no
|
26
|
-
|
27
|
-
RSpec/VerifiedDoubles:
|
11
|
+
Metrics/AbcSize:
|
28
12
|
Enabled: no
|
29
13
|
|
30
|
-
|
14
|
+
Metrics/BlockLength:
|
31
15
|
Enabled: no
|
32
16
|
|
33
|
-
|
17
|
+
Metrics/MethodLength:
|
34
18
|
Enabled: no
|
35
19
|
|
36
|
-
|
20
|
+
Style/Documentation:
|
37
21
|
Enabled: no
|
38
22
|
|
39
23
|
Style/FrozenStringLiteralComment:
|
@@ -42,8 +26,5 @@ Style/FrozenStringLiteralComment:
|
|
42
26
|
Include:
|
43
27
|
- 'lib/**/*'
|
44
28
|
|
45
|
-
Performance/TimesMap:
|
46
|
-
Enabled: no
|
47
|
-
|
48
29
|
Naming/FileName:
|
49
30
|
Enabled: no
|
data/.ruby-gemset
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
freddy
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7
|
data/Gemfile
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem 'codeclimate-test-reporter'
|
4
3
|
gem 'hamster', '~> 3.0'
|
5
|
-
gem '
|
4
|
+
gem 'opentelemetry-sdk', '~> 1.0.0.rc3'
|
6
5
|
gem 'pry'
|
7
6
|
gem 'rspec'
|
8
|
-
gem 'rubocop', '~>
|
9
|
-
gem 'rubocop-rspec', '~>
|
7
|
+
gem 'rubocop', '~> 1.19'
|
8
|
+
gem 'rubocop-rspec', '~> 2.4'
|
10
9
|
|
11
10
|
gemspec
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Messaging API supporting acknowledgements and request-response
|
2
2
|
|
3
|
-
[![Build Status](https://
|
4
|
-
[![Code Climate](https://codeclimate.com/github/salemove/freddy/badges/gpa.svg)](https://codeclimate.com/github/salemove/freddy)
|
5
|
-
[![Test Coverage](https://codeclimate.com/github/salemove/freddy/badges/coverage.svg)](https://codeclimate.com/github/salemove/freddy/coverage)
|
3
|
+
[![Build Status](https://github.com/salemove/freddy/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/salemove/freddy/actions/workflows/ci.yml?query=branch%3Amaster)
|
6
4
|
|
7
5
|
## Setup
|
8
6
|
|
@@ -147,14 +145,11 @@ responder_handler.shutdown
|
|
147
145
|
|
148
146
|
## Request Tracing
|
149
147
|
|
150
|
-
Freddy supports [
|
151
|
-
|
152
|
-
|
153
|
-
```
|
154
|
-
|
155
|
-
Current trace can be accessed through a thread-local variable `OpenTracing.active_span`. Calling `deliver` or `deliver_with_response` will pass trace context to down-stream services.
|
148
|
+
Freddy supports [OpenTelemetry API|https://github.com/open-telemetry/opentelemetry-ruby].
|
149
|
+
The trace information is automatically passed through `deliver`,
|
150
|
+
`deliver_with_response`, `respond_to` and `tap_into` calls.
|
156
151
|
|
157
|
-
|
152
|
+
This is not compatible with `opentelemetry-instrumentation-bunny` library.
|
158
153
|
|
159
154
|
## Notes about concurrency
|
160
155
|
|
data/freddy.gemspec
CHANGED
@@ -1,19 +1,17 @@
|
|
1
|
-
|
2
1
|
lib = File.expand_path('lib', __dir__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'freddy/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name
|
7
|
-
|
8
|
-
else
|
9
|
-
'freddy'
|
10
|
-
end
|
11
|
-
spec.version = '1.7.0'
|
6
|
+
spec.name = 'freddy'
|
7
|
+
spec.version = Freddy::VERSION
|
12
8
|
spec.authors = ['Glia TechMovers']
|
13
9
|
spec.email = ['techmovers@salemove.com']
|
14
10
|
spec.description = 'Messaging API'
|
15
11
|
spec.summary = 'API for inter-application messaging supporting acknowledgements and request-response'
|
16
|
-
spec.license = '
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.homepage = 'https://github.com/salemove/freddy'
|
14
|
+
spec.required_ruby_version = '>= 2.7'
|
17
15
|
|
18
16
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
19
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
@@ -23,14 +21,9 @@ Gem::Specification.new do |spec|
|
|
23
21
|
spec.add_development_dependency 'bundler'
|
24
22
|
spec.add_development_dependency 'rake'
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
spec.add_dependency 'bunny', '~> 2.11'
|
31
|
-
spec.add_dependency 'oj', '~> 3.6'
|
32
|
-
end
|
33
|
-
|
34
|
-
spec.add_dependency 'opentracing', '~> 0.4'
|
24
|
+
spec.add_dependency 'bunny', '~> 2.11'
|
25
|
+
spec.add_dependency 'oj', '~> 3.6'
|
26
|
+
spec.add_dependency 'opentelemetry-api', '~> 1.0.0.rc3'
|
27
|
+
spec.add_dependency 'opentelemetry-semantic_conventions', '~> 1.0'
|
35
28
|
spec.add_dependency 'thread', '~> 0.1'
|
36
29
|
end
|
data/lib/freddy.rb
CHANGED
@@ -3,9 +3,11 @@
|
|
3
3
|
require 'json'
|
4
4
|
require 'thread/pool'
|
5
5
|
require 'securerandom'
|
6
|
-
require '
|
6
|
+
require 'opentelemetry'
|
7
|
+
require 'opentelemetry/semantic_conventions'
|
8
|
+
require_relative './freddy/version'
|
7
9
|
|
8
|
-
Dir[File.dirname(__FILE__)
|
10
|
+
Dir["#{File.dirname(__FILE__)}/freddy/*.rb"].sort.each(&method(:require))
|
9
11
|
|
10
12
|
class Freddy
|
11
13
|
FREDDY_TOPIC_EXCHANGE_NAME = 'freddy-topic'
|
@@ -26,22 +28,15 @@ class Freddy
|
|
26
28
|
# @return [Freddy]
|
27
29
|
#
|
28
30
|
# @example
|
29
|
-
# Freddy.build(Logger.new(
|
30
|
-
def self.build(logger = Logger.new(
|
31
|
-
OpenTracing.global_tracer ||= OpenTracing::Tracer.new
|
32
|
-
|
31
|
+
# Freddy.build(Logger.new($stdout), user: 'thumper', pass: 'howdy')
|
32
|
+
def self.build(logger = Logger.new($stdout), max_concurrency: DEFAULT_MAX_CONCURRENCY, **config)
|
33
33
|
connection = Adapters.determine.connect(config)
|
34
34
|
new(connection, logger, max_concurrency)
|
35
35
|
end
|
36
36
|
|
37
|
-
# @
|
38
|
-
def self.
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
# @deprecated Use OpenTracing ScopeManager instead
|
43
|
-
def self.trace=(trace)
|
44
|
-
OpenTracing.scope_manager.activate(trace) if OpenTracing.active_span != trace
|
37
|
+
# @private
|
38
|
+
def self.tracer
|
39
|
+
@tracer ||= OpenTelemetry.tracer_provider.tracer('freddy', Freddy::VERSION)
|
45
40
|
end
|
46
41
|
|
47
42
|
def initialize(connection, logger, max_concurrency)
|
data/lib/freddy/adapters.rb
CHANGED
@@ -1,36 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'adapters/bunny_adapter'
|
4
|
+
|
3
5
|
class Freddy
|
4
6
|
module Adapters
|
5
7
|
def self.determine
|
6
|
-
|
7
|
-
require_relative 'adapters/march_hare_adapter'
|
8
|
-
MarchHareAdapter
|
9
|
-
else
|
10
|
-
require_relative 'adapters/bunny_adapter'
|
11
|
-
BunnyAdapter
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module Shared
|
16
|
-
class Queue
|
17
|
-
def initialize(queue)
|
18
|
-
@queue = queue
|
19
|
-
end
|
20
|
-
|
21
|
-
def bind(*args)
|
22
|
-
@queue.bind(*args)
|
23
|
-
self
|
24
|
-
end
|
25
|
-
|
26
|
-
def name
|
27
|
-
@queue.name
|
28
|
-
end
|
29
|
-
|
30
|
-
def message_count
|
31
|
-
@queue.message_count
|
32
|
-
end
|
33
|
-
end
|
8
|
+
BunnyAdapter
|
34
9
|
end
|
35
10
|
end
|
36
11
|
end
|
@@ -47,12 +47,29 @@ class Freddy
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
class Queue
|
50
|
+
class Queue
|
51
|
+
def initialize(queue)
|
52
|
+
@queue = queue
|
53
|
+
end
|
54
|
+
|
55
|
+
def bind(*args)
|
56
|
+
@queue.bind(*args)
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def name
|
61
|
+
@queue.name
|
62
|
+
end
|
63
|
+
|
64
|
+
def message_count
|
65
|
+
@queue.message_count
|
66
|
+
end
|
67
|
+
|
51
68
|
def subscribe(manual_ack: false)
|
52
69
|
@queue.subscribe(manual_ack: manual_ack) do |info, properties, payload|
|
53
70
|
parsed_payload = Payload.parse(payload)
|
54
71
|
delivery = Delivery.new(
|
55
|
-
parsed_payload, properties, info.routing_key, info.delivery_tag
|
72
|
+
parsed_payload, properties, info.routing_key, info.delivery_tag, info.exchange
|
56
73
|
)
|
57
74
|
yield(delivery)
|
58
75
|
end
|
data/lib/freddy/consumers.rb
CHANGED
@@ -35,21 +35,11 @@ class Freddy
|
|
35
35
|
|
36
36
|
def process_message(delivery)
|
37
37
|
@consume_thread_pool.process do
|
38
|
-
|
39
|
-
scope = delivery.build_trace("freddy:respond:#{@destination}",
|
40
|
-
tags: {
|
41
|
-
'peer.address' => "#{@destination}:#{delivery.payload[:type]}",
|
42
|
-
'component' => 'freddy',
|
43
|
-
'span.kind' => 'server', # RPC
|
44
|
-
'message_bus.destination' => @destination,
|
45
|
-
'message_bus.correlation_id' => delivery.correlation_id
|
46
|
-
})
|
47
|
-
|
38
|
+
delivery.in_span do
|
48
39
|
yield(delivery)
|
49
|
-
ensure
|
50
|
-
@channel.acknowledge(delivery.tag, false)
|
51
|
-
scope.close
|
52
40
|
end
|
41
|
+
ensure
|
42
|
+
@channel.acknowledge(delivery.tag, false)
|
53
43
|
end
|
54
44
|
end
|
55
45
|
end
|
@@ -7,11 +7,9 @@ class Freddy
|
|
7
7
|
@logger = logger
|
8
8
|
end
|
9
9
|
|
10
|
-
def consume(_channel, queue)
|
10
|
+
def consume(_channel, queue, &block)
|
11
11
|
@logger.debug "Consuming messages on #{queue.name}"
|
12
|
-
queue.subscribe
|
13
|
-
yield(delivery)
|
14
|
-
end
|
12
|
+
queue.subscribe(&block)
|
15
13
|
end
|
16
14
|
end
|
17
15
|
end
|
@@ -47,33 +47,21 @@ class Freddy
|
|
47
47
|
|
48
48
|
def process_message(_queue, delivery)
|
49
49
|
@consume_thread_pool.process do
|
50
|
-
|
51
|
-
scope = delivery.build_trace("freddy:observe:#{@pattern}",
|
52
|
-
tags: {
|
53
|
-
'message_bus.destination' => @pattern,
|
54
|
-
'message_bus.correlation_id' => delivery.correlation_id,
|
55
|
-
'component' => 'freddy',
|
56
|
-
'span.kind' => 'consumer' # Message Bus
|
57
|
-
},
|
58
|
-
force_follows_from: true)
|
59
|
-
|
50
|
+
delivery.in_span(force_follows_from: true) do
|
60
51
|
yield delivery.payload, delivery.routing_key
|
61
|
-
|
62
52
|
@channel.acknowledge(delivery.tag)
|
63
|
-
rescue StandardError
|
64
|
-
case on_exception
|
65
|
-
when :reject
|
66
|
-
@channel.reject(delivery.tag)
|
67
|
-
when :requeue
|
68
|
-
@channel.reject(delivery.tag, true)
|
69
|
-
else
|
70
|
-
@channel.acknowledge(delivery.tag)
|
71
|
-
end
|
72
|
-
|
73
|
-
raise
|
74
|
-
ensure
|
75
|
-
scope.close
|
76
53
|
end
|
54
|
+
rescue StandardError
|
55
|
+
case on_exception
|
56
|
+
when :reject
|
57
|
+
@channel.reject(delivery.tag)
|
58
|
+
when :requeue
|
59
|
+
@channel.reject(delivery.tag, true)
|
60
|
+
else
|
61
|
+
@channel.acknowledge(delivery.tag)
|
62
|
+
end
|
63
|
+
|
64
|
+
raise
|
77
65
|
end
|
78
66
|
end
|
79
67
|
|
data/lib/freddy/delivery.rb
CHANGED
@@ -4,11 +4,12 @@ class Freddy
|
|
4
4
|
class Delivery
|
5
5
|
attr_reader :routing_key, :payload, :tag
|
6
6
|
|
7
|
-
def initialize(payload, metadata, routing_key, tag)
|
7
|
+
def initialize(payload, metadata, routing_key, tag, exchange)
|
8
8
|
@payload = payload
|
9
9
|
@metadata = metadata
|
10
10
|
@routing_key = routing_key
|
11
11
|
@tag = tag
|
12
|
+
@exchange = exchange
|
12
13
|
end
|
13
14
|
|
14
15
|
def correlation_id
|
@@ -23,20 +24,52 @@ class Freddy
|
|
23
24
|
@metadata.reply_to
|
24
25
|
end
|
25
26
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
27
|
+
def in_span(force_follows_from: false, &block)
|
28
|
+
name = "#{@exchange}.#{@routing_key} receive"
|
29
|
+
kind = OpenTelemetry::Trace::SpanKind::CONSUMER
|
30
|
+
producer_context = OpenTelemetry.propagation.extract(@metadata[:headers] || {})
|
31
|
+
|
32
|
+
if force_follows_from
|
33
|
+
producer_span_context = OpenTelemetry::Trace.current_span(producer_context).context
|
34
|
+
|
35
|
+
links = []
|
36
|
+
links << OpenTelemetry::Trace::Link.new(producer_span_context) if producer_span_context.valid?
|
37
|
+
|
38
|
+
# In general we should start a new trace here and just link two traces
|
39
|
+
# together. But Zipkin (which we currently use) doesn't support links.
|
40
|
+
# So even though the root trace could finish before anything here
|
41
|
+
# starts executing, we'll continue with the root trace here as well.
|
42
|
+
OpenTelemetry::Context.with_current(producer_context) do
|
43
|
+
Freddy.tracer.in_span(name, attributes: span_attributes, links: links, kind: kind, &block)
|
37
44
|
end
|
45
|
+
else
|
46
|
+
OpenTelemetry::Context.with_current(producer_context) do
|
47
|
+
Freddy.tracer.in_span(name, attributes: span_attributes, kind: kind, &block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def span_attributes
|
55
|
+
destination_kind = @exchange == '' ? 'queue' : 'topic'
|
56
|
+
|
57
|
+
attributes = {
|
58
|
+
'payload.type' => (@payload[:type] || 'unknown').to_s,
|
59
|
+
OpenTelemetry::SemanticConventions::Trace::MESSAGING_SYSTEM => 'rabbitmq',
|
60
|
+
OpenTelemetry::SemanticConventions::Trace::MESSAGING_DESTINATION => @exchange,
|
61
|
+
OpenTelemetry::SemanticConventions::Trace::MESSAGING_DESTINATION_KIND => destination_kind,
|
62
|
+
OpenTelemetry::SemanticConventions::Trace::MESSAGING_RABBITMQ_ROUTING_KEY => @routing_key,
|
63
|
+
OpenTelemetry::SemanticConventions::Trace::MESSAGING_OPERATION => 'receive'
|
64
|
+
}
|
65
|
+
|
66
|
+
# There's no correlation_id when a message was sent using
|
67
|
+
# `Freddy#deliver`.
|
68
|
+
if correlation_id
|
69
|
+
attributes[OpenTelemetry::SemanticConventions::Trace::MESSAGING_CONVERSATION_ID] = correlation_id
|
70
|
+
end
|
38
71
|
|
39
|
-
|
72
|
+
attributes
|
40
73
|
end
|
41
74
|
end
|
42
75
|
end
|