Skip to content

Commit

Permalink
refactor: add Filters in LoggingSpec (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 authored Nov 6, 2024
1 parent baffe02 commit f5e8c20
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 2 deletions.
7 changes: 7 additions & 0 deletions apis/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ type LoggingSpec struct {
// +kubebuilder:validation:Enum:={"info", "error", "warn", "debug"}
Level LoggingLevel `json:"level,omitempty"`

// Filters is the filters of the logging.
// User can use [EnvFilter](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html) to filter the logging.
// We can use `target[span{field=value}]=level` to filter the logging by target and span field.
// For example, "mito2=debug" will filter the logging of target `mito2` to `debug` level.
// +optional
Filters []string `json:"filters,omitempty"`

// LogsDir is the directory path of the logs.
// +optional
LogsDir string `json:"logsDir,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions config/crd/resources/greptime.io_greptimedbclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -5811,6 +5815,10 @@ spec:
type: string
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -8704,6 +8712,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -11630,6 +11642,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -11681,6 +11697,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -17457,6 +17477,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down
4 changes: 4 additions & 0 deletions config/crd/resources/greptime.io_greptimedbstandalones.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down
1 change: 1 addition & 0 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ _Appears in:_
| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `level` _[LoggingLevel](#logginglevel)_ | Level is the level of the logging. | | Enum: [info error warn debug] <br /> |
| `filters` _string array_ | Filters is the filters of the logging.<br />User can use [EnvFilter](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html) to filter the logging.<br />We can use `target[span\{field=value\}]=level` to filter the logging by target and span field.<br />For example, "mito2=debug" will filter the logging of target `mito2` to `debug` level. | | |
| `logsDir` _string_ | LogsDir is the directory path of the logs. | | |
| `persistentWithData` _boolean_ | PersistentWithData indicates whether to persist the log with the datanode data storage. It **ONLY** works for the datanode component.<br />If false, the log will be stored in ephemeral storage. | | |
| `onlyLogToStdout` _boolean_ | OnlyLogToStdout indicates whether to only log to stdout. If true, the log will not be stored in the storage even if the storage is configured. | | |
Expand Down
5 changes: 4 additions & 1 deletion examples/cluster/configure-logging/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
spec:
logging:
format: json
level: debug
level: info
logsDir: /data/greptimedb/logs
onlyLogToStdout: false
base:
Expand All @@ -19,3 +19,6 @@ spec:
- "etcd.etcd-cluster.svc.cluster.local:2379"
datanode:
replicas: 1
logging:
filters:
- mito2=debug
28 changes: 28 additions & 0 deletions manifests/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2907,6 +2907,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -5817,6 +5821,10 @@ spec:
type: string
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -8710,6 +8718,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -11636,6 +11648,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -11687,6 +11703,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -17463,6 +17483,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -20842,6 +20866,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down
28 changes: 28 additions & 0 deletions manifests/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2900,6 +2900,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -5810,6 +5814,10 @@ spec:
type: string
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -8703,6 +8711,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -11629,6 +11641,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -11680,6 +11696,10 @@ spec:
type: integer
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -17456,6 +17476,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down Expand Up @@ -20835,6 +20859,10 @@ spec:
type: object
logging:
properties:
filters:
items:
type: string
type: array
format:
enum:
- json
Expand Down
12 changes: 11 additions & 1 deletion pkg/dbconfig/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package dbconfig

import (
"encoding/base64"
"fmt"
"strconv"
"strings"

"k8s.io/klog/v2"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -176,7 +178,7 @@ func (c *LoggingConfig) ConfigureLogging(spec *v1alpha1.LoggingSpec) {
c.Dir = pointer.String(spec.LogsDir)
}

c.Level = pointer.String(string(spec.Level))
c.Level = pointer.String(c.levelWithFilters(string(spec.Level), spec.Filters))
c.LogFormat = pointer.String(string(spec.Format))

if spec.SlowQuery != nil {
Expand All @@ -193,3 +195,11 @@ func (c *LoggingConfig) ConfigureLogging(spec *v1alpha1.LoggingSpec) {
c.SlowQuerySampleRatio = pointer.Float64(ration)
}
}

// levelWithFilters returns the level with filters. For example, it will output "info,mito2=debug" if the level is "info" and the filters are ["mito2=debug"].
func (c *LoggingConfig) levelWithFilters(level string, filters []string) string {
if len(filters) > 0 {
return fmt.Sprintf("%s,%s", level, strings.Join(filters, ","))
}
return level
}

0 comments on commit f5e8c20

Please sign in to comment.