Skip to content

Commit c206d9e

Browse files
committed
Use the same SourceType for labels and truncate stage
1 parent 8112b40 commit c206d9e

5 files changed

Lines changed: 68 additions & 64 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
// LabelsConfig is a set of labels to be extracted
2525
type LabelsConfig struct {
2626
Values map[string]*string `alloy:"values,attr"`
27-
SourceType string `alloy:"source_type,attr,optional"`
27+
SourceType SourceType `alloy:"source_type,attr,optional"`
2828
}
2929

3030
// validateLabelsConfig validates the Label stage configuration
@@ -34,11 +34,11 @@ func validateLabelsConfig(cfg *LabelsConfig) error {
3434
}
3535

3636
if cfg.SourceType == "" {
37-
cfg.SourceType = LabelsSourceExtractedMap
37+
cfg.SourceType = SourceTypeExtractedMap
3838
}
3939

4040
switch cfg.SourceType {
41-
case LabelsSourceExtractedMap, LabelsSourceStructuredMetadata:
41+
case SourceTypeExtractedMap, SourceTypeStructuredMetadata:
4242
default:
4343
return fmt.Errorf(ErrInvalidSourceType, cfg.SourceType)
4444
}
@@ -87,9 +87,9 @@ func (l *labelStage) Run(in chan Entry) chan Entry {
8787
defer close(out)
8888
for e := range in {
8989
switch l.cfg.SourceType {
90-
case LabelsSourceExtractedMap:
90+
case SourceTypeExtractedMap:
9191
l.addLabelFromExtractedMap(e.Labels, e.Extracted)
92-
case LabelsSourceStructuredMetadata:
92+
case SourceTypeStructuredMetadata:
9393
l.addLabelsFromStructuredMetadata(e.Labels, e.StructuredMetadata)
9494
}
9595
out <- e

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestLabels(t *testing.T) {
142142
},
143143
"label value is set from name for extracted": {
144144
config: LabelsConfig{
145-
SourceType: LabelsSourceExtractedMap,
145+
SourceType: SourceTypeExtractedMap,
146146
Values: map[string]*string{
147147
"l1": &lv1,
148148
"l2": nil,
@@ -157,7 +157,7 @@ func TestLabels(t *testing.T) {
157157
},
158158
"label value is set from name for structured_metadata": {
159159
config: LabelsConfig{
160-
SourceType: LabelsSourceStructuredMetadata,
160+
SourceType: SourceTypeStructuredMetadata,
161161
Values: map[string]*string{
162162
"l1": &lv1,
163163
"l2": nil,
@@ -215,7 +215,7 @@ func TestLabelsStage_Process(t *testing.T) {
215215
},
216216
"extract_success_structured_metadata": {
217217
LabelsConfig{
218-
SourceType: LabelsSourceStructuredMetadata,
218+
SourceType: SourceTypeStructuredMetadata,
219219
Values: map[string]*string{
220220
"testLabel": ptr("testStrucuturedMetadata"),
221221
}},
@@ -243,7 +243,7 @@ func TestLabelsStage_Process(t *testing.T) {
243243
},
244244
"different_source_name_structured_metadata": {
245245
LabelsConfig{
246-
SourceType: LabelsSourceStructuredMetadata,
246+
SourceType: SourceTypeStructuredMetadata,
247247
Values: map[string]*string{
248248
"testLabel": &sourceName,
249249
}},
@@ -267,7 +267,7 @@ func TestLabelsStage_Process(t *testing.T) {
267267
},
268268
"empty_structured_metadata": {
269269
LabelsConfig{
270-
SourceType: LabelsSourceStructuredMetadata,
270+
SourceType: SourceTypeStructuredMetadata,
271271
Values: map[string]*string{
272272
"testLabel": &sourceName,
273273
}},

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

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package stages
22

33
import (
4-
"encoding"
54
"errors"
6-
"fmt"
75
"maps"
86
"slices"
97
"strings"
@@ -36,46 +34,14 @@ type TruncateConfig struct {
3634
}
3735

3836
type RuleConfig struct {
39-
Limit units.Base2Bytes `alloy:"limit,attr"`
40-
Suffix string `alloy:"suffix,attr,optional"`
41-
Sources []string `alloy:"sources,attr,optional"`
42-
SourceType TruncateSourceType `alloy:"source_type,attr,optional"`
37+
Limit units.Base2Bytes `alloy:"limit,attr"`
38+
Suffix string `alloy:"suffix,attr,optional"`
39+
Sources []string `alloy:"sources,attr,optional"`
40+
SourceType SourceType `alloy:"source_type,attr,optional"`
4341

4442
effectiveLimit units.Base2Bytes
4543
}
4644

47-
var (
48-
_ encoding.TextMarshaler = TruncateSourceType("")
49-
_ encoding.TextUnmarshaler = (*TruncateSourceType)(nil)
50-
)
51-
52-
type TruncateSourceType string
53-
54-
// UnmarshalText implements encoding.TextUnmarshaler.
55-
func (t *TruncateSourceType) UnmarshalText(text []byte) error {
56-
str := string(text)
57-
switch str {
58-
case string(TruncateSourceLine), string(TruncateSourceLabel), string(TruncateSourceStructuredMetadata), string(TruncateSourceExtractedMap):
59-
*t = TruncateSourceType(str)
60-
default:
61-
return fmt.Errorf("unknown source_type: %s", str)
62-
}
63-
64-
return nil
65-
}
66-
67-
// MarshalText implements encoding.TextMarshaler.
68-
func (t TruncateSourceType) MarshalText() (text []byte, err error) {
69-
return []byte(t), nil
70-
}
71-
72-
const (
73-
TruncateSourceLine TruncateSourceType = "line"
74-
TruncateSourceLabel TruncateSourceType = "label"
75-
TruncateSourceStructuredMetadata TruncateSourceType = "structured_metadata"
76-
TruncateSourceExtractedMap TruncateSourceType = "extracted"
77-
)
78-
7945
// validateTruncateConfig validates the TruncateConfig for the truncateStage
8046
func validateTruncateConfig(cfg *TruncateConfig) error {
8147
if len(cfg.Rules) == 0 {
@@ -90,10 +56,10 @@ func validateTruncateConfig(cfg *TruncateConfig) error {
9056
}
9157

9258
if r.SourceType == "" {
93-
r.SourceType = TruncateSourceLine
59+
r.SourceType = SourceTypeLine
9460
}
9561

96-
if r.SourceType == TruncateSourceLine && len(r.Sources) > 0 {
62+
if r.SourceType == SourceTypeLine && len(r.Sources) > 0 {
9763
return errors.New(errSourcesForLine)
9864
}
9965

@@ -138,7 +104,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
138104
truncated := map[string]struct{}{}
139105
for _, r := range m.cfg.Rules {
140106
switch r.SourceType {
141-
case TruncateSourceLine:
107+
case SourceTypeLine:
142108
if len(e.Line) > int(r.effectiveLimit) {
143109
e.Line = e.Line[:r.effectiveLimit] + r.Suffix
144110
markTruncated(m.truncatedCount, truncated, truncateLineField)
@@ -147,7 +113,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
147113
level.Debug(m.logger).Log("msg", "line has been truncated", "limit", r.effectiveLimit, "truncated_line", e.Line)
148114
}
149115
}
150-
case TruncateSourceLabel:
116+
case SourceTypeLabel:
151117
if len(r.Sources) > 0 {
152118
for _, source := range r.Sources {
153119
name := model.LabelName(source)
@@ -160,7 +126,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
160126
m.tryTruncateLabel(r, e.Labels, k, v, truncated)
161127
}
162128
}
163-
case TruncateSourceStructuredMetadata:
129+
case SourceTypeStructuredMetadata:
164130
if len(r.Sources) > 0 {
165131
for i, v := range e.StructuredMetadata {
166132
if slices.Contains(r.Sources, v.Name) {
@@ -174,7 +140,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
174140
e.StructuredMetadata[i] = m.tryTruncateStructuredMetadata(r, v, truncated)
175141
}
176142
}
177-
case TruncateSourceExtractedMap:
143+
case SourceTypeExtractedMap:
178144
if len(r.Sources) > 0 {
179145
for _, source := range r.Sources {
180146
if v, ok := e.Extracted[source]; ok {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func Test_TruncateStage_Process(t *testing.T) {
8080
config: []*RuleConfig{
8181
{
8282
Limit: 15,
83-
SourceType: TruncateSourceLabel,
83+
SourceType: SourceTypeLabel,
8484
Suffix: "[truncated]",
8585
},
8686
},
@@ -96,7 +96,7 @@ func Test_TruncateStage_Process(t *testing.T) {
9696
config: []*RuleConfig{
9797
{
9898
Limit: 15,
99-
SourceType: TruncateSourceLabel,
99+
SourceType: SourceTypeLabel,
100100
Suffix: "[truncated]",
101101
Sources: []string{"app"},
102102
},
@@ -113,7 +113,7 @@ func Test_TruncateStage_Process(t *testing.T) {
113113
config: []*RuleConfig{
114114
{
115115
Limit: 15,
116-
SourceType: TruncateSourceStructuredMetadata,
116+
SourceType: SourceTypeStructuredMetadata,
117117
Suffix: "<trunc>",
118118
},
119119
},
@@ -132,7 +132,7 @@ func Test_TruncateStage_Process(t *testing.T) {
132132
config: []*RuleConfig{
133133
{
134134
Limit: 15,
135-
SourceType: TruncateSourceStructuredMetadata,
135+
SourceType: SourceTypeStructuredMetadata,
136136
Suffix: "<trunc>",
137137
Sources: []string{"meta1"},
138138
},
@@ -155,18 +155,18 @@ func Test_TruncateStage_Process(t *testing.T) {
155155
},
156156
{
157157
Limit: 15,
158-
SourceType: TruncateSourceLabel,
158+
SourceType: SourceTypeLabel,
159159
Suffix: "[truncated]",
160160
Sources: []string{"app"},
161161
},
162162
{
163163
Limit: 15,
164-
SourceType: TruncateSourceStructuredMetadata,
164+
SourceType: SourceTypeStructuredMetadata,
165165
Suffix: "<trunc>",
166166
},
167167
{
168168
Limit: 8,
169-
SourceType: TruncateSourceExtractedMap,
169+
SourceType: SourceTypeExtractedMap,
170170
Sources: []string{"field2"},
171171
},
172172
},
@@ -298,7 +298,7 @@ func Test_ValidateTruncateConfig(t *testing.T) {
298298
config: &TruncateConfig{
299299
Rules: []*RuleConfig{{
300300
Limit: 10,
301-
SourceType: TruncateSourceLabel,
301+
SourceType: SourceTypeLabel,
302302
Suffix: "...",
303303
}},
304304
},
@@ -309,7 +309,7 @@ func Test_ValidateTruncateConfig(t *testing.T) {
309309
config: &TruncateConfig{
310310
Rules: []*RuleConfig{{
311311
Limit: 10,
312-
SourceType: TruncateSourceStructuredMetadata,
312+
SourceType: SourceTypeStructuredMetadata,
313313
Suffix: "...",
314314
}},
315315
},
@@ -321,7 +321,7 @@ func Test_ValidateTruncateConfig(t *testing.T) {
321321
Rules: []*RuleConfig{
322322
{
323323
Limit: 10,
324-
SourceType: TruncateSourceLabel,
324+
SourceType: SourceTypeLabel,
325325
Sources: []string{"app"},
326326
Suffix: "...",
327327
},
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package stages
2+
3+
import (
4+
"encoding"
5+
"fmt"
6+
)
7+
8+
type SourceType string
9+
10+
const (
11+
SourceTypeLine SourceType = "line"
12+
SourceTypeLabel SourceType = "label"
13+
SourceTypeStructuredMetadata SourceType = "structured_metadata"
14+
SourceTypeExtractedMap SourceType = "extracted"
15+
)
16+
17+
var (
18+
_ encoding.TextMarshaler = SourceType("")
19+
_ encoding.TextUnmarshaler = (*SourceType)(nil)
20+
)
21+
22+
// UnmarshalText implements encoding.TextUnmarshaler.
23+
func (t *SourceType) UnmarshalText(text []byte) error {
24+
str := string(text)
25+
switch str {
26+
case string(SourceTypeLine), string(SourceTypeLabel), string(SourceTypeStructuredMetadata), string(SourceTypeExtractedMap):
27+
*t = SourceType(str)
28+
default:
29+
return fmt.Errorf("unknown source_type: %s", str)
30+
}
31+
32+
return nil
33+
}
34+
35+
// MarshalText implements encoding.TextMarshaler.
36+
func (t SourceType) MarshalText() (text []byte, err error) {
37+
return []byte(t), nil
38+
}

0 commit comments

Comments
 (0)