deployagram-kafka 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/README.md +9 -0
- data/lib/deployagram/kafka.rb +198 -0
- metadata +77 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a5c663b2005c0685d7e67f99e6fa15dbfc60ceb799f61e57923bcb96d488c957
|
|
4
|
+
data.tar.gz: 89ffe6f4adbf2e53364c6dfcdf04ffb09ff5da1afda6e6256e7084bd21abacb0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9d25e784e4f0efa39f2ef6c992b5ecbdb67909803b4778fc3431b8a14bf8b791764080c78af7c522b4027d7bc65c1d8dcb71200e865c6aa8049149364fb8c358
|
|
7
|
+
data.tar.gz: 70d28e0ae4ba557e1aa9aa0a474abbb9f8d8cb217edfbe9bd6326ae974391e1f3f4eeaa6c7ccbdf39d714ebf0b3d2573020ac421a2b58ebbed5783524c6e83e5
|
data/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
DEPLOYAGRAM PROPRIETARY LICENSE
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Deployagram, Cookiebit and Andy Muir.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted to use this software solely for its intended purpose.
|
|
6
|
+
|
|
7
|
+
RESTRICTIONS:
|
|
8
|
+
- No modification of the software is permitted
|
|
9
|
+
- No reverse engineering, decompilation, or disassembly
|
|
10
|
+
- No redistribution without explicit written permission
|
|
11
|
+
- Source code remains proprietary and confidential
|
|
12
|
+
- Usage is limited to the compiled npm package (the distributed tarball) artifact only
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
|
data/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# deployagram-kafka
|
|
2
|
+
|
|
3
|
+
Deployagram Kafka recorders, and a Kafka and Schema Registry stack for service tests.
|
|
4
|
+
|
|
5
|
+
Records consumed and produced Kafka records to the Collector, and starts a KRaft broker and a Confluent Schema Registry on the Deployagram network.
|
|
6
|
+
|
|
7
|
+
Depends on: `deployagram-logger-client`, `deployagram-environment`.
|
|
8
|
+
|
|
9
|
+
Part of the Deployagram Ruby packages. See `../../README.md`.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "securerandom"
|
|
3
|
+
require "uri"
|
|
4
|
+
require "deployagram/docker"
|
|
5
|
+
require "deployagram/errors"
|
|
6
|
+
|
|
7
|
+
module Deployagram
|
|
8
|
+
# Kafka recording. The Collector reads each record's key and value from the broker, so the client sends only the
|
|
9
|
+
# coordinates: topic, partition and offset. Send a produced record after the broker acknowledges it, and a
|
|
10
|
+
# consumed record before the handler runs. The Collector stamps each record when it receives it, so a consumed
|
|
11
|
+
# record sent after the handler would show the handler's HTTP calls first.
|
|
12
|
+
module Kafka
|
|
13
|
+
PROTOCOL_CONSUMER = "KAFKA Consumer".freeze
|
|
14
|
+
PROTOCOL_PRODUCER = "KAFKA Producer".freeze
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def record_consumed(logger, consuming_app:, topic:, partition:, offset:)
|
|
19
|
+
logger.kafka_consumer_read(
|
|
20
|
+
"nameOfConsumingApp" => consuming_app,
|
|
21
|
+
"protocol" => PROTOCOL_CONSUMER,
|
|
22
|
+
"topic" => topic,
|
|
23
|
+
"partition" => partition,
|
|
24
|
+
"offset" => offset,
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def record_produced(logger, producing_app:, topic:, partition:, offset:)
|
|
29
|
+
logger.kafka_producer_record(
|
|
30
|
+
"nameOfProducingApp" => producing_app,
|
|
31
|
+
"protocol" => PROTOCOL_PRODUCER,
|
|
32
|
+
"topic" => topic,
|
|
33
|
+
"partition" => partition,
|
|
34
|
+
"offset" => offset,
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Records the record a ruby-kafka producer has just written to a partition. ruby-kafka's producer does not return
|
|
39
|
+
# the offset of what it wrote, so this reads the partition's last offset once the broker has acknowledged the
|
|
40
|
+
# write. That is the record just written only when nothing else writes to the partition meanwhile, which holds for
|
|
41
|
+
# a test that writes to a topic of its own. Raises if the partition is empty.
|
|
42
|
+
def record_last_produced(logger, kafka, producing_app:, topic:, partition: 0)
|
|
43
|
+
offset = kafka.last_offset_for(topic, partition)
|
|
44
|
+
raise EnvironmentError, "No record on #{topic} partition #{partition} to record" if offset.negative?
|
|
45
|
+
|
|
46
|
+
record_produced(logger, producing_app: producing_app, topic: topic, partition: partition, offset: offset)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The Collector's settings for a run that starts its own Kafka stack. Pass it as the logger environment, so the
|
|
50
|
+
# Collector runs in runtime configuration mode and accepts the broker and registry addresses.
|
|
51
|
+
COLLECTOR_ENVIRONMENT = {
|
|
52
|
+
"KAFKA_BROKER_LOCATION_MODE" => "RUNTIME",
|
|
53
|
+
"SCHEMA_REGISTRY_LOCATION_MODE" => "RUNTIME",
|
|
54
|
+
}.freeze
|
|
55
|
+
|
|
56
|
+
# A Kafka broker (KRaft, no ZooKeeper) and a Confluent Schema Registry, started on the deployagram network.
|
|
57
|
+
# The Collector reaches them by their aliases, and the application reaches them on localhost at the ports
|
|
58
|
+
# returned by bootstrap_servers and schema_registry_url. Stop only what this run started.
|
|
59
|
+
class Stack
|
|
60
|
+
KAFKA_IMAGE = "confluentinc/cp-kafka:7.6.1".freeze
|
|
61
|
+
REGISTRY_IMAGE = "confluentinc/cp-schema-registry:7.6.1".freeze
|
|
62
|
+
KAFKA_ALIAS = "kafka".freeze
|
|
63
|
+
REGISTRY_ALIAS = "schema-registry".freeze
|
|
64
|
+
INTERNAL_KAFKA_PORT = 29092
|
|
65
|
+
REGISTRY_PORT = 8081
|
|
66
|
+
READY_TIMEOUT_SECONDS = 90
|
|
67
|
+
# A fixed KRaft cluster id, 22 base64 characters.
|
|
68
|
+
CLUSTER_ID = "4L6g3nShT-eMCtK--X86sw".freeze
|
|
69
|
+
|
|
70
|
+
attr_reader :bootstrap_servers, :schema_registry_url
|
|
71
|
+
|
|
72
|
+
def self.start(**options)
|
|
73
|
+
new(**options).tap(&:start)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Starts a stack, and points the Collector at it, so that the stack is not left running if the Collector cannot
|
|
77
|
+
# be configured.
|
|
78
|
+
def self.start_for(logger, **options)
|
|
79
|
+
start(**options).tap do |stack|
|
|
80
|
+
stack.configure_collector(logger)
|
|
81
|
+
rescue StandardError
|
|
82
|
+
stack.stop
|
|
83
|
+
raise
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def initialize(kafka_port: nil, registry_port: nil)
|
|
88
|
+
@kafka_port = kafka_port || Docker.free_port
|
|
89
|
+
@registry_port = registry_port || Docker.free_port
|
|
90
|
+
@containers = []
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Starts the broker, then the registry, and waits for each to be ready.
|
|
94
|
+
def start
|
|
95
|
+
start_containers
|
|
96
|
+
self
|
|
97
|
+
rescue StandardError
|
|
98
|
+
# A failed start must not leave the containers this run began behind.
|
|
99
|
+
stop
|
|
100
|
+
raise
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def start_containers
|
|
104
|
+
Docker.ensure_network
|
|
105
|
+
@containers << Docker.run_detached(KAFKA_IMAGE, env: kafka_env, network_alias: KAFKA_ALIAS,
|
|
106
|
+
publish: ["#{@kafka_port}:9092"], name: unique_name("kafka"))
|
|
107
|
+
wait_for_log(@containers.last, /Kafka Server started/)
|
|
108
|
+
|
|
109
|
+
@containers << Docker.run_detached(REGISTRY_IMAGE, env: registry_env, network_alias: REGISTRY_ALIAS,
|
|
110
|
+
publish: ["#{@registry_port}:#{REGISTRY_PORT}"],
|
|
111
|
+
name: unique_name("schema-registry"))
|
|
112
|
+
wait_for_registry
|
|
113
|
+
|
|
114
|
+
@bootstrap_servers = "localhost:#{@kafka_port}"
|
|
115
|
+
@schema_registry_url = "http://localhost:#{@registry_port}"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# The address the Collector uses, which is on the Docker network.
|
|
119
|
+
def collector_bootstrap_servers
|
|
120
|
+
"PLAINTEXT://#{KAFKA_ALIAS}:#{INTERNAL_KAFKA_PORT}"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def collector_schema_registry_url
|
|
124
|
+
"http://#{REGISTRY_ALIAS}:#{REGISTRY_PORT}"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Points the Collector at this stack. Needs the Collector in runtime configuration mode.
|
|
128
|
+
def configure_collector(logger)
|
|
129
|
+
logger.configure_kafka_broker(collector_bootstrap_servers)
|
|
130
|
+
logger.configure_schema_registry(collector_schema_registry_url)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Removes the containers this stack started, registry first.
|
|
134
|
+
def stop
|
|
135
|
+
@containers.reverse_each { |id| Docker.remove(id) }
|
|
136
|
+
@containers.clear
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
private
|
|
140
|
+
|
|
141
|
+
def kafka_env
|
|
142
|
+
{
|
|
143
|
+
"CLUSTER_ID" => CLUSTER_ID,
|
|
144
|
+
"KAFKA_NODE_ID" => "1",
|
|
145
|
+
"KAFKA_PROCESS_ROLES" => "broker,controller",
|
|
146
|
+
"KAFKA_LISTENERS" => "INTERNAL://0.0.0.0:#{INTERNAL_KAFKA_PORT},EXTERNAL://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093",
|
|
147
|
+
"KAFKA_ADVERTISED_LISTENERS" => "INTERNAL://#{KAFKA_ALIAS}:#{INTERNAL_KAFKA_PORT},EXTERNAL://localhost:#{@kafka_port}",
|
|
148
|
+
"KAFKA_LISTENER_SECURITY_PROTOCOL_MAP" => "INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,CONTROLLER:PLAINTEXT",
|
|
149
|
+
"KAFKA_CONTROLLER_LISTENER_NAMES" => "CONTROLLER",
|
|
150
|
+
"KAFKA_CONTROLLER_QUORUM_VOTERS" => "1@#{KAFKA_ALIAS}:9093",
|
|
151
|
+
"KAFKA_INTER_BROKER_LISTENER_NAME" => "INTERNAL",
|
|
152
|
+
"KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR" => "1",
|
|
153
|
+
"KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR" => "1",
|
|
154
|
+
"KAFKA_TRANSACTION_STATE_LOG_MIN_ISR" => "1",
|
|
155
|
+
"KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS" => "0",
|
|
156
|
+
"KAFKA_AUTO_CREATE_TOPICS_ENABLE" => "true",
|
|
157
|
+
}
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def registry_env
|
|
161
|
+
{
|
|
162
|
+
"SCHEMA_REGISTRY_HOST_NAME" => REGISTRY_ALIAS,
|
|
163
|
+
"SCHEMA_REGISTRY_LISTENERS" => "http://0.0.0.0:#{REGISTRY_PORT}",
|
|
164
|
+
"SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS" => collector_bootstrap_servers,
|
|
165
|
+
}
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def unique_name(part)
|
|
169
|
+
"deployagram-demo-#{part}-#{SecureRandom.hex(4)}"
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def wait_for_log(container_id, pattern)
|
|
173
|
+
deadline = Time.now + READY_TIMEOUT_SECONDS
|
|
174
|
+
until Time.now > deadline
|
|
175
|
+
raise EnvironmentError, "#{container_id[0, 12]} stopped:\n#{Docker.logs(container_id).lines.last(30).join}" if Docker.stopped?(container_id)
|
|
176
|
+
return if Docker.logs(container_id).match?(pattern)
|
|
177
|
+
|
|
178
|
+
sleep 1
|
|
179
|
+
end
|
|
180
|
+
raise EnvironmentError, "#{container_id[0, 12]} was not ready within #{READY_TIMEOUT_SECONDS}s"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def wait_for_registry
|
|
184
|
+
uri = URI("http://localhost:#{@registry_port}/subjects")
|
|
185
|
+
deadline = Time.now + READY_TIMEOUT_SECONDS
|
|
186
|
+
until Time.now > deadline
|
|
187
|
+
begin
|
|
188
|
+
return if Net::HTTP.get_response(uri).is_a?(Net::HTTPSuccess)
|
|
189
|
+
rescue SystemCallError, SocketError, IOError, Timeout::Error
|
|
190
|
+
# Not up yet.
|
|
191
|
+
end
|
|
192
|
+
sleep 1
|
|
193
|
+
end
|
|
194
|
+
raise EnvironmentError, "The schema registry did not answer within #{READY_TIMEOUT_SECONDS}s"
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: deployagram-kafka
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Deployagram
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: deployagram-logger-client
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- "="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.1.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- "="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 0.1.0
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: deployagram-environment
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- "="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: 0.1.0
|
|
36
|
+
type: :runtime
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- "="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 0.1.0
|
|
44
|
+
description: "Records consumed and produced Kafka records to the Collector, and starts a KRaft broker and a Confluent Schema Registry on the Deployagram network."
|
|
45
|
+
email:
|
|
46
|
+
- andy@deployagram.com
|
|
47
|
+
executables: []
|
|
48
|
+
extensions: []
|
|
49
|
+
extra_rdoc_files: []
|
|
50
|
+
files:
|
|
51
|
+
- LICENSE
|
|
52
|
+
- README.md
|
|
53
|
+
- lib/deployagram/kafka.rb
|
|
54
|
+
licenses:
|
|
55
|
+
- SEE LICENSE IN LICENSE
|
|
56
|
+
metadata:
|
|
57
|
+
rubygems_mfa_required: "true"
|
|
58
|
+
rdoc_options: []
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
-
|
|
64
|
+
- ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: "3.2"
|
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
-
|
|
70
|
+
- ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: "0"
|
|
73
|
+
requirements: []
|
|
74
|
+
rubygems_version: 4.1.0
|
|
75
|
+
specification_version: 4
|
|
76
|
+
summary: "Deployagram Kafka recorders, and a Kafka and Schema Registry stack for service tests"
|
|
77
|
+
test_files: []
|