From 5bd6cbc9a1cd478bf90ecea659c5d844d9af1431 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Sat, 1 Jul 2023 12:32:45 -0700 Subject: [PATCH] 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 --- dashboards/README.md | 10 ++--- dashboards/datasources.yml | 2 +- dashboards/docker-compose-linux.yml | 32 +++------------- ...ompose-osx.yml => docker-compose.base.yml} | 0 examples/metrics-and-dashboards/README.md | 2 +- examples/metrics-and-dashboards/compose.yml | 38 +++---------------- .../grafana-config/grafana.ini | 5 --- .../provisioning/dashboards/dashboards.yml | 25 ------------ .../provisioning/datasources/datasource.yml | 15 -------- .../metrics-and-dashboards/grafana.Dockerfile | 10 ----- examples/metrics-and-dashboards/main.go | 3 +- .../metrics-and-dashboards/prometheus.yml | 3 +- 12 files changed, 22 insertions(+), 123 deletions(-) rename dashboards/{docker-compose-osx.yml => docker-compose.base.yml} (100%) delete mode 100644 examples/metrics-and-dashboards/grafana-config/grafana.ini delete mode 100644 examples/metrics-and-dashboards/grafana-config/provisioning/dashboards/dashboards.yml delete mode 100644 examples/metrics-and-dashboards/grafana-config/provisioning/datasources/datasource.yml delete mode 100644 examples/metrics-and-dashboards/grafana.Dockerfile diff --git a/dashboards/README.md b/dashboards/README.md index a0307b68e..6080dc03f 100644 --- a/dashboards/README.md +++ b/dashboards/README.md @@ -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. diff --git a/dashboards/datasources.yml b/dashboards/datasources.yml index ec9c1f28a..ed47ec11a 100644 --- a/dashboards/datasources.yml +++ b/dashboards/datasources.yml @@ -9,5 +9,5 @@ datasources: orgId: 1 type: prometheus access: proxy - url: http://host.docker.internal:9090 + url: http://prometheus:9090 editable: false diff --git a/dashboards/docker-compose-linux.yml b/dashboards/docker-compose-linux.yml index 3e016e5a6..da9293249 100644 --- a/dashboards/docker-compose-linux.yml +++ b/dashboards/docker-compose-linux.yml @@ -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" diff --git a/dashboards/docker-compose-osx.yml b/dashboards/docker-compose.base.yml similarity index 100% rename from dashboards/docker-compose-osx.yml rename to dashboards/docker-compose.base.yml diff --git a/examples/metrics-and-dashboards/README.md b/examples/metrics-and-dashboards/README.md index 6a41fb4e8..9218994d2 100644 --- a/examples/metrics-and-dashboards/README.md +++ b/examples/metrics-and-dashboards/README.md @@ -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. diff --git a/examples/metrics-and-dashboards/compose.yml b/examples/metrics-and-dashboards/compose.yml index f5a675119..fb10b4a81 100644 --- a/examples/metrics-and-dashboards/compose.yml +++ b/examples/metrics-and-dashboards/compose.yml @@ -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 diff --git a/examples/metrics-and-dashboards/grafana-config/grafana.ini b/examples/metrics-and-dashboards/grafana-config/grafana.ini deleted file mode 100644 index 91bab4865..000000000 --- a/examples/metrics-and-dashboards/grafana-config/grafana.ini +++ /dev/null @@ -1,5 +0,0 @@ -[paths] -provisioning = /etc/grafana/provisioning - -[server] -enable_gzip = true diff --git a/examples/metrics-and-dashboards/grafana-config/provisioning/dashboards/dashboards.yml b/examples/metrics-and-dashboards/grafana-config/provisioning/dashboards/dashboards.yml deleted file mode 100644 index 27bbeef59..000000000 --- a/examples/metrics-and-dashboards/grafana-config/provisioning/dashboards/dashboards.yml +++ /dev/null @@ -1,25 +0,0 @@ -# config file version -apiVersion: 1 - -providers: - # an unique provider name - - name: My Dashboard - # org id. will default to orgId 1 if not specified - org_id: 1 - # name of the dashboard folder. Required - folder: "" - # provider type. Required - type: "file" - # disable dashboard deletion - disableDeletion: false - # enable dashboard editing - editable: true - # how often Grafana will scan for changed dashboards - updateIntervalSeconds: 5 - # allow updating provisioned dashboards from the UI - allowUiUpdates: true - options: - # path to dashboard files on disk. Required - path: /etc/grafana/dashboards - # use folder names from filesystem to create folders in Grafana - foldersFromFilesStructure: true diff --git a/examples/metrics-and-dashboards/grafana-config/provisioning/datasources/datasource.yml b/examples/metrics-and-dashboards/grafana-config/provisioning/datasources/datasource.yml deleted file mode 100644 index eed755880..000000000 --- a/examples/metrics-and-dashboards/grafana-config/provisioning/datasources/datasource.yml +++ /dev/null @@ -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 diff --git a/examples/metrics-and-dashboards/grafana.Dockerfile b/examples/metrics-and-dashboards/grafana.Dockerfile deleted file mode 100644 index f9d71e48e..000000000 --- a/examples/metrics-and-dashboards/grafana.Dockerfile +++ /dev/null @@ -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 diff --git a/examples/metrics-and-dashboards/main.go b/examples/metrics-and-dashboards/main.go index 9ca0bcf06..85d78e4e4 100644 --- a/examples/metrics-and-dashboards/main.go +++ b/examples/metrics-and-dashboards/main.go @@ -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) diff --git a/examples/metrics-and-dashboards/prometheus.yml b/examples/metrics-and-dashboards/prometheus.yml index a532a5fb2..3f86ad0c2 100644 --- a/examples/metrics-and-dashboards/prometheus.yml +++ b/examples/metrics-and-dashboards/prometheus.yml @@ -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"]