Skip to content

Commit 6097ff5

Browse files
avilagaston9uri-99
andauthored
feat: add gas price to prometheus (#1257)
Co-authored-by: Uriel Mihura <[email protected]>
1 parent fae77d3 commit 6097ff5

File tree

13 files changed

+211
-19
lines changed

13 files changed

+211
-19
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"annotations": {
3+
"list": [
4+
{
5+
"builtIn": 1,
6+
"datasource": {
7+
"type": "grafana",
8+
"uid": "-- Grafana --"
9+
},
10+
"enable": true,
11+
"hide": true,
12+
"iconColor": "rgba(0, 211, 255, 1)",
13+
"name": "Annotations & Alerts",
14+
"type": "dashboard"
15+
}
16+
]
17+
},
18+
"editable": true,
19+
"fiscalYearStartMonth": 0,
20+
"graphTooltip": 0,
21+
"id": 4,
22+
"links": [],
23+
"liveNow": false,
24+
"panels": [
25+
{
26+
"datasource": {
27+
"type": "prometheus",
28+
"uid": "prometheus"
29+
},
30+
"fieldConfig": {
31+
"defaults": {
32+
"color": {
33+
"mode": "palette-classic"
34+
},
35+
"custom": {
36+
"axisCenteredZero": false,
37+
"axisColorMode": "text",
38+
"axisLabel": "gwei",
39+
"axisPlacement": "auto",
40+
"barAlignment": 0,
41+
"drawStyle": "line",
42+
"fillOpacity": 3,
43+
"gradientMode": "none",
44+
"hideFrom": {
45+
"legend": false,
46+
"tooltip": false,
47+
"viz": false
48+
},
49+
"insertNulls": false,
50+
"lineInterpolation": "linear",
51+
"lineStyle": {
52+
"fill": "solid"
53+
},
54+
"lineWidth": 3,
55+
"pointSize": 1,
56+
"scaleDistribution": {
57+
"type": "linear"
58+
},
59+
"showPoints": "auto",
60+
"spanNulls": false,
61+
"stacking": {
62+
"group": "A",
63+
"mode": "none"
64+
},
65+
"thresholdsStyle": {
66+
"mode": "off"
67+
}
68+
},
69+
"mappings": [],
70+
"thresholds": {
71+
"mode": "absolute",
72+
"steps": [
73+
{
74+
"color": "green",
75+
"value": null
76+
},
77+
{
78+
"color": "red",
79+
"value": 80
80+
}
81+
]
82+
},
83+
"unit": "none"
84+
},
85+
"overrides": []
86+
},
87+
"gridPos": {
88+
"h": 8,
89+
"w": 12,
90+
"x": 0,
91+
"y": 0
92+
},
93+
"id": 1,
94+
"options": {
95+
"legend": {
96+
"calcs": [],
97+
"displayMode": "list",
98+
"placement": "bottom",
99+
"showLegend": false
100+
},
101+
"tooltip": {
102+
"mode": "single",
103+
"sort": "none"
104+
}
105+
},
106+
"targets": [
107+
{
108+
"datasource": {
109+
"type": "prometheus",
110+
"uid": "prometheus"
111+
},
112+
"disableTextWrap": false,
113+
"editorMode": "code",
114+
"expr": "gas_price * 10^(-9)",
115+
"fullMetaSearch": false,
116+
"hide": false,
117+
"includeNullMetadata": true,
118+
"instant": false,
119+
"legendFormat": "__auto",
120+
"range": true,
121+
"refId": "A",
122+
"useBackend": false
123+
}
124+
],
125+
"title": "Gas Price",
126+
"type": "timeseries"
127+
}
128+
],
129+
"refresh": "",
130+
"schemaVersion": 38,
131+
"style": "dark",
132+
"tags": [],
133+
"templating": {
134+
"list": []
135+
},
136+
"time": {
137+
"from": "now-30m",
138+
"to": "now"
139+
},
140+
"timepicker": {},
141+
"timezone": "",
142+
"title": "Ethereum",
143+
"uid": "cc1ca6ab-6a36-41e0-afcf-2946c15afde5",
144+
"version": 6,
145+
"weekStart": ""
146+
}

prometheus/prometheus.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ scrape_configs:
2424
- targets: [ "host.docker.internal:9093" ]
2525
labels:
2626
bot: "batcher"
27+
28+
- job_name: "aligned-tracker"
29+
scrape_interval: 1s
30+
static_configs:
31+
- targets: [ "host.docker.internal:4001" ]
32+
labels:
33+
bot: "tracker"

