Skip to content

Commit

Permalink
chore: prepare for first release
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan committed Jun 17, 2022
1 parent e989577 commit 55d2516
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 9 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: goreleaser

on:
push:
tags:
- "*"

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GH_GORELEASER_TOKEN }}
21 changes: 21 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
env:
- GO111MODULE=on
- CGO_ENABLED=0

builds:
- binary: nomad-vector-logger.bin
id: nomad-vector-logger
goos:
- linux
goarch:
- amd64
ldflags:
- -s -w -X "main.buildString={{ .Tag }} ({{ .ShortCommit }} {{ .Date }})"
dir: .

archives:
- format: tar.gz
files:
- README.md
- LICENSE
- config.sample.toml
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP-BIN := ./bin/nomad-events-sink.bin
APP-BIN := ./bin/nomad-vector-logger.bin

LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_COMMIT_DATE := $(shell git show -s --format=%ci ${LAST_COMMIT})
Expand Down
110 changes: 109 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,110 @@
<a href="https://zerodha.tech"><img src="https://zerodha.tech/static/images/github-badge.svg" align="right" /></a>

# nomad-vector-logger
A daemon which continuously watches jobs running in a Nomad cluster and templates out a Vector configuration file which can be used to collect application logs enriched with Nomad metadata.

A daemon which continuously watches jobs running in a [Nomad](https://www.nomadproject.io/) cluster and templates out a [Vector](https://vector.dev/) configuration file which can be used to collect application logs enriched with Nomad metadata.

## Why

### The Problem

Currently, Nomad stores all application logs inside `$NOMAD_DATA_DIR/$NOMAD_ALLOC_DIR/logs/` directory. The limitation here is that these logs don't come with any metadata about the task/job/allocation. If you're running multiple applications on the same host, a log collecting agent cannot distinguish these logs. For `docker` driver, this is a solved problem by configuring the `log_driver` for the container.

However for `raw_exec` and `exec` this still seems to be an issue until [10219](https://github.com/hashicorp/nomad/issues/10219) is addressed.

### The Solution

- Nomad provides an [Events stream](https://github.com/mr-karan/nomad-events-sink) which continuosly streams events from the Nomad cluster.`nomad-vector-logger` is a Go daemon which subscribes to the Events stream on topic `Allocation` and listens for allocation updates.
- It then **templates** out a `vector` configuration to collect app logs from the allocation's log directory and enriches the configuration with metadata such as `NodeID`, `Namesapce`, `Task Name`, `JobName`, `AllocID` etc.
- `vector` is started with a [`--watch-config`](https://vector.dev/docs/administration/management/#reloading) flag which automatically live-reloads `vector` whenever config changes. A config change can happen whenever an allocation is _created/stopped/restarted_.

You can see a sample [config file](./examples/vector/nomad.toml) that is generated by this program. This can be used in addition with other vector config files to provide the config for rest of pipeline (additional transformations, sinks etc).

#### Before

Logs without any metdata on `/opt/nomad/data/alloc/$ALLOC_ID/alloc/logs`:

```
==> proxy.stdout.0 <==
192.168.29.76 - - [17/Jun/2022:11:01:42 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.81.0" "-"
```

#### After

This is an example JSON log collected from `nginx` task running with `raw_exec` task driver on Nomad, collected using `vector`:

```json
{
"file": "/opt/nomad/data/alloc/2cbf80ff-ef2a-e634-33ec-e097a6061001/alloc/logs/proxy.stdout.0",
"host": "pop-os",
"message": "192.168.29.76 - - [17/Jun/2022:11:00:03 +0000] \"GET / HTTP/1.1\" 200 27 \"-\" \"curl/7.81.0\" \"-\"",
"nomad_alloc_id": "2cbf80ff-ef2a-e634-33ec-e097a6061001",
"nomad_group_name": "nginx",
"nomad_job_name": "nginx",
"nomad_namespace": "default",
"nomad_node_name": "pop-os",
"nomad_task_name": "proxy",
"source_type": "file",
"timestamp": "2022-06-17T11:00:03.922205021Z"
}
```

## Deploy

- This is meant to run inside a Nomad cluster and should have proper ACL to listen to `Allocation:*` events.
- This is meant to be run as a `system` job. Each allocation of this program is responsible to configure `vector` for the allocations running on the host.
- Vector should be deployed externally, as a system job as well and should have access to the same directory that this program uses to generate the files.

You can choose one of the various deployment options:

### Binary

Grab the latest release from [Releases](https://github.com/mr-karan/nomad-vector-logger/releases).

To run:

```
$ ./nomad-vector-logger.bin --config config.toml
```

### Nomad

TODO

### Docker

TODO

## Configuration

Refer to [config.sample.toml](./config.sample.toml) for a list of configurable values.

### Environment Variables

All config variables can also be populated as env vairables by prefixing `NOMAD_VECTOR_LOGGER_` and replacing `.` with `__`.

For eg: `app.data_dir` becomes `NOMAD_VECTOR_LOGGER_app__data_dir`.

Nomad API client reads the following environment variables:

- `NOMAD_TOKEN`
- `NOMAD_ADDR`
- `NOMAD_REGION`
- `NOMAD_NAMESPACE`
- `NOMAD_HTTP_AUTH`
- `NOMAD_CACERT`
- `NOMAD_CAPATH`
- `NOMAD_CLIENT_CERT`
- `NOMAD_CLIENT_KEY`
- `NOMAD_TLS_SERVER_NAME`
- `NOMAD_SKIP_VERIFY`

You can read about them in detail [here](https://www.nomadproject.io/docs/runtime/environment).

## Contribution

Please feel free to open a new issue for bugs, feedback etc.

## LICENSE

[LICENSE](./LICENSE)
6 changes: 3 additions & 3 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
log = "debug" # `debug` for verbose logs. `info` otherwise.
data_dir = "./data/events" # Directory to store `index.json` file which is used to set index offset when listening for events.
commit_index_interval = "5s" # Interval at which `index.json` gets stored in `data_dir`.
nomad_data_dir = "/opt/nomad/data/alloc"
remove_alloc_delay = "5s"
nomad_data_dir = "/opt/nomad/data/alloc" # Nomad data directory where allocs are stored.
remove_alloc_delay = "5s" # If the alloc is completed or stopped, the allocation isn't removed immediately from vector's config. You can delay the removal of alloc by `n` duration to ensure that vector has finished collecting all logs till then.
vector_config_dir = "examples/vector/" # Path to the generated vector config file.
extra_templates_dir = "static/"
extra_templates_dir = "static/" # Extra templates that can be given. They will be rendered in `$vector_config_dir`. You can use variables mentioned in vector.tmpl if required.

[stream]
max_reconnect_attempts = 5 # Maximum reconnection attempts with Nomad Events API. After this limit is breached, program exits.
19 changes: 19 additions & 0 deletions examples/vector/nomad.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

[sources.source_nomad_alloc_2cbf80ff-ef2a-e634-33ec-e097a6061001_proxy]
type = "file"
include = [ "/opt/nomad/data/alloc/2cbf80ff-ef2a-e634-33ec-e097a6061001/alloc/logs/proxy*" ]
line_delimiter = "\n"
read_from = "beginning"

[transforms.transform_nomad_alloc_2cbf80ff-ef2a-e634-33ec-e097a6061001_proxy]
type = "remap"
inputs = ["source_nomad_alloc_2cbf80ff-ef2a-e634-33ec-e097a6061001_proxy"]
source = '''
# Store Nomad metadata.
.nomad_namespace = "default"
.nomad_node_name = "pop-os"
.nomad_job_name = "nginx"
.nomad_group_name = "nginx"
.nomad_task_name = "proxy"
.nomad_alloc_id = "2cbf80ff-ef2a-e634-33ec-e097a6061001"
'''
7 changes: 7 additions & 0 deletions examples/vector/sink.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[sinks.stdout_nomad]
type = "console"
inputs = ["transform_nomad_alloc*"]
target = "stdout"

[sinks.stdout_nomad.encoding]
codec = "json"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/mr-karan/nomad-alloc-logger
module github.com/mr-karan/nomad-vector-logger

go 1.18

require (
github.com/hashicorp/nomad/api v0.0.0-20211103234928-04cab9dbecd3
github.com/knadh/koanf v1.4.1
github.com/mr-karan/nomad-events-sink v0.1.1-0.20220617051258-624503512560
github.com/mr-karan/nomad-events-sink v0.2.0
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.21.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mr-karan/nomad-events-sink v0.1.1-0.20220617051258-624503512560 h1:u4oy5nydOTf0ppR5O2PErcEtEku2zbZT6IxgZVFv/jE=
github.com/mr-karan/nomad-events-sink v0.1.1-0.20220617051258-624503512560/go.mod h1:JgAAR7BTiHHlpQ6ktutjkZhdCvPk7585Tt4YyxVknDU=
github.com/mr-karan/nomad-events-sink v0.2.0 h1:0nyJDfSMCmA600BNerKqO0bYrumLDlxlb243rrBJRjU=
github.com/mr-karan/nomad-events-sink v0.2.0/go.mod h1:JgAAR7BTiHHlpQ6ktutjkZhdCvPk7585Tt4YyxVknDU=
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down

0 comments on commit 55d2516

Please sign in to comment.