metrics: deduplicate code in examples (#2404)

* Dedup code in metrics examples

* Readme tweaks

* Remove unused code

* Remove unused code

* Use extra_hosts for grafana on linux
This commit is contained in:
Marco Munizaga
2023-07-01 12:32:45 -07:00
committed by GitHub
parent fd7818473e
commit 5bd6cbc9a1
12 changed files with 22 additions and 123 deletions

View File

@@ -20,13 +20,13 @@ go func() {
This exposes a metrics collection endpoint at http://localhost:5001/debug/metrics/prometheus. Note that this is the same endpoint that [Kubo](https://github.com/ipfs/kubo) uses, so if you want to gather metrics from Kubo, you can skip this step.
On Linux, dashboards can be inspected locally by running:
```bash
docker-compose -f docker-compose-linux.yml up
```
On macOS:
```bash
docker-compose -f docker-compose-osx.yml up
docker compose -f docker-compose.base.yml up
```
On Linux, dashboards can be inspected locally by running:
```bash
docker compose -f docker-compose.base.yml -f docker-compose-linux.yml up
```
and opening Grafana at http://localhost:3000.

View File

@@ -9,5 +9,5 @@ datasources:
orgId: 1
type: prometheus
access: proxy
url: http://host.docker.internal:9090
url: http://prometheus:9090
editable: false

View File

@@ -1,34 +1,12 @@
version: "3.7"
services:
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
network_mode: "host"
extra_hosts: # define a host.docker.internal alias, so we can use the same datasources.yml and prometheus.yml on Linux and OSX
extra_hosts:
# define a host.docker.internal alias, so we can use the same prometheus.yml on Linux and macOS
- "host.docker.internal:127.0.0.1"
grafana:
image: grafana/grafana:latest
depends_on:
- prometheus
ports:
- "3000:3000"
network_mode: "host"
extra_hosts: # define a host.docker.internal alias, so we can use the same datasources.yml and prometheus.yml on Linux and OSX
- "host.docker.internal:127.0.0.1"
environment:
- GF_AUTH_DISABLE_LOGIN_FORM=true
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
volumes:
- ./dashboard.yml:/etc/grafana/provisioning/dashboards/main.yml
- ./datasources.yml:/etc/grafana/provisioning/datasources/prom.yml
- ./autonat/autonat.json:/var/lib/grafana/dashboards/autonat.json
- ./autorelay/autorelay.json:/var/lib/grafana/dashboards/autorelay.json
- ./eventbus/eventbus.json:/var/lib/grafana/dashboards/eventbus.json
- ./holepunch/holepunch.json:/var/lib/grafana/dashboards/holepunch.json
- ./identify/identify.json:/var/lib/grafana/dashboards/identify.json
- ./relaysvc/relaysvc.json:/var/lib/grafana/dashboards/relaysvc.json
- ./swarm/swarm.json:/var/lib/grafana/dashboards/swarm.json
extra_hosts:
# define a prometheus alias, so we can use the same datasources.yml on Linux and macOS
- "prometheus:127.0.0.1"

View File

@@ -7,7 +7,7 @@ small go-libp2p dummy application is included to emit metrics.
Run it with:
```
docker compose up
docker compose -f ../../dashboards/docker-compose.base.yml -f ./compose.yml up
```
Go to http://localhost:3000/dashboards to see the dashboards.

View File

@@ -1,41 +1,15 @@
services:
prometheus-server:
prometheus:
image: prom/prometheus:latest
restart: unless-stopped
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.enable-lifecycle'
ports:
- 9090:9090
expose:
- 9090
grafana:
# image: sha256:b4cfd1a8f2d221019265ca75b20e066b5ddd34631210441d5877914a0af2fd67
build:
dockerfile: examples/metrics-and-dashboards/grafana.Dockerfile
context: ../../
environment:
- GF_AUTH_DISABLE_LOGIN_FORM=true
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- "9090:9090"
volumes:
- ./grafana-config/grafana.ini:/etc/grafana/grafana.ini
- ./grafana-config/provisioning:/etc/grafana/provisioning
ports:
- 3000:3000
- ../examples/metrics-and-dashboards/prometheus.yml:/etc/prometheus/prometheus.yml
go-libp2p-node:
build:
context: .
context: ../examples/metrics-and-dashboards/
dockerfile: go-libp2p-node.Dockerfile
ports:
- 2112:2112
- 5001:5001
expose:
- 2112
- 5001

View File

@@ -1,5 +0,0 @@
[paths]
provisioning = /etc/grafana/provisioning
[server]
enable_gzip = true

View File

@@ -1,25 +0,0 @@
# config file version
apiVersion: 1
providers:
# <string> an unique provider name
- name: My Dashboard
# <int> org id. will default to orgId 1 if not specified
org_id: 1
# <string, required> name of the dashboard folder. Required
folder: ""
# <string, required> provider type. Required
type: "file"
# <bool> disable dashboard deletion
disableDeletion: false
# <bool> enable dashboard editing
editable: true
# <int> how often Grafana will scan for changed dashboards
updateIntervalSeconds: 5
# <bool> allow updating provisioned dashboards from the UI
allowUiUpdates: true
options:
# <string, required> path to dashboard files on disk. Required
path: /etc/grafana/dashboards
# <bool> use folder names from filesystem to create folders in Grafana
foldersFromFilesStructure: true

View File

@@ -1,15 +0,0 @@
# config file version
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
# Access mode - proxy (server in the UI) or direct (browser in the UI).
access: proxy
uid: "prometheusuid"
url: http://prometheus-server:9090
jsonData:
httpMethod: POST
manageAlerts: true
prometheusType: Prometheus
prometheusVersion: 2.43.0

View File

@@ -1,10 +0,0 @@
FROM grafana/grafana
COPY ./dashboards /etc/grafana/dashboards
USER root
# Replace every instrance of ${DS_PROMETHEUS} with "prometheusuid"
# Grafana doesn't let us do this in provisioning, so we do it ourselves.
RUN sed -i 's/${DS_PROMETHEUS}/prometheusuid/g' /etc/grafana/dashboards/*/*.json
RUN cat /etc/grafana/dashboards/swarm/swarm.json
USER grafana

View File

@@ -24,7 +24,8 @@ const ClientCount = 32
func main() {
http.Handle("/metrics", promhttp.Handler())
go func() {
http.ListenAndServe(":2112", nil)
http.Handle("/debug/metrics/prometheus", promhttp.Handler())
log.Fatal(http.ListenAndServe(":5001", nil))
}()
rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer)

View File

@@ -11,5 +11,6 @@ scrape_configs:
static_configs:
- targets: ["node-exporter:9100"]
- job_name: "go-libp2p"
metrics_path: /debug/metrics/prometheus
static_configs:
- targets: ["go-libp2p-node:2112"]
- targets: ["go-libp2p-node:5001"]