telemetry_api/.env.dev

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
ALIGNED_CONFIG_FILE="../contracts/script/output/devnet/alignedlayer_deployment_output.json"
2-
OPERATOR_FETCHER_WAIT_TIME_MS=5000
3-
ENVIRONMENT=devnet
1+
export ALIGNED_CONFIG_FILE="../contracts/script/output/devnet/alignedlayer_deployment_output.json"
2+
export OPERATOR_FETCHER_WAIT_TIME_MS=5000
3+
export ENVIRONMENT=devnet
4+
export RPC_URL=http://localhost:8545

telemetry_api/config/dev.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ config :phoenix, :stacktrace_depth, 20
6363
# Initialize plugs at runtime for faster development compilation
6464
config :phoenix, :plug_init_mode, :runtime
6565

66+
# Node RPC URL
67+
rpc_url = System.get_env("RPC_URL") ||
68+
raise """
69+
environment variable RPC_URL is missing.
70+
"""
71+
6672
# Configure ethereumex url
67-
config :ethereumex, url: "http://localhost:8545"
73+
config :ethereumex, url: rpc_url
6874

6975
# For development, we use the stdout exporter to ensure everything is working properly
7076
# config :opentelemetry, traces_exporter: {:otel_exporter_stdout, []}

telemetry_api/lib/telemetry_api/application.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule TelemetryApi.Application do
77

88
@impl true
99
def start(_type, _args) do
10+
TelemetryApi.MetricsExporter.setup()
11+
1012
children = [
1113
TraceStore,
1214
TelemetryApiWeb.Telemetry,
@@ -17,7 +19,7 @@ defmodule TelemetryApi.Application do
1719
# {TelemetryApi.Worker, arg},
1820
# Start to serve requests, typically the last entry
1921
TelemetryApiWeb.Endpoint,
20-
TelemetryApi.Periodic.OperatorFetcher
22+
TelemetryApi.Periodically
2123
]
2224

2325
# See https://hexdocs.pm/elixir/Supervisor.html

telemetry_api/lib/telemetry_api/contract_managers/operator_state_retriever.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ defmodule TelemetryApi.ContractManagers.OperatorStateRetriever do
4646
parse_operators(operators_state)
4747
else
4848
{:error, %{reason: :econnrefused}} -> {:error, "Blockchain is not reachable"}
49+
{:error, reason} -> {:error, reason}
4950
end
5051
end
5152

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule TelemetryApi.EthereumMetrics do
2+
use Prometheus.Metric
3+
4+
@gauge [name: :gas_price, help: "Ethereum Gas Price.", labels: []]
5+
6+
def new_gas_price(gas_price) do
7+
Gauge.set(
8+
[name: :gas_price, labels: []],
9+
gas_price
10+
)
11+
end
12+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule TelemetryApi.MetricsExporter do
2+
use Prometheus.PlugExporter
3+
end

telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex renamed to telemetry_api/lib/telemetry_api/periodically.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
defmodule TelemetryApi.Periodic.OperatorFetcher do
1+
defmodule TelemetryApi.Periodically do
22
use GenServer
33
alias TelemetryApi.Operators
4+
alias TelemetryApi.EthereumMetrics
45
alias TelemetryApi.ContractManagers.RegistryCoordinatorManager
56
require Logger
67

@@ -30,6 +31,8 @@ defmodule TelemetryApi.Periodic.OperatorFetcher do
3031
end
3132

3233
def send_work() do
34+
one_second = 1000
35+
:timer.send_interval(one_second * 10, :gas_price) # every 10 seconds, once per block + some margin
3336
:timer.send_interval(@wait_time_ms, :poll_service)
3437
end
3538

@@ -39,6 +42,16 @@ defmodule TelemetryApi.Periodic.OperatorFetcher do
3942
{:noreply, state}
4043
end
4144

45+
def handle_info(:gas_price, _state) do
46+
case Ethers.current_gas_price() do
47+
{:ok, gas_price} ->
48+
EthereumMetrics.new_gas_price(gas_price)
49+
50+
{:error, error} ->
51+
IO.inspect("Error fetching gas price: #{error}")
52+
end
53+
{:noreply, %{}}
54+
end
4255
defp fetch_operators_info() do
4356
case Operators.fetch_all_operators() do
4457
:ok -> :ok

telemetry_api/lib/telemetry_api_web/endpoint.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule TelemetryApiWeb.Endpoint do
4444
pass: ["*/*"],
4545
json_decoder: Phoenix.json_library()
4646

47+
plug TelemetryApi.MetricsExporter
4748
plug Plug.MethodOverride
4849
plug Plug.Head
4950
plug Plug.Session, @session_options

0 commit comments

Comments
 (0)