11package stages
22
33import (
4- "encoding"
54 "errors"
6- "fmt"
75 "maps"
86 "slices"
97 "strings"
@@ -36,46 +34,14 @@ type TruncateConfig struct {
3634}
3735
3836type 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
8046func 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 {
0 commit comments