Skip to content

Commit 26490cb

Browse files
authored
Add Prometheus client and scratchpad docs (#10)
- Add Prometheus HTTP client for querying Prometheus (Faraday) - New file: `app/lib/r3x/client/prometheus.rb` - Introduce `Result` Enumerable to parse series and expose metric/value/timestamp - New file: `app/lib/r3x/client/prometheus/result.rb` - Ignore `scratchpad/` and document its purpose in `AGENTS.md`
1 parent 8b2c276 commit 26490cb

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@
4848

4949
/workflows
5050
/docs/mvp
51+
/scratchpad

AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ This Rails app uses a small set of preferred libraries for common integration wo
159159
- Prefer duck typing: focus on behavior rather than names. Ask what the object can do and which methods it responds to; prefer ability over identity.
160160
- Reasoning: Duck typing is meritocratic. New classes can participate by exposing the right behavior without forcing central dispatch code to learn every subtype.
161161

162+
## Scratchpad
163+
164+
- `scratchpad/` is for quick test scripts, one-off experiments, and prototyping — anything outside the main implementation code and test suite.
165+
- The directory is gitignored. Do not place production code there.
166+
- Use it when you need to verify something quickly (e.g., hitting an API, testing a query) without writing a proper test.
167+
162168
## Scope
163169

164170
- Apply these as defaults for new work.

app/lib/r3x/client/prometheus.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module R3x
2+
module Client
3+
class Prometheus
4+
def initialize(base_url:)
5+
@connection = Faraday.new(url: base_url) do |f|
6+
f.request :json
7+
f.response :json
8+
end
9+
end
10+
11+
def query(promql)
12+
response = connection.get("api/v1/query", query: promql)
13+
raise "Prometheus query failed: #{response.status}" unless response.success?
14+
15+
Result.new(response.body["data"])
16+
end
17+
18+
private
19+
20+
attr_reader :connection
21+
end
22+
end
23+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module R3x
2+
module Client
3+
class Prometheus
4+
class Result
5+
include Enumerable
6+
7+
attr_reader :result_type
8+
9+
def initialize(data)
10+
@result_type = data["resultType"]
11+
@series = data.fetch("result", []).map { |s| Series.new(s) }
12+
end
13+
14+
def each(&block)
15+
series.each(&block)
16+
end
17+
18+
private
19+
20+
attr_reader :series
21+
22+
Series = Struct.new(:data) do
23+
def metric
24+
data["metric"]
25+
end
26+
27+
def value
28+
data.dig("value", 1)
29+
end
30+
31+
def timestamp
32+
data.dig("value", 0)
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)