Skip to content

Commit 874ec6b

Browse files
committed
added rules and updated doc
1 parent ba13e09 commit 874ec6b

File tree

5 files changed

+359
-32
lines changed

5 files changed

+359
-32
lines changed

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# 🚀 Container Exporter (CXP)
2+
3+
A resource-friendly, highly efficient, and minimal Prometheus exporter to track Memory, CPU, Disk and Network I/O usage of Docker containers along with their lifecycle (up/down) state used for alerting.
4+
5+
Check out the [web page](https://shayan-ghani.github.io/Container-Exporter/) for more information.
6+
7+
## Table of Contents
8+
1. [DEV STACK](#%EF%B8%8F-dev-stack)
9+
2. [DEMO](#-demo)
10+
3. [Step-by-Step Guide](#-step-by-step-guide)
11+
1. [Before You start](#before-you-start)
12+
2. [Getting started](#getting-started)
13+
- [Deploy with Github Actions](#-deploy-with-github-actions)
14+
- [Deploy with Docker](#-deploy-with-docker)
15+
- [Deploy without Docker](#-cant-use-docker-ok-then-)
16+
- [Run with a Custom Port](#-run-with-a-custom-port)
17+
3. [Add CXP to Prometheus](#-add-cxp-to-prometheus)
18+
4. [Grafana Dashboards](#-grafana-dashboards)
19+
5. [TO-DO](#to-do)
20+
6. [Contributions](#contributions)
21+
7. [Contact Information](#contact-information)
22+
23+
## 🛠️ DEV STACK
24+
![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54) ![Fastapi](https://img.shields.io/badge/fastapi-%23000.svg?style=for-the-badge&logo=fastapi&logoColor=ffdd54) ![Docker](https://img.shields.io/badge/docker-3670A0?style=for-the-badge&logo=docker&logoColor=ffff) ![Prometheus](https://img.shields.io/badge/Prometheus-E6522C?style=for-the-badge&logo=Prometheus&logoColor=white) ![Uvicorn](https://img.shields.io/badge/uvicorn-%298729.svg?style=for-the-badge&logo=uvicorn&logoColor=white)
25+
26+
see a sample of the metrics page [here](https://shayan-ghani.github.io/Container-Exporter/metrics.html).
27+
28+
## 🎥 DEMO
29+
<img src="https://github.com/Shayan-Ghani/Container-Exporter/blob/media/capture/CXP-DEMO.gif" width="100%" height="50%" />
30+
31+
32+
## 📋 Step-by-Step Guide
33+
34+
### Before You start
35+
- Port 8000 must be open (default port)
36+
- Docker & Docker Compose should be installed (optional)
37+
- The presence of Git and Python3.10
38+
39+
### Getting started
40+
41+
#### ⚙️ Deploy with Github Actions
42+
- fork the repository.
43+
- go to the fork repository and switch to `Action` tab.
44+
- click the `I understand my workflows, go ahead and enable them` button.
45+
- now you have access to all of the workflows, **however make sure you change the secrets listed below accordingly**:
46+
1. `secretes.DOCKER_TOKEN` : the personal access token docker hub of your(or your organization) account.
47+
2. `secretes.GHCR_TOKEN` : github classic access token with (packages read:write permissions) or just simply use `${{github.token}}`. [help](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)
48+
49+
*here's how workflows work:*
50+
- on push to `master` the project will be `built`, `deployed` and `released`.
51+
*since deploying to your servers requires runner configuration it must be triggered manually, you can modify its behavior on `cd.yml` workflow.*
52+
- on push to any branch **except for** `master` code will be `built` and `healthchecked`
53+
- on pr the project will be `healthchecked` and `built`.
54+
55+
*double check the required variables and secrets to prevent any unexpected failures*
56+
57+
#### 🐳 Deploy with Docker
58+
- clone and checkout to the repository using the following commands:
59+
```bash
60+
git clone https://github.com/Shayan-Ghani/Container-exporter.git
61+
cd Container-Exporter
62+
```
63+
- Deploy the docker-compose file that suits you the best for instance :
64+
```bash
65+
# Make the file executable
66+
chmod +x ./start.sh
67+
68+
# With docker compose v1
69+
docker-compose -f container-exporter.yml up -d
70+
# Or using v2
71+
docker compose -f container-exporter.yml up -d
72+
# build from base with Dockerfile
73+
docker-compose -f container-exporter-local.yml up --build -d
74+
```
75+
76+
#### 🐍 Can't use Docker? Ok then :
77+
```bash
78+
# No need if done already
79+
chmod +x ./start.sh
80+
81+
# Set up virtualenv
82+
python3 -m venv venv
83+
source venv/bin/activate
84+
pip install -U pip
85+
86+
# Install the required python packages
87+
pip install -r requirements.txt
88+
89+
# start the initializer script.
90+
./start.sh &
91+
```
92+
93+
You can use nohup as well :
94+
```
95+
nohup ./start.sh -out ./nohup.out
96+
97+
# to stop cxp without docker use this command
98+
kill -9 <PID>
99+
```
100+
Replace `<PID>` with the pid of ./start.sh script.
101+
102+
#### 🚢 Run With A Custom Parameters:
103+
104+
- adjust the following settings for `uvicorn` as environment variables:
105+
- HOST (Default: 0.0.0.0)
106+
- PORT (Default: 8000)
107+
- WORKERS (Default : 1)
108+
- LOG_LEVEL (Default : warning)
109+
110+
Example:
111+
```bash
112+
PORT="8000" ./start.sh <your custome port> &
113+
```
114+
115+
### 🔥 Add CXP to Prometheus
116+
- Edit your `prometheus.yml` file and add the address of container-exporter in scrape_configs:
117+
118+
![Prometheus config](https://github.com/Shayan-Ghani/Container-Exporter/blob/media/capture/scrape-config.png "Prometheus configuration file")
119+
120+
- Reload or restart your Prometheus server and reach out to `http://127.0.0.1:8000/metrics`
121+
### That is it you are good to go, Enjoy Using CXP! "}"
122+
123+
## 📊 Grafana Dashboards
124+
Check out [dashboards](./dashboards) directory for Json files. including CPU & Memory usage + containers status (uptime).
125+
126+
**Change `Your Prometheus data source uid` with the uid of Prometheus data source uid. you can find it this way:**
127+
- Reach out to Grafana then enter `Home > Administration > Data sources` then click on your Prometheus data source.
128+
- the characters after `datasources/edit/` are your uid. (e.g datasources/edit/**c8e586ac-4262-4aad5-a103-1240ss826424**)
129+
130+
- alternatively, use `dashboard-gen.sh` script to change the dashboards' uid by providing the uid as the first argument of the script. do the following steps:
131+
132+
```
133+
cd scripts && bash dashboard-gen.sh <your uid>
134+
```
135+
- replace `<your uid>` with your Prometheus datasource uid.
136+
137+
- now head to Grafana dashboards and hit `new > import` then copy the dashboard Json file and paste it into `Import via panel json`
138+
139+
- hit the `load` button and Done!
140+
141+
## TO-DO
142+
- [x] Disk I/O usage
143+
- [x] Network I/O Usage
144+
- [x] Add metrics in units of byte
145+
- [x] Check and Unregister *stat* metrics for containers that are not running
146+
- [x] Design and develop a static website to showcase Documentation, new features, etc.
147+
- [x] Enable functionality and smoke testing in ci
148+
- [X] Add `clear_metrics` functionality to switch on clearing the labels or setting them to 0 to maintain time series data, on user's demand.
149+
- [ ] Design grafana dashboards and share them on grafana cloud
150+
- [ ] Add unit tests
151+
152+
## Contributions
153+
Welcome to CXP! This project is production-ready now, and we encourage contributions to enhance its functionality, optimize code, and add new features
154+
155+
Feel free to contribute in any wacacy you can. If you come across a bug or have a suggestion, please don't hesitate to file an issue. Your input is valuable and helps us improve CXP for everyone; Therefore, add any desired function or feature to TO DO section. We appreciate your contribution to making CXP even better! If you have any questions or need assistance, feel free to reach out. Thank you!
156+
157+
- If you want to add metrics to cxp, make sure the naming convention is conformed to. (`cxp_metric_name`)

doc.html

Lines changed: 113 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
box-sizing: border-box;
1919
}
2020

21+
:root {
22+
--brand-main: #FF5722;
23+
/* for buttons & links */
24+
--brand-heading: #FFA07A;
25+
/* for h1/h2 */
26+
--brand-accent: #FFAB91;
27+
/* for h3, hover states */
28+
}
29+
30+
2131
body {
2232
background-color: #0d0d0d;
2333
font-family: "Source Code Pro", monospace;
@@ -65,16 +75,18 @@
6575
box-shadow: 0 0 50px rgba(255, 69, 0, 0.12);
6676
display: flex;
6777
flex-direction: column;
68-
gap: 1.5rem;
78+
gap: 1.8rem;
79+
line-height: 2.5rem;
80+
6981
}
7082

7183
h1 {
72-
color: #FF5722;
84+
color: var(--brand-main);
7385
font-size: 2.6rem;
7486
}
7587

7688
h2 {
77-
color: #FF5722;
89+
color: var(--brand-main);
7890
font-size: 1.8rem;
7991
}
8092

@@ -84,13 +96,13 @@
8496
}
8597

8698
p {
87-
line-height: 1.7;
99+
line-height: 2.5rem;
88100
color: #ccc;
89101
}
90102

91103
ol,
92104
ul {
93-
padding-left: 1.5rem;
105+
padding-left: 2.5rem;
94106
display: flex;
95107
flex-direction: column;
96108
gap: 0.5rem;
@@ -124,7 +136,7 @@
124136
}
125137

126138
a {
127-
color: #FF5722;
139+
color: var(--brand-main);
128140
text-decoration: none;
129141
}
130142

@@ -147,15 +159,33 @@
147159
.section {
148160
display: flex;
149161
flex-direction: column;
150-
gap: 1rem;
162+
gap: 2rem;
151163
}
152164

165+
153166
.badges {
154167
display: flex;
155168
flex-wrap: wrap;
156169
gap: 0.5rem;
157170
margin-top: 0.5rem;
158171
}
172+
173+
174+
h1 {
175+
color: var(--brand-heading);
176+
font-size: 2.6rem;
177+
}
178+
179+
h2 {
180+
color: var(--brand-heading);
181+
font-size: 1.8rem;
182+
}
183+
184+
h3 {
185+
color: var(--brand-accent);
186+
font-size: 1.4rem;
187+
}
188+
159189
</style>
160190
</head>
161191

@@ -165,27 +195,35 @@
165195
<div class="container">
166196
<h1>🚀 Container Exporter (CXP)</h1>
167197
<p>A resource-friendly, highly efficient Prometheus exporter for monitoring Docker containers' Memory, CPU,
168-
Disk, and Network I/O usage — with uptime tracking.</p>
169-
<p>when a container is not running due to whatever reason <code>cxp</code> removes all its stats metrics (cpu, memory, etc.) but keeps its status (running/not-running) to facilitate alerting in case something goes wrong with that contaier.</p>
170-
171-
<p>Check out a sample of the metrics output <a href="./metrics.html">here</a> to see how it works.</p>
198+
Disk, and Network I/O usage — with uptime tracking.</p>
199+
<p>when a container is not running due to whatever reason <code>cxp</code> removes all its stats metrics (cpu,
200+
memory, etc.)
201+
<code>(if CONTAINER_EXPORTER_CLEAR_METRICS=True otherwise they are set to 0 to retain time series data.)</code>
202+
but keeps its status ( running/not-running/(restarting/unhealthy) ) to facilitate alerting in case something
203+
goes wrong with that contaier.
204+
</p>
205+
206+
<p>Check out a sample of the metrics output <a href="./metrics.html">here</a> to see how it works.</p>
172207
<div class="section">
173208
<h2>📚 Table of Contents</h2>
174209
<ol>
175210
<li><a href="#dev-stack">DEV STACK</a></li>
176211
<li><a href="#demo">DEMO</a></li>
177212
<li><a href="#guide">Deployment Guide</a></li>
213+
<li><a href="#settings">Config and Settings</a></li>
178214
<li><a href="#grafana">Grafana Dashboards</a></li>
179-
<li><a href="#todo">To-Do</a></li>
180-
<li><a href="#contributions">Contributions</a></li>
215+
<li><a href="#alerting">Alerting</a></li>
181216
</ol>
182217
</div>
183218

184219
<div class="section" id="dev-stack">
185220
<h2>🛠️ DEV STACK</h2>
186221
<div class="badges">
222+
187223
<img class="badge"
188224
src="https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54">
225+
<img class="badge"
226+
src="https://img.shields.io/badge/fastapi-%23000.svg?style=for-the-badge&logo=fastapi&logoColor=ffdd54">
189227
<img class="badge"
190228
src="https://img.shields.io/badge/docker-3670A0?style=for-the-badge&logo=docker&logoColor=ffff">
191229
<img class="badge"
@@ -200,6 +238,33 @@ <h2>🎥 DEMO</h2>
200238
<img src="https://shayan-ghani.github.io/Container-Exporter/CXP-DEMO.gif" alt="CXP Demo GIF">
201239
</div>
202240

241+
<div class="section" id="settings">
242+
<h2>Config and Settings </h2>
243+
<div class="config">
244+
<h3>Uvicorn</h3>
245+
<p>the following settings can be set via exporting environment variables and are applied to <code>start.sh</code> entrypoint. </p>
246+
<ul>
247+
<li>HOST (Default: 0.0.0.0)</li>
248+
<li>PORT (Default: 8000)</li>
249+
<li>WORKERS: Prometheus Multi-Processesing needed, not Implemented yet as of version [1.3.0] (Default : 1)</li>
250+
<li>LOG_LEVEL (Default : warning) </li>
251+
</ul>
252+
</div>
253+
<h3>App Settings</h3>
254+
<p>the following settings can be set via exporting environment variables or a <code>.env</code> file. </p>
255+
<ul>
256+
<li><code>CONTAINER_EXPORTER_CLEAR_METRICS</code> (Default: True)</li>
257+
<p>when set to False the metrics for container stats will not be fully cleared and set to zero to retain the time series data recorded for that container.
258+
note that only <b>Gauge Metrics</b> can be set to zero, so <code>network</code> and <code>disk io</code> settings will be fully cleared either way.
259+
</p>
260+
</ul>
261+
<div class="config">
262+
263+
</div>
264+
265+
266+
</div>
267+
203268
<div class="section" id="guide">
204269
<h2>📋 Deployment Guide</h2>
205270

@@ -210,6 +275,36 @@ <h3>Before You Start</h3>
210275
<li>Git and Python ≥ 3.10 available</li>
211276
</ul>
212277

278+
<h3>⚙️ Deploy with GitHub Actions</h3>
279+
<ul>
280+
<li>Fork the repository.</li>
281+
<li>Go to the forked repository and switch to the Actions tab.</li>
282+
<li>Click the <code>I understand my workflows, go ahead and enable them</code> button.</li>
283+
<li>Now you have access to all of the workflows; however, make sure you change the secrets listed below
284+
accordingly:
285+
<ul>
286+
<li><code>secrets.DOCKER_TOKEN</code>: the personal access token for Docker Hub of your (or your
287+
organization’s) account.</li>
288+
<li><code>secrets.GHCR_TOKEN</code>: GitHub classic access token with
289+
<code>packages:read-write</code> permissions, or simply use
290+
<code>${{ github.token }}</code>.
291+
</li>
292+
</ul>
293+
</li>
294+
</ul>
295+
296+
<h3>How Workflows Work</h3>
297+
<ul>
298+
<li>On push to <code>master</code>, the project will be built, deployed, and released. Since deploying
299+
to your servers requires runner configuration, it must be triggered manually; you can modify its
300+
behavior in the <code>cd.yml</code> workflow.</li>
301+
<li>On push to any branch except <code>master</code>, code will be built and health-checked.</li>
302+
<li>On pull request, the project will be health-checked and built.</li>
303+
</ul>
304+
305+
<bold>Double check the required variables and secrets to prevent any unexpected failures.</bold>
306+
307+
213308
<h3>With Docker Compose</h3>
214309
<pre>git clone https://github.com/Shayan-Ghani/Container-exporter.git
215310
cd Container-Exporter
@@ -236,22 +331,11 @@ <h2>📊 Grafana Dashboards</h2>
236331
to auto-replace datasource UIDs.
237332
</p>
238333
</div>
239-
240-
241-
<div class="section" id="todo">
242-
<h2>📌 To-Do</h2>
243-
<ul>
244-
<li>✅ Disk I/O metrics</li>
245-
<li>✅ Network I/O metrics</li>
246-
<li>✅ Clean metrics of dead containers</li>
247-
<li>🚧 Publish Grafana dashboards</li>
248-
<li>🚧 Build a static website</li>
249-
</ul>
250-
</div>
251-
252-
<div class="section" id="contributions">
253-
<h2>🤝 Contributions</h2>
254-
<p>Open for pull requests and issues. Help improve CXP!</p>
334+
<div class="section" id="alerting">
335+
<h2>Alerting Rules</h2>
336+
<p>
337+
A page of sample Prometheus rules can be accessed <a href="./rules.html">here</a> . setting alerts on <code>cxp_container_status</code> is one of the main points users prefer <code>CXP</code> over<code>cAdvisor.</code>
338+
</p>
255339
</div>
256340

257341
<div class="footer">

0 commit comments

Comments
 (0)