Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Metrics Feature #775

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package main

import (
"net/http"
"os"
"os/signal"
"path"
"strconv"

"github.com/crazy-max/diun/v4/internal/app"
"github.com/crazy-max/diun/v4/internal/config"
"github.com/crazy-max/diun/v4/internal/logging"
"github.com/crazy-max/diun/v4/pkg/utl"
"github.com/pkg/profile"
"github.com/rs/zerolog/log"

diunApi "github.com/crazy-max/diun/v4/pkg/api"
apiMetrics "github.com/crazy-max/diun/v4/pkg/api/metrics"
apiScan "github.com/crazy-max/diun/v4/pkg/api/scan"
)

// ServeCmd holds serve command args and flags
Expand Down Expand Up @@ -89,6 +95,28 @@ func (s *ServeCmd) Run(ctx *Context) error {
log.Fatal().Err(err).Msgf("Cannot initialize %s", ctx.Meta.Name)
}

if *cfg.APIMetrics.EnableAPI || *cfg.APIMetrics.EnableScan {
httpAPI := diunApi.New(cfg.APIMetrics.Token, cfg.APIMetrics.Port)

if *cfg.APIMetrics.EnableAPI {
metricsHandler := apiMetrics.New()
httpAPI.RegisterFunc(cfg.APIMetrics.APIPath, metricsHandler.Handle)
log.Info().Msgf("API Server Registered: %s", cfg.APIMetrics.APIPath)
}

if *cfg.APIMetrics.EnableScan {
scanHandler := apiScan.New(func() { diun.Run() })
httpAPI.RegisterFunc(cfg.APIMetrics.ScanPath, scanHandler.Handle)
log.Info().Msgf("API Server Registered: %s", cfg.APIMetrics.ScanPath)
}

if err := httpAPI.Start(false); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msgf("failed to start API %s", err)
}

log.Info().Msgf("API Server Started Port: %s EnableAPI: %s EnableScan: %s", cfg.APIMetrics.Port, strconv.FormatBool(*cfg.APIMetrics.EnableAPI), strconv.FormatBool(*cfg.APIMetrics.EnableScan))
}

// Start
err = diun.Start()
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions docs/config/apimetrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# API Metrics configuration

## Overview

```yaml
apimetrics:
enableApi: true
enableScan: true
token: ApiToken
port: 6080
apiPath: /v1/metrics
scanPath: /v1/scan
Comment on lines +6 to +12
Copy link

@jooola jooola Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keys have inconsistent casing. It should probably be snake case ?

```

## Configuration

### `token`

Authentication Bearer Token used for accessing the Metrics API endpoint and the Scan API endpoint. (default: ApiToken)

!!! example "Config file"
```yaml
apimetrics:
token: ApiToken
```

!!! abstract "Environment variables"
* `DIUN_APIMETRICS_TOKEN`

### `port`

TCP port used for the http api. (default: 6080)

!!! example "Config file"
```yaml
apimetrics:
port: 6080
```

!!! abstract "Environment variables"
* `DIUN_APIMETRICS_PORT`

### `enableApi`

Enable or disable the Metrics endpoint. (default: false)

!!! example "Config file"
```yaml
apimetrics:
enableApi: true
```

!!! abstract "Environment variables"
* `DIUN_APIMETRICS_ENABLEAPI`

### `apiPath`

Path to expose the API Metrics on. (default: /v1/metrics)

!!! example "Config file"
```yaml
apimetrics:
apiPath: /v1/metrics
```

!!! abstract "Environment variables"
* `DIUN_APIMETRICS_APIPATH`

### `enableScan`

Enable or disable the Scan endpoint. The Scan endpoint allows for the triggering of a re-scan of the images. (default: false)

!!! example "Config file"
```yaml
apimetrics:
enableScan: true
```

!!! abstract "Environment variables"
* `DIUN_APIMETRICS_ENABLESCAN`

### `scanPath`

Path to expose the API Metrics on. (default: /v1/scann)

!!! example "Config file"
```yaml
apimetrics:
apiScan: true
```

!!! abstract "Environment variables"
* `DIUN_APIMETRICS_APISCAN`
1 change: 1 addition & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,4 @@ Can be transposed to:
* [nomad](../providers/nomad.md)
* [dockerfile](../providers/dockerfile.md)
* [file](../providers/file.md)
* [apimetrics](apimetrics.md)
20 changes: 18 additions & 2 deletions docs/providers/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,28 @@ Include created and exited containers too (default `false`).
!!! abstract "Environment variables"
* `DIUN_PROVIDERS_DOCKER_WATCHSTOPPED`

### `pullImages`

Pull new image if found in the remote repository. (default `false`).
This feature is used in conjunction with the apimetrics.

!!! example "File"
```yaml
providers:
docker:
pullImages: true
```

!!! abstract "Environment variables"
* `DIUN_PROVIDERS_DOCKER_PULLIMAGES`


## Docker labels

You can configure more finely the way to analyze the image of your container through Docker labels:

| Name | Default | Description |
|---------------------|--------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| ------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `diun.enable` | | Set to true to enable image analysis of this container |
| `diun.regopt` | | [Registry options](../config/regopts.md) name to use |
| `diun.watch_repo` | `false` | Watch all tags of this container image ([be careful](../faq.md#docker-hub-rate-limits) with this setting) |
Expand All @@ -190,7 +206,7 @@ You can configure more finely the way to analyze the image of your container thr
## Default metadata

| Key | Description |
|-------------------------------|-------------------------|
| ----------------------------- | ----------------------- |
| `diun.metadata.ctn_id` | Container ID |
| `diun.metadata.ctn_names` | Container names |
| `diun.metadata.ctn_command` | Container command |
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ require (
k8s.io/client-go v0.25.4
)

require github.com/prometheus/client_golang v1.12.1

require (
github.com/BurntSushi/toml v1.2.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
Expand All @@ -63,6 +65,8 @@ require (
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/aokoli/goutils v1.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/containers/libtrust v0.0.0-20200511145503-9c3a6c22cd9a // indirect
github.com/containers/ocicrypt v1.1.5 // indirect
Expand Down Expand Up @@ -105,6 +109,7 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -116,6 +121,9 @@ require (
github.com/opencontainers/runc v1.1.4 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
Expand Down
Loading