Skip to content

Commit 05f6469

Browse files
committed
feat(loki/stages): add JSON tags to all stage config structs
Adds `json:"..."` tags to every stage configuration struct in loki/process/stages and loki/process/metric. These tags are required to serialize stage configs to/from JSON for use in the PodLogs CRD (see #4738). Notable serialization choices: - `time.Duration` fields (OlderThan, MaxIdle) are tagged `json:"-"` because Go serialises them as nanosecond int64 values, which are not human-readable in Kubernetes YAML. Users can rely on the default value. - `units.Base2Bytes` already implements TextMarshaler/TextUnmarshaler and serialises as "5MiB", so it is exposed normally. No behaviour change — the alloy:"..." tags that drive existing parsing are untouched; json tags are purely additive. Refs: #4738 fix(loki/stages): mark GeoIPConfig.Source as required in JSON Remove omitempty from the Source field JSON tag so the field is never silently dropped when serialising a PodLogs pipeline stage config. Source is a required field (alloy:"source,attr") and omitting it in JSON would leave a nil pointer that causes the stage to do nothing. Refs: #4738 Signed-off-by: QuentinBisson <quentin@giantswarm.io>
1 parent 96d34e8 commit 05f6469

30 files changed

+128
-128
lines changed

internal/component/loki/process/metric/counters.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ const (
1818
// CounterConfig defines a counter metric whose value only goes up.
1919
type CounterConfig struct {
2020
// Shared fields
21-
Name string `alloy:"name,attr"`
22-
Description string `alloy:"description,attr,optional"`
23-
Source string `alloy:"source,attr,optional"`
24-
Prefix string `alloy:"prefix,attr,optional"`
25-
MaxIdle time.Duration `alloy:"max_idle_duration,attr,optional"`
26-
Value string `alloy:"value,attr,optional"`
21+
Name string `alloy:"name,attr" json:"name"`
22+
Description string `alloy:"description,attr,optional" json:"description,omitempty"`
23+
Source string `alloy:"source,attr,optional" json:"source,omitempty"`
24+
Prefix string `alloy:"prefix,attr,optional" json:"prefix,omitempty"`
25+
MaxIdle time.Duration `alloy:"max_idle_duration,attr,optional" json:"-"` // not supported in CRD: time.Duration is nanosecond int64 in JSON
26+
Value string `alloy:"value,attr,optional" json:"value,omitempty"`
2727

2828
// Counter-specific fields
29-
Action string `alloy:"action,attr"`
30-
MatchAll bool `alloy:"match_all,attr,optional"`
31-
CountEntryBytes bool `alloy:"count_entry_bytes,attr,optional"`
29+
Action string `alloy:"action,attr" json:"action"`
30+
MatchAll bool `alloy:"match_all,attr,optional" json:"matchAll,omitempty"`
31+
CountEntryBytes bool `alloy:"count_entry_bytes,attr,optional" json:"countEntryBytes,omitempty"`
3232
}
3333

3434
// DefaultCounterConfig sets the default for a Counter.

internal/component/loki/process/metric/gauges.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ var DefaultGaugeConfig = GaugeConfig{
2929
// GaugeConfig defines a gauge metric whose value can go up or down.
3030
type GaugeConfig struct {
3131
// Shared fields
32-
Name string `alloy:"name,attr"`
33-
Description string `alloy:"description,attr,optional"`
34-
Source string `alloy:"source,attr,optional"`
35-
Prefix string `alloy:"prefix,attr,optional"`
36-
MaxIdle time.Duration `alloy:"max_idle_duration,attr,optional"`
37-
Value string `alloy:"value,attr,optional"`
32+
Name string `alloy:"name,attr" json:"name"`
33+
Description string `alloy:"description,attr,optional" json:"description,omitempty"`
34+
Source string `alloy:"source,attr,optional" json:"source,omitempty"`
35+
Prefix string `alloy:"prefix,attr,optional" json:"prefix,omitempty"`
36+
MaxIdle time.Duration `alloy:"max_idle_duration,attr,optional" json:"-"` // not supported in CRD: time.Duration is nanosecond int64 in JSON
37+
Value string `alloy:"value,attr,optional" json:"value,omitempty"`
3838

3939
// Gauge-specific fields
40-
Action string `alloy:"action,attr"`
40+
Action string `alloy:"action,attr" json:"action"`
4141
}
4242

4343
// SetToDefault implements syntax.Defaulter.

internal/component/loki/process/metric/histograms.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ var DefaultHistogramConfig = HistogramConfig{
1818
// HistogramConfig defines a histogram metric whose values are bucketed.
1919
type HistogramConfig struct {
2020
// Shared fields
21-
Name string `alloy:"name,attr"`
22-
Description string `alloy:"description,attr,optional"`
23-
Source string `alloy:"source,attr,optional"`
24-
Prefix string `alloy:"prefix,attr,optional"`
25-
MaxIdle time.Duration `alloy:"max_idle_duration,attr,optional"`
26-
Value string `alloy:"value,attr,optional"`
21+
Name string `alloy:"name,attr" json:"name"`
22+
Description string `alloy:"description,attr,optional" json:"description,omitempty"`
23+
Source string `alloy:"source,attr,optional" json:"source,omitempty"`
24+
Prefix string `alloy:"prefix,attr,optional" json:"prefix,omitempty"`
25+
MaxIdle time.Duration `alloy:"max_idle_duration,attr,optional" json:"-"` // not supported in CRD: time.Duration is nanosecond int64 in JSON
26+
Value string `alloy:"value,attr,optional" json:"value,omitempty"`
2727

2828
// Histogram-specific fields
29-
Buckets []float64 `alloy:"buckets,attr"`
29+
Buckets []float64 `alloy:"buckets,attr" json:"buckets"`
3030
}
3131

3232
// SetToDefault implements syntax.Defaulter.

internal/component/loki/process/stages/cri.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
// CRIConfig is an empty struct that is used to enable a pre-defined pipeline
2020
// for decoding entries that are using the CRI logging format.
2121
type CRIConfig struct {
22-
MaxPartialLines int `alloy:"max_partial_lines,attr,optional"`
23-
MaxPartialLineSize uint64 `alloy:"max_partial_line_size,attr,optional"`
24-
MaxPartialLineSizeTruncate bool `alloy:"max_partial_line_size_truncate,attr,optional"`
22+
MaxPartialLines int `alloy:"max_partial_lines,attr,optional" json:"maxPartialLines,omitempty"`
23+
MaxPartialLineSize uint64 `alloy:"max_partial_line_size,attr,optional" json:"maxPartialLineSize,omitempty"`
24+
MaxPartialLineSizeTruncate bool `alloy:"max_partial_line_size_truncate,attr,optional" json:"maxPartialLineSizeTruncate,omitempty"`
2525
}
2626

2727
var (

internal/component/loki/process/stages/drop.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ var (
3131

3232
// DropConfig contains the configuration for a dropStage
3333
type DropConfig struct {
34-
DropReason string `alloy:"drop_counter_reason,attr,optional"`
35-
Source string `alloy:"source,attr,optional"`
36-
Value string `alloy:"value,attr,optional"`
37-
Separator string `alloy:"separator,attr,optional"`
38-
Expression string `alloy:"expression,attr,optional"`
39-
OlderThan time.Duration `alloy:"older_than,attr,optional"`
40-
LongerThan units.Base2Bytes `alloy:"longer_than,attr,optional"`
34+
DropReason string `alloy:"drop_counter_reason,attr,optional" json:"dropReason,omitempty"`
35+
Source string `alloy:"source,attr,optional" json:"source,omitempty"`
36+
Value string `alloy:"value,attr,optional" json:"value,omitempty"`
37+
Separator string `alloy:"separator,attr,optional" json:"separator,omitempty"`
38+
Expression string `alloy:"expression,attr,optional" json:"expression,omitempty"`
39+
OlderThan time.Duration `alloy:"older_than,attr,optional" json:"-"` // not supported in CRD: time.Duration is nanosecond int64 in JSON
40+
LongerThan units.Base2Bytes `alloy:"longer_than,attr,optional" json:"longerThan,omitempty"` // TextMarshaler: serializes as "5MiB"
4141
}
4242

4343
// validateDropConfig validates the DropConfig for the dropStage

internal/component/loki/process/stages/geoip.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ var fields = map[GeoIPFields]string{
5656

5757
// GeoIPConfig represents GeoIP stage config
5858
type GeoIPConfig struct {
59-
DB string `alloy:"db,attr"`
60-
Source *string `alloy:"source,attr"`
61-
DBType string `alloy:"db_type,attr,optional"`
62-
CustomLookups map[string]string `alloy:"custom_lookups,attr,optional"`
59+
DB string `alloy:"db,attr" json:"db"`
60+
Source *string `alloy:"source,attr" json:"source"`
61+
DBType string `alloy:"db_type,attr,optional" json:"dbType,omitempty"`
62+
CustomLookups map[string]string `alloy:"custom_lookups,attr,optional" json:"customLookups,omitempty"`
6363
}
6464

6565
func validateGeoIPConfig(c GeoIPConfig) (map[string]jmespath.JMESPath, error) {

internal/component/loki/process/stages/json.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const (
2222

2323
// JSONConfig represents a JSON Stage configuration
2424
type JSONConfig struct {
25-
Expressions map[string]string `alloy:"expressions,attr"`
26-
Source *string `alloy:"source,attr,optional"`
27-
DropMalformed bool `alloy:"drop_malformed,attr,optional"`
25+
Expressions map[string]string `alloy:"expressions,attr" json:"expressions"`
26+
Source *string `alloy:"source,attr,optional" json:"source,omitempty"`
27+
DropMalformed bool `alloy:"drop_malformed,attr,optional" json:"dropMalformed,omitempty"`
2828
}
2929

3030
// validateJSONConfig validates a json config and returns a map of necessary jmespath expressions.

internal/component/loki/process/stages/label_drop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var ErrEmptyLabelDropStageConfig = errors.New("labeldrop stage config cannot be
1212

1313
// LabelDropConfig contains the slice of labels to be dropped.
1414
type LabelDropConfig struct {
15-
Values []string `alloy:"values,attr"`
15+
Values []string `alloy:"values,attr" json:"values"`
1616
}
1717

1818
func newLabelDropStage(config LabelDropConfig) (Stage, error) {

internal/component/loki/process/stages/label_keep.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var ErrEmptyLabelAllowStageConfig = errors.New("labelallow stage config cannot b
1212

1313
// LabelAllowConfig contains the slice of labels to allow through.
1414
type LabelAllowConfig struct {
15-
Values []string `alloy:"values,attr"`
15+
Values []string `alloy:"values,attr" json:"values"`
1616
}
1717

1818
func newLabelAllowStage(config LabelAllowConfig) (Stage, error) {

internal/component/loki/process/stages/labels.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const (
2323

2424
// LabelsConfig is a set of labels to be extracted
2525
type LabelsConfig struct {
26-
Values map[string]*string `alloy:"values,attr"`
27-
SourceType SourceType `alloy:"source_type,attr,optional"`
26+
Values map[string]*string `alloy:"values,attr" json:"values"`
27+
SourceType SourceType `alloy:"source_type,attr,optional" json:"sourceType,omitempty"`
2828
}
2929

3030
// validateLabelsConfig validates the Label stage configuration

0 commit comments

Comments
 (0)