-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: add example for metrics and dashboard (#2232)
- Loading branch information
Showing
11 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Metrics and Dashboards | ||
|
||
An example to demonstrate using Prometheus and Grafana to view go-libp2p | ||
metrics. Sets up a Prometheus server and Grafana server via Docker compose. A | ||
small go-libp2p dummy application is included to emit metrics. | ||
|
||
Run it with: | ||
|
||
``` | ||
docker compose up | ||
``` | ||
|
||
Go to http://localhost:3000/dashboards to see the dashboards. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
services: | ||
|
||
prometheus-server: | ||
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 | ||
volumes: | ||
- ./grafana-config/grafana.ini:/etc/grafana/grafana.ini | ||
- ./grafana-config/provisioning:/etc/grafana/provisioning | ||
ports: | ||
- 3000:3000 | ||
|
||
go-libp2p-node: | ||
build: | ||
context: . | ||
dockerfile: go-libp2p-node.Dockerfile | ||
ports: | ||
- 2112:2112 | ||
expose: | ||
- 2112 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM golang:alpine | ||
WORKDIR /app | ||
COPY ./main.go . | ||
RUN go mod init example.com/m/v2 | ||
RUN go mod tidy | ||
RUN go build main.go | ||
ENTRYPOINT [ "/app/main" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[paths] | ||
provisioning = /etc/grafana/provisioning | ||
|
||
[server] | ||
enable_gzip = true |
25 changes: 25 additions & 0 deletions
25
examples/metrics-and-dashboards/grafana-config/provisioning/dashboards/dashboards.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 |
15 changes: 15 additions & 0 deletions
15
examples/metrics-and-dashboards/grafana-config/provisioning/datasources/datasource.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/ping" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
|
||
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" | ||
rcmgrObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs" | ||
) | ||
|
||
const ClientCount = 32 | ||
|
||
func main() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
go func() { | ||
http.ListenAndServe(":2112", nil) | ||
}() | ||
|
||
rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer) | ||
|
||
str, err := rcmgrObs.NewStatsTraceReporter() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
rmgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(rcmgr.DefaultLimits.AutoScale()), rcmgr.WithTraceReporter(str)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
server, err := libp2p.New(libp2p.ResourceManager(rmgr)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Make a bunch of clients that all ping the server at various times | ||
wg := sync.WaitGroup{} | ||
for i := 0; i < ClientCount; i++ { | ||
wg.Add(1) | ||
go func(i int) { | ||
defer wg.Done() | ||
time.Sleep(time.Duration(i%100) * 100 * time.Millisecond) | ||
newClient(peer.AddrInfo{ | ||
ID: server.ID(), | ||
Addrs: server.Addrs(), | ||
}, i) | ||
}(i) | ||
} | ||
wg.Wait() | ||
} | ||
|
||
func newClient(serverInfo peer.AddrInfo, pings int) { | ||
// Sleep some random amount of time to spread out the clients so the graphs look more interesting | ||
time.Sleep(time.Duration(rand.Intn(100)) * time.Second) | ||
fmt.Println("Started client", pings) | ||
|
||
client, err := libp2p.New( | ||
// We just want metrics from the server | ||
libp2p.DisableMetrics(), | ||
libp2p.NoListenAddrs, | ||
) | ||
defer func() { | ||
_ = client.Close() | ||
}() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
client.Connect(context.Background(), serverInfo) | ||
|
||
p := ping.Ping(context.Background(), client, serverInfo.ID) | ||
|
||
pingSoFar := 0 | ||
for pingSoFar < pings { | ||
res := <-p | ||
pingSoFar++ | ||
if res.Error != nil { | ||
log.Fatal(res.Error) | ||
} | ||
time.Sleep(5 * time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
global: | ||
scrape_interval: 1m | ||
|
||
scrape_configs: | ||
- job_name: "prometheus" | ||
scrape_interval: 1m | ||
static_configs: | ||
- targets: ["localhost:9090"] | ||
|
||
- job_name: "node" | ||
static_configs: | ||
- targets: ["node-exporter:9100"] | ||
- job_name: "go-libp2p" | ||
static_configs: | ||
- targets: ["go-libp2p-node:2112"] |