-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create docker file for docker-exporter
- Loading branch information
Deepak Tiwari
committed
Dec 13, 2024
1 parent
500a88a
commit 7510330
Showing
19 changed files
with
2,004 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Build and Push Docker-Exporter Image | ||
|
||
on: | ||
push: | ||
branches: [ '*' ] | ||
paths: | ||
- 'docker-exporter/**' | ||
- '.github/workflows/docker-exporter.yaml' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
|
||
- name: Generate container metadata | ||
id: meta | ||
uses: docker/metadata-action@v3 | ||
with: | ||
images: ghcr.io/obmondo/dockerfiles/docker-exporter | ||
tags: | | ||
type=semver,pattern={{version}},value=v0.3.0 | ||
flavor: | | ||
latest=false | ||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.repository_owner }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build & push container image | ||
id: docker_build | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: ./docker-exporter | ||
labels: ${{ steps.meta.outputs.labels }} | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} |
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,30 @@ | ||
FROM golang:1.22-alpine3.21 AS base | ||
|
||
RUN adduser -D -H docker-exporter | ||
|
||
ENV GO111MODULE=on \ | ||
CGO_ENABLED=0 \ | ||
GOOS=linux \ | ||
GOARCH=amd64 | ||
|
||
WORKDIR /build | ||
|
||
COPY . . | ||
|
||
RUN go mod download | ||
|
||
FROM base AS build | ||
|
||
RUN go build -o docker-exporter -tags prod main.go | ||
|
||
FROM scratch AS prod | ||
|
||
COPY --from=build /etc/passwd /etc/passwd | ||
COPY --from=build /etc/group /etc/group | ||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
|
||
COPY --from=build /build/docker-exporter / | ||
|
||
USER docker-exporter:docker-exporter | ||
|
||
CMD ["./docker-exporter"] |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 David Borzek | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,95 @@ | ||
# docker exporter | ||
|
||
[![Go Report Card](https://goreportcard.com/badge/github.com/davidborzek/docker-exporter)](https://goreportcard.com/report/github.com/davidborzek/docker-exporter) | ||
|
||
Simple and lightweight Prometheus exporter for docker container metrics. | ||
|
||
## Prerequisites | ||
|
||
- [Go](https://golang.org/doc/) | ||
|
||
## Installation | ||
|
||
### Using Docker | ||
|
||
The exporter is available as a Docker image. | ||
You can run it using the following example: | ||
|
||
``` | ||
$ docker run \ | ||
-u root \ | ||
-v /var/run/docker.sock:/var/run/docker.sock \ | ||
-p 8080:8080 \ | ||
ghcr.io/davidborzek/docker-exporter:latest | ||
``` | ||
|
||
> Note: To run Docker Exporter, you'll need to mount the Docker socket from your host system. This operation necessitates root privileges or the user running the command to be a member of the Docker group. It's important to note that mounting the Docker socket grants the container unrestricted access to Docker. For a more secure approach, consider utilizing the Docker Socket Proxy, which is further explained below for additional information. | ||
### Running with [docker-socket-proxy](https://github.com/Tecnativa/docker-socket-proxy) | ||
|
||
``` | ||
$ docker run \ | ||
-e "DOCKER_HOST=tcp://localhost:2375" \ | ||
-p 8080:8080 \ | ||
ghcr.io/davidborzek/docker-exporter:latest | ||
``` | ||
|
||
> Note: the [docker-socket-proxy](https://github.com/Tecnativa/docker-socket-proxy#not-always-needed) needs to have container access enabled. (`CONTAINERS=1`) | ||
### Prometheus config | ||
|
||
Once you have configured the exporter, update your `prometheus.yml` scrape config: | ||
|
||
```yaml | ||
scrape_configs: | ||
- job_name: "docker_exporter" | ||
static_configs: | ||
- targets: ["localhost:8080"] | ||
``` | ||
### Config | ||
| Flag | Description | Default Value | Environment Variable | | ||
| ---------------- | ---------------------------------------------------------------------------------------------------- | ------------------------ | ------------------------------ | | ||
| `--port` | The port of docker exporter server. | `8080` | `DOCKER_EXPORTER_PORT` | | ||
| `--host` | The host of docker exporter server. | | `DOCKER_EXPORTER_HOST` | | ||
| `--auth-token` | Optional auth token for the docker exporter server. If no token is set authentication is disabled. | | `DOCKER_EXPORTER_AUTH_TOKEN` | | ||
| `--log-level` | Log level for the exporter. | `info` | `DOCKER_EXPORTER_LOG_LEVEL` | | ||
| `--ignore-label` | Set the label name for ignoring docker containers. (See [Ignoring Containers](#ignoring-containers)) | `docker-exporter.ignore` | `DOCKER_EXPORTER_IGNORE_LABEL` | | ||
|
||
### Exported Metrics | ||
|
||
| Metric Name | Description | Labels | | ||
| ------------------------------------------- | ---------------------------------- | ----------------------- | | ||
| docker_container_block_io_read_bytes | Block I/O read bytes total | name | | ||
| docker_container_block_io_write_bytes | Block I/O write bytes total | name | | ||
| docker_container_cpu_usage_percentage | CPU usage in percentage | name | | ||
| docker_container_info | Infos about the container | name, image_name, image | | ||
| docker_container_memory_total_bytes | Total memory in bytes | name | | ||
| docker_container_memory_usage_bytes | Memory usage in bytes | name | | ||
| docker_container_memory_usage_percentage | Memory usage in percentage | name | | ||
| docker_container_network_rx_bytes | Network received bytes total | name, network | | ||
| docker_container_network_rx_dropped_packets | Network dropped packets total | name, network | | ||
| docker_container_network_rx_errors | Network received errors | name, network | | ||
| docker_container_network_rx_packets | Network received packets total | name, network | | ||
| docker_container_network_tx_bytes | Network sent bytes total | name, network | | ||
| docker_container_network_tx_dropped_packets | Network dropped packets total | name, network | | ||
| docker_container_network_tx_errors | Network sent errors | name, network | | ||
| docker_container_network_tx_packets | Network sent packets total | name, network | | ||
| docker_container_pids_current | Current number of pids | name | | ||
| docker_container_state | State of the container | name, state | | ||
| docker_container_uptime | Uptime of the container in seconds | name | | ||
| docker_exporter_scrape_duration | Duration of the scrape in seconds | | | ||
| docker_exporter_scrape_errors | Number of scrape errors | | | ||
|
||
### Ignoring Containers | ||
|
||
You can ignore containers by setting the label `docker-exporter.ignore` on the container. The label name can be configured with the `--ignore-label` flag. | ||
|
||
```yaml | ||
services: | ||
nginx: | ||
image: nginx | ||
labels: | ||
docker-exporter.ignore: "true" | ||
``` |
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,123 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/davidborzek/docker-exporter/internal/clock" | ||
"github.com/davidborzek/docker-exporter/internal/collector" | ||
"github.com/davidborzek/docker-exporter/internal/handler" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/urfave/cli/v2" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
version = "v0.3.0" | ||
) | ||
|
||
var ( | ||
flags = []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "port", | ||
Value: "8080", | ||
Usage: "The port of docker exporter server", | ||
EnvVars: []string{"DOCKER_EXPORTER_PORT"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "host", | ||
Usage: "The host of docker exporter server", | ||
EnvVars: []string{"DOCKER_EXPORTER_HOST"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "auth-token", | ||
Usage: "Optional auth token for the docker exporter server. If no token is set authentication is disabled.", | ||
EnvVars: []string{"DOCKER_EXPORTER_AUTH_TOKEN"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "log-level", | ||
Usage: "Log level", | ||
Value: "info", | ||
EnvVars: []string{"DOCKER_EXPORTER_LOG_LEVEL"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "ignore-label", | ||
Usage: "Label to ignore containers", | ||
Value: "docker-exporter.ignore", | ||
EnvVars: []string{"DOCKER_EXPORTER_IGNORE_LABEL"}, | ||
}, | ||
} | ||
) | ||
|
||
func parseLogLevel(level string) log.Level { | ||
switch strings.ToLower(level) { | ||
case "debug": | ||
return log.DebugLevel | ||
case "info": | ||
return log.InfoLevel | ||
case "warning": | ||
return log.WarnLevel | ||
case "error": | ||
return log.ErrorLevel | ||
case "fatal": | ||
return log.FatalLevel | ||
} | ||
|
||
log.WithField("level", level). | ||
Warn("invalid log level provided - falling back to 'info'") | ||
|
||
return log.InfoLevel | ||
} | ||
|
||
func start(ctx *cli.Context) error { | ||
log.SetFormatter(&log.TextFormatter{ | ||
FullTimestamp: true, | ||
}) | ||
|
||
log.SetLevel( | ||
parseLogLevel(ctx.String("log-level")), | ||
) | ||
|
||
log.WithField("pid", os.Getpid()). | ||
Info("docker prometheus exporter started") | ||
|
||
token := ctx.String("auth-token") | ||
if len(token) > 0 { | ||
log.Info("authentication is enabled") | ||
} | ||
|
||
dc, err := collector.NewDockerCollector(clock.NewClock(), ctx.String("ignore-label")) | ||
if err != nil { | ||
log.WithError(err). | ||
Fatal("failed to create docker collector") | ||
} | ||
|
||
prometheus.MustRegister(dc) | ||
|
||
h := handler.New(token) | ||
|
||
addr := net.JoinHostPort( | ||
ctx.String("host"), ctx.String("port")) | ||
log.WithField("addr", addr). | ||
Infof("starting the http server") | ||
|
||
return http.ListenAndServe(addr, h) | ||
} | ||
|
||
func Main(args []string) { | ||
app := cli.App{ | ||
Name: "Docker Prometheus exporter", | ||
Usage: "Export Docker metrics to prometheus format", | ||
Action: start, | ||
Flags: flags, | ||
Version: version, | ||
} | ||
|
||
if err := app.Run(args); err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
} |
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,62 @@ | ||
module github.com/davidborzek/docker-exporter | ||
|
||
go 1.22 | ||
|
||
toolchain go1.23.4 | ||
|
||
require ( | ||
github.com/docker/docker v27.4.0+incompatible | ||
github.com/prometheus/client_golang v1.20.5 | ||
github.com/sirupsen/logrus v1.9.3 | ||
github.com/stretchr/testify v1.10.0 | ||
github.com/urfave/cli/v2 v2.27.5 | ||
go.uber.org/mock v0.5.0 | ||
) | ||
|
||
require ( | ||
github.com/Microsoft/go-winio v0.6.1 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/distribution/reference v0.5.0 // indirect | ||
github.com/docker/distribution v2.8.2+incompatible // indirect | ||
github.com/docker/go-connections v0.4.0 // indirect | ||
github.com/docker/go-units v0.5.0 // indirect | ||
github.com/felixge/httpsnoop v1.0.4 // indirect | ||
github.com/go-logr/logr v1.4.1 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/klauspost/compress v1.17.9 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/kylelemons/godebug v1.1.0 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect | ||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect | ||
github.com/moby/docker-image-spec v1.3.1 // indirect | ||
github.com/moby/term v0.5.0 // indirect | ||
github.com/morikuni/aec v1.0.0 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/opencontainers/go-digest v1.0.0 // indirect | ||
github.com/opencontainers/image-spec v1.0.2 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/client_model v0.6.1 // indirect | ||
github.com/prometheus/common v0.55.0 // indirect | ||
github.com/prometheus/procfs v0.15.1 // indirect | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect | ||
go.opentelemetry.io/otel v1.22.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.22.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.22.0 // indirect | ||
golang.org/x/mod v0.18.0 // indirect | ||
golang.org/x/net v0.26.0 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.22.0 // indirect | ||
golang.org/x/time v0.3.0 // indirect | ||
golang.org/x/tools v0.22.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
gotest.tools/v3 v3.5.1 // indirect | ||
) |
Oops, something went wrong.