Skip to content

Commit

Permalink
Merge pull request #11 from jum/extract_caddy2
Browse files Browse the repository at this point in the history
Extract caddy log information, second try
  • Loading branch information
nanoandrew4 authored May 27, 2024
2 parents 3e9745d + 40a037e commit 3ae225d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ The following [log-opts](https://docs.docker.com/config/containers/logging/confi
| 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. |
| extract-gcp | false | Extract trace, labels and source location fields if present and formatted for Google cloud logging. This is produced for example by the golang log/slog package with the slogdriver handler |
| extract-caddy | false | Extract trace and HTTP Request from caddy if present and format for Google cloud logging. |
| 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 Down
67 changes: 67 additions & 0 deletions ngcplogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"reflect"
"runtime"
"sync"
Expand Down Expand Up @@ -78,12 +80,14 @@ type nGCPLogger struct {
logger *logging.Logger
instance *instanceInfo
container *containerInfo
projectID string

extractJsonMessage bool
extractSeverity bool
excludeTimestamp bool
extractMsg bool
extractGcp bool
extractCaddy bool
}

type dockerLogEntry struct {
Expand Down Expand Up @@ -205,11 +209,13 @@ func New(info logger.Info) (logger.Logger, error) {
Created: info.ContainerCreated,
Metadata: extraAttributes,
},
projectID: project,
extractJsonMessage: true,
extractSeverity: true,
excludeTimestamp: false,
extractMsg: true,
extractGcp: false,
extractCaddy: false,
}

if info.Config[logCmdKey] == "true" {
Expand All @@ -231,6 +237,9 @@ func New(info logger.Info) (logger.Logger, error) {
if info.Config["extract-gcp"] == "true" {
l.extractGcp = true
}
if info.Config["extract-caddy"] == "true" {
l.extractCaddy = true
}

if instanceResource != nil {
l.instance = instanceResource
Expand Down Expand Up @@ -292,6 +301,7 @@ func (l *nGCPLogger) Log(lMsg *logger.Message) error {
m["instance"] = l.instance
m["container"] = l.container
l.extractGcpFromPayload(m, &entry)
l.extractCaddyFromPayload(m, &entry)
payload = m
}
} else {
Expand Down Expand Up @@ -403,6 +413,63 @@ func (l *nGCPLogger) extractGcpFromPayload(m map[string]any, entry *logging.Entr
}
}

func (l *nGCPLogger) extractCaddyFromPayload(m map[string]any, entry *logging.Entry) {

if l.extractCaddy {
if val, exists := m["request"]; exists {
hr := logging.HTTPRequest{
Request: &http.Request{
Header: make(http.Header),
},
}
v := assertOrLog[map[string]any](val)
hr.Request.Method = assertOrLog[string](v["method"])
_, isTLS := v["tls"]
var h = "http"
if isTLS {
h = "https"
}
hr.Request.URL = &url.URL{
Scheme: h,
Host: assertOrLog[string](v["host"]),
RawPath: assertOrLog[string](v["uri"]),
Path: assertOrLog[string](v["uri"]),
}
if t, ok := m["bytes_read"]; ok {
hr.RequestSize = int64(assertOrLog[float64](t))
}
if t, ok := m["status"]; ok {
hr.Status = int(assertOrLog[float64](t))
}
if t, ok := m["size"]; ok {
hr.ResponseSize = int64(assertOrLog[float64](t))
}
if t, ok := m["duration"]; ok {
hr.Latency = time.Duration(assertOrLog[float64](t) * float64(time.Second))
}
hr.Request.Proto = assertOrLog[string](v["proto"])
hr.RemoteIP = assertOrLog[string](v["remote_ip"]) + ":" + assertOrLog[string](v["remote_port"])

if t, ok := v["headers"]; ok {
headers := assertOrLog[map[string]any](t)
for h, v := range headers {
for _, s := range assertOrLog[[]any](v) {
hr.Request.Header.Add(h, assertOrLog[string](s))
}
}
}
entry.HTTPRequest = &hr
//Caddy request contains more data, don't
//delete.
//delete(m, "request")
}
if val, exists := m["traceID"]; exists {
entry.Trace = "projects/" + l.projectID + "/traces/" + val.(string)
delete(m, "traceID")
}
}
}

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

0 comments on commit 3ae225d

Please sign in to comment.