Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoandrew4 committed Apr 29, 2024
2 parents a035316 + c5a341b commit 96da1b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ on:
- 'main'
tags:
- 'v*.*.*'
# release:
# types:
# - published

# permissions are needed if pushing to ghcr.io
permissions:
Expand Down Expand Up @@ -49,7 +46,9 @@ jobs:
nanoandrew4/ngcplogs
# Docker tags based on the following events/attributes
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=tag
type=semver,pattern={{raw}}
- name: Build all supported architectures
run: |
make push PLUGIN_TAG=${{ steps.docker_meta_tag_step.outputs.tags }}
make push PLUGIN_TAG=${{ steps.docker_meta_tag_step.outputs.version }} PLUGIN_NAME=${{ github.repository }}
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PLUGIN_NAME=nanoandrew4/ngcplogs
PLUGIN_NAME?=nanoandrew4/ngcplogs
PLUGIN_TAG?=latest
PLUGIN_DIR=./ngcplogs-plugin
PLUGIN_SUPPORTED_ARCHS=linux/amd64 linux/arm64
Expand Down Expand Up @@ -46,6 +46,7 @@ docker plugin rm -f ${PLUGIN_NAME}:${TAG_ARCH}-${PLUGIN_TAG} || true

@echo "### create new plugin ${PLUGIN_NAME}:${TAG_ARCH}-${PLUGIN_TAG} from ${PLUGIN_DIR}-${TAG_ARCH}"
docker plugin create ${PLUGIN_NAME}:${TAG_ARCH}-${PLUGIN_TAG} ${PLUGIN_DIR}-${TAG_ARCH}
docker plugin create ghcr.io/${PLUGIN_NAME}:${TAG_ARCH}-${PLUGIN_TAG} ${PLUGIN_DIR}-${TAG_ARCH}

endef

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ The following [log-opts](https://docs.docker.com/config/containers/logging/confi
| extract-json-message | true | Enables unmarshalling JSON messages and sending the jsonPayload as the unmarshalled map. Kind of the whole point of this plugin, but you can disable it so it behaves just like the `gcplogs` plugin if you wish |
| local-logging | false | Enables logging to a local file, so logs can be viewed with the `docker logs` command. If false, the command will show no output |
| extract-severity | true | Extracts the severity from JSON logs to set them for the log that will be sent to GCP. It will be removed from the jsonPayload section, since it is set at the root level. Currently the supported severity field names to extract are the following: `severity`, `level` |
| extract-msg | true | Extracts the msg field from JSON logs to set the message field GCP expects. It will be removed from the jsonPayload section, since it is set at the root level. Fields named msg are produced for example by the golang log/slog package. |
| exclude-timestamp | false | Excludes timestamp fields from the final jsonPayload, since docker sends its own nanosecond precision timestamp for each log. Currently it can remove fields with the following names: `timestamp`, `time`, `ts` |
| sleep-interval | 500 | Milliseconds to sleep when there are no logs to send before checking again. The higher the value, the lower the CPU usage will be |
| credentials-file | | Absolute path to the GCP credentials JSON file to use when authenticating (only necessary when running the plugin outside of GCP) |
Expand All @@ -142,4 +143,4 @@ If you want to build the plugin yourself, use the makefile with the following co
make all
```

See the Makefile for other build targets
See the Makefile for other build targets
19 changes: 18 additions & 1 deletion ngcplogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"encoding/json"
"errors"
"fmt"
"google.golang.org/api/option"
"sync"
"sync/atomic"
"time"

"google.golang.org/api/option"

"github.com/docker/docker/daemon/logger"

"cloud.google.com/go/compute/metadata"
Expand Down Expand Up @@ -77,6 +78,7 @@ type nGCPLogger struct {
extractJsonMessage bool
extractSeverity bool
excludeTimestamp bool
extractMsg bool
}

type dockerLogEntry struct {
Expand Down Expand Up @@ -201,6 +203,7 @@ func New(info logger.Info) (logger.Logger, error) {
extractJsonMessage: true,
extractSeverity: true,
excludeTimestamp: false,
extractMsg: true,
}

if info.Config[logCmdKey] == "true" {
Expand All @@ -216,6 +219,9 @@ func New(info logger.Info) (logger.Logger, error) {
if info.Config["exclude-timestamp"] == "true" {
l.excludeTimestamp = true
}
if info.Config["extract-msg"] == "false" {
l.extractMsg = false
}

if instanceResource != nil {
l.instance = instanceResource
Expand Down Expand Up @@ -269,6 +275,7 @@ func (l *nGCPLogger) Log(lMsg *logger.Message) error {
} else {
severity = l.extractSeverityFromPayload(m)
l.excludeTimestampFromPayload(m)
l.extractMsgFromPayload(m)
m["instance"] = l.instance
m["container"] = l.container
payload = m
Expand Down Expand Up @@ -328,6 +335,16 @@ func (l *nGCPLogger) excludeTimestampFromPayload(m map[string]any) {
}
}

func (l *nGCPLogger) extractMsgFromPayload(m map[string]any) {

if l.extractMsg {
if msg, exists := m["msg"]; exists {
m["message"] = msg
delete(m, "msg")
}
}
}

func (l *nGCPLogger) Close() error {
err := l.logger.Flush()
if err != nil {
Expand Down

0 comments on commit 96da1b7

Please sign in to comment.