Skip to content

Commit 5941c14

Browse files
committed
Use the same SourceType for labels and truncate stage
1 parent cb4eec3 commit 5941c14

5 files changed

Lines changed: 46 additions & 45 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: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,25 @@ type TruncateConfig struct {
3636
}
3737

3838
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"`
39+
Limit units.Base2Bytes `alloy:"limit,attr"`
40+
Suffix string `alloy:"suffix,attr,optional"`
41+
Sources []string `alloy:"sources,attr,optional"`
42+
SourceType SourceType `alloy:"source_type,attr,optional"`
4343

4444
effectiveLimit units.Base2Bytes
4545
}
4646

4747
var (
48-
_ encoding.TextMarshaler = TruncateSourceType("")
49-
_ encoding.TextUnmarshaler = (*TruncateSourceType)(nil)
48+
_ encoding.TextMarshaler = SourceType("")
49+
_ encoding.TextUnmarshaler = (*SourceType)(nil)
5050
)
5151

52-
type TruncateSourceType string
53-
5452
// UnmarshalText implements encoding.TextUnmarshaler.
55-
func (t *TruncateSourceType) UnmarshalText(text []byte) error {
53+
func (t *SourceType) UnmarshalText(text []byte) error {
5654
str := string(text)
5755
switch str {
58-
case string(TruncateSourceLine), string(TruncateSourceLabel), string(TruncateSourceStructuredMetadata), string(TruncateSourceExtractedMap):
59-
*t = TruncateSourceType(str)
56+
case string(SourceTypeLine), string(SourceTypeLabel), string(SourceTypeStructuredMetadata), string(SourceTypeExtractedMap):
57+
*t = SourceType(str)
6058
default:
6159
return fmt.Errorf("unknown source_type: %s", str)
6260
}
@@ -65,17 +63,10 @@ func (t *TruncateSourceType) UnmarshalText(text []byte) error {
6563
}
6664

6765
// MarshalText implements encoding.TextMarshaler.
68-
func (t TruncateSourceType) MarshalText() (text []byte, err error) {
66+
func (t SourceType) MarshalText() (text []byte, err error) {
6967
return []byte(t), nil
7068
}
7169

72-
const (
73-
TruncateSourceLine TruncateSourceType = "line"
74-
TruncateSourceLabel TruncateSourceType = "label"
75-
TruncateSourceStructuredMetadata TruncateSourceType = "structured_metadata"
76-
TruncateSourceExtractedMap TruncateSourceType = "extracted"
77-
)
78-
7970
// validateTruncateConfig validates the TruncateConfig for the truncateStage
8071
func validateTruncateConfig(cfg *TruncateConfig) error {
8172
if len(cfg.Rules) == 0 {
@@ -90,10 +81,10 @@ func validateTruncateConfig(cfg *TruncateConfig) error {
9081
}
9182

9283
if r.SourceType == "" {
93-
r.SourceType = TruncateSourceLine
84+
r.SourceType = SourceTypeLine
9485
}
9586

96-
if r.SourceType == TruncateSourceLine && len(r.Sources) > 0 {
87+
if r.SourceType == SourceTypeLine && len(r.Sources) > 0 {
9788
return errors.New(errSourcesForLine)
9889
}
9990

@@ -138,7 +129,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
138129
truncated := map[string]struct{}{}
139130
for _, r := range m.cfg.Rules {
140131
switch r.SourceType {
141-
case TruncateSourceLine:
132+
case SourceTypeLine:
142133
if len(e.Line) > int(r.effectiveLimit) {
143134
e.Line = e.Line[:r.effectiveLimit] + r.Suffix
144135
markTruncated(m.truncatedCount, truncated, truncateLineField)
@@ -147,7 +138,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
147138
level.Debug(m.logger).Log("msg", "line has been truncated", "limit", r.effectiveLimit, "truncated_line", e.Line)
148139
}
149140
}
150-
case TruncateSourceLabel:
141+
case SourceTypeLabel:
151142
if len(r.Sources) > 0 {
152143
for _, source := range r.Sources {
153144
name := model.LabelName(source)
@@ -160,7 +151,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
160151
m.tryTruncateLabel(r, e.Labels, k, v, truncated)
161152
}
162153
}
163-
case TruncateSourceStructuredMetadata:
154+
case SourceTypeStructuredMetadata:
164155
if len(r.Sources) > 0 {
165156
for i, v := range e.StructuredMetadata {
166157
if slices.Contains(r.Sources, v.Name) {
@@ -174,7 +165,7 @@ func (m *truncateStage) Run(in chan Entry) chan Entry {
174165
e.StructuredMetadata[i] = m.tryTruncateStructuredMetadata(r, v, truncated)
175166
}
176167
}
177-
case TruncateSourceExtractedMap:
168+
case SourceTypeExtractedMap:
178169
if len(r.Sources) > 0 {
179170
for _, source := range r.Sources {
180171
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package stages
2+
3+
type SourceType string
4+
5+
const (
6+
SourceTypeLine SourceType = "line"
7+
SourceTypeLabel SourceType = "label"
8+
SourceTypeStructuredMetadata SourceType = "structured_metadata"
9+
SourceTypeExtractedMap SourceType = "extracted"
10+
)

0 commit comments

Comments
 (0)