Skip to content

Commit dc3e44a

Browse files
committed
Add Container name to the logs being fetched from Loki
Adding container name to help us figure out from which step a particular log is generated.
1 parent 37790fb commit dc3e44a

File tree

5 files changed

+9
-1
lines changed

5 files changed

+9
-1
lines changed

config/base/env/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ LOGGING_PLUGIN_PROXY_PATH=/api/logs/v1/application
4141
LOGGING_PLUGIN_API_URL=
4242
LOGGING_PLUGIN_TOKEN_PATH=/var/run/secrets/kubernetes.io/serviceaccount/token
4343
LOGGING_PLUGIN_NAMESPACE_KEY=kubernetes_namespace_name
44+
LOGGING_PLUGIN_CONTAINER_KEY=kubernetes.container_name
4445
LOGGING_PLUGIN_STATIC_LABELS='log_type=application'
4546
LOGGING_PLUGIN_CA_CERT=
4647
LOGGING_PLUGIN_QUERY_LIMIT=1700

docs/logging-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ These are the common configuration options for all third party logging APIs.
4343
- `LOGGING_PLUGIN_API_URL`: The URL of the third party logging API.
4444
- `LOGGING_PLUGIN_TOKEN_PATH`: The path to the file containing the API token. (optional)
4545
- `LOGGING_PLUGIN_NAMESPACE_KEY`: The key to use for the namespace filtering.
46+
- `LOGGING_PLUGIN_CONTAINER_KEY`: The key to use for adding container name to the logs. (optional)
4647
- `LOGGING_PLUGIN_STATIC_LABELS`: The static labels to use for the logs.
4748
- `LOGGING_PLUGIN_PROXY_PATH`: The path to the proxy to use for the third party logging API. (optional)
4849
- `LOGGING_PLUGIN_CA_CERT`: The CA certificate to use for the third party logging API. This should ideally be passed as environment variable in the deployment spec of the results-api pod. (optional)

pkg/api/server/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Config struct {
5555

5656
LOGGING_PLUGIN_API_URL string `mapstructure:"LOGGING_PLUGIN_API_URL"`
5757
LOGGING_PLUGIN_NAMESPACE_KEY string `mapstructure:"LOGGING_PLUGIN_NAMESPACE_KEY"`
58+
LOGGING_PLUGIN_CONTAINER_KEY string `mapstructure:"LOGGING_PLUGIN_CONTAINER_KEY"`
5859
LOGGING_PLUGIN_STATIC_LABELS string `mapstructure:"LOGGING_PLUGIN_STATIC_LABELS"`
5960
LOGGING_PLUGIN_TOKEN_PATH string `mapstructure:"LOGGING_PLUGIN_TOKEN_PATH"`
6061
LOGGING_PLUGIN_PROXY_PATH string `mapstructure:"LOGGING_PLUGIN_PROXY_PATH"`

pkg/api/server/v1alpha2/plugin/plugin_logs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ func getLokiLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record)
170170
for k, v := range s.queryParams {
171171
parameters.Add(k, v)
172172
}
173-
parameters.Add("query", `{ `+s.staticLabels+s.config.LOGGING_PLUGIN_NAMESPACE_KEY+`="`+parent+`" }|json uid="`+uidKey+`", message="message" |uid="`+rec.Name+`"| line_format "{{.message}}"`)
173+
query := `{ ` + s.staticLabels + s.config.LOGGING_PLUGIN_NAMESPACE_KEY + `="` + parent + `" }|json uid="` + uidKey + `", message="message" |uid="` + rec.Name + `"| line_format "{{.message}}"`
174+
if s.config.LOGGING_PLUGIN_CONTAINER_KEY != "" {
175+
query = `{ ` + s.staticLabels + s.config.LOGGING_PLUGIN_NAMESPACE_KEY + `="` + parent + `" }|json uid="` + uidKey + `", container="` + s.config.LOGGING_PLUGIN_CONTAINER_KEY + `", message="message" |uid="` + rec.Name + `"| line_format "container-{{.container}}: message={{.message}}"`
176+
}
177+
parameters.Add("query", query)
174178
parameters.Add("end", endTime)
175179
parameters.Add("start", startTime)
176180
parameters.Add("limit", strconv.FormatUint(uint64(s.queryLimit), 10))

test/e2e/loki_vector/loki-vector-api-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ data:
2424
LOGGING_PLUIN_TOKEN_PATH=/var/run/secrets/kubernetes.io/serviceaccount/token
2525
LOGGING_PLUGIN_PROXY_PATH=
2626
LOGGING_PLUGIN_NAMESPACE_KEY=kubernetes_namespace_name
27+
LOGGING_PLUGIN_CONTAINER_KEY=kubernetes.container_name
2728
LOGGING_PLUGIN_API_URL=http://loki.logging.svc.cluster.local:3100/loki/api/v1/query_range
2829
kind: ConfigMap
2930
metadata:

0 commit comments

Comments
 (0)