From 83d1cb2a98d89c333b9d9155fe199d4aecf3beff Mon Sep 17 00:00:00 2001 From: Lou Knauer Date: Fri, 4 Feb 2022 08:46:14 +0100 Subject: [PATCH] Add documentation, change config format --- README.md | 8 +++++--- api.go | 12 ++++++------ config.json | 6 +++++- lineprotocol.go | 6 +++--- metric-store.go | 35 +++++++++++++++++++++++++---------- 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3c8833e..791bdf4 100644 --- a/README.md +++ b/README.md @@ -74,15 +74,17 @@ All durations are specified as string that will be parsed [like this](https://pk - `"sum"` means that values from the child levels are summed up for the parent level - `"avg"` means that values from the child levels are averaged for the parent level - `scope`: Unused at the moment, should be something like `"node"`, `"socket"` or `"hwthread"` -- `nats`: Url of NATS.io server (The `updates` channel will be subscribed for metrics), example: "nats://localhost:4222" -- `http-api-address`: Where to listen via HTTP, example: ":8080" +- `nats`: + - `address`: Url of NATS.io server, example: "nats://localhost:4222" +- `http-api`: + - `address`: Address to bind to, for example `0.0.0.0:8080` + - `https-cert-file` and `https-key-file`: Optional, if provided enable HTTPS using those files as certificate/key. - `jwt-public-key`: Base64 encoded string, use this to verify requests to the HTTP API - `retention-on-memory`: Keep all values in memory for at least that amount of time - `checkpoints`: - `interval`: Do checkpoints every X seconds/minutes/hours - `directory`: Path to a directory - `restore`: After a restart, load the last X seconds/minutes/hours of data back into memory - - `archive`: - `interval`: Move and compress all checkpoints not needed anymore every X seconds/minutes/hours - `directory`: Path to a directory diff --git a/api.go b/api.go index 5bf93fb..ab294b4 100644 --- a/api.go +++ b/api.go @@ -307,7 +307,7 @@ func authentication(next http.Handler, publicKey ed25519.PublicKey) http.Handler }) } -func StartApiServer(ctx context.Context, address string, httpsConfig *HttpsConfig) error { +func StartApiServer(ctx context.Context, httpConfig *HttpConfig) error { r := mux.NewRouter() r.HandleFunc("/api/free", handleFree) @@ -322,7 +322,7 @@ func StartApiServer(ctx context.Context, address string, httpsConfig *HttpsConfi server := &http.Server{ Handler: r, - Addr: address, + Addr: httpConfig.Address, WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } @@ -337,14 +337,14 @@ func StartApiServer(ctx context.Context, address string, httpsConfig *HttpsConfi } go func() { - if httpsConfig != nil { - log.Printf("API https endpoint listening on '%s'\n", address) - err := server.ListenAndServeTLS(httpsConfig.CertFile, httpsConfig.KeyFile) + if httpConfig.CertFile != "" && httpConfig.KeyFile != "" { + log.Printf("API https endpoint listening on '%s'\n", httpConfig.Address) + err := server.ListenAndServeTLS(httpConfig.CertFile, httpConfig.KeyFile) if err != nil && err != http.ErrServerClosed { log.Println(err) } } else { - log.Printf("API http endpoint listening on '%s'\n", address) + log.Printf("API http endpoint listening on '%s'\n", httpConfig.Address) err := server.ListenAndServe() if err != nil && err != http.ErrServerClosed { log.Println(err) diff --git a/config.json b/config.json index 16fa894..e707c9c 100644 --- a/config.json +++ b/config.json @@ -15,8 +15,12 @@ "interval": "168h", "directory": "./var/archive" }, + "http-api": { + "address": "0.0.0.0:8081", + "https-cert-file": null, + "https-key-file": null + }, "retention-in-memory": "48h", - "http-api-address": "0.0.0.0:8081", "nats": null, "jwt-public-key": "kzfYrYy+TzpanWZHJ5qSdMj5uKUWgq74BWhQG6copP0=" } diff --git a/lineprotocol.go b/lineprotocol.go index 743b737..5ed8967 100644 --- a/lineprotocol.go +++ b/lineprotocol.go @@ -20,8 +20,8 @@ type Metric struct { // Connect to a nats server and subscribe to "updates". This is a blocking // function. handleLine will be called for each line recieved via nats. // Send `true` through the done channel for gracefull termination. -func ReceiveNats(address string, handleLine func(dec *lineprotocol.Decoder) error, workers int, ctx context.Context) error { - nc, err := nats.Connect(address) +func ReceiveNats(conf *NatsConfig, handleLine func(dec *lineprotocol.Decoder) error, workers int, ctx context.Context) error { + nc, err := nats.Connect(conf.Address) if err != nil { return err } @@ -64,7 +64,7 @@ func ReceiveNats(address string, handleLine func(dec *lineprotocol.Decoder) erro return err } - log.Printf("NATS subscription to 'updates' on '%s' established\n", address) + log.Printf("NATS subscription to 'updates' on '%s' established\n", conf.Address) <-ctx.Done() err = sub.Unsubscribe() diff --git a/metric-store.go b/metric-store.go index 4775442..d1d16cb 100644 --- a/metric-store.go +++ b/metric-store.go @@ -15,23 +15,38 @@ import ( ) type MetricConfig struct { - Frequency int64 `json:"frequency"` + // Interval in seconds at which measurements will arive. + Frequency int64 `json:"frequency"` + + // Can be 'sum', 'avg' or null. Describes how to aggregate metrics from the same timestep over the hierarchy. Aggregation string `json:"aggregation"` - Scope string `json:"scope"` + + // Unused for now. + Scope string `json:"scope"` } -type HttpsConfig struct { - CertFile string `json:"cert"` - KeyFile string `json:"key"` +type HttpConfig struct { + // Address to bind to, for example "0.0.0.0:8081" + Address string `json:"address"` + + // If not the empty string, use https with this as the certificate file + CertFile string `json:"https-cert-file"` + + // If not the empty string, use https with this as the key file + KeyFile string `json:"https-key-file"` +} + +type NatsConfig struct { + // Address of the nats server + Address string `json:"address"` } type Config struct { Metrics map[string]MetricConfig `json:"metrics"` RetentionInMemory string `json:"retention-in-memory"` - Nats string `json:"nats"` + Nats *NatsConfig `json:"nats"` JwtPublicKey string `json:"jwt-public-key"` - HttpApiAddress string `json:"http-api-address"` - HttpsConfig *HttpsConfig `json:"https"` + HttpConfig *HttpConfig `json:"http-api"` Checkpoints struct { Interval string `json:"interval"` RootDir string `json:"directory"` @@ -195,14 +210,14 @@ func main() { wg.Add(1) go func() { - err := StartApiServer(ctx, conf.HttpApiAddress, conf.HttpsConfig) + err := StartApiServer(ctx, conf.HttpConfig) if err != nil { log.Fatal(err) } wg.Done() }() - if len(conf.Nats) != 0 { + if conf.Nats != nil { wg.Add(1) go func() {