-
Notifications
You must be signed in to change notification settings - Fork 783
/
processor_failed.go
79 lines (65 loc) · 2.24 KB
/
processor_failed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package condition
import (
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeProcessorFailed] = TypeSpec{
constructor: NewProcessorFailed,
Summary: `
Returns true if a processing stage of a message has failed.`,
Description: `
This condition is useful for dropping failed messages or creating dead letter
queues, you can read more about these patterns [here](/docs/configuration/error_handling).`,
FieldSpecs: docs.FieldSpecs{
partFieldSpec,
},
}
}
//------------------------------------------------------------------------------
// ProcessorFailedConfig is a configuration struct containing fields for the
// processor_failed condition.
type ProcessorFailedConfig struct {
Part int `json:"part" yaml:"part"`
}
// NewProcessorFailedConfig returns a ProcessorFailedConfig with default values.
func NewProcessorFailedConfig() ProcessorFailedConfig {
return ProcessorFailedConfig{
Part: 0,
}
}
//------------------------------------------------------------------------------
// ProcessorFailed is a condition that checks whether processing steps have
// failed for a message.
type ProcessorFailed struct {
part int
mCount metrics.StatCounter
mTrue metrics.StatCounter
mFalse metrics.StatCounter
}
// NewProcessorFailed returns a ProcessorFailed condition.
func NewProcessorFailed(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
return &ProcessorFailed{
part: conf.ProcessorFailed.Part,
mCount: stats.GetCounter("count"),
mTrue: stats.GetCounter("true"),
mFalse: stats.GetCounter("false"),
}, nil
}
//------------------------------------------------------------------------------
// Check attempts to check a message part against a configured condition.
func (p *ProcessorFailed) Check(msg types.Message) bool {
p.mCount.Incr(1)
if l := len(msg.Get(p.part).Metadata().Get("benthos_processing_failed")); l > 0 {
p.mTrue.Incr(1)
return true
}
p.mFalse.Incr(1)
return false
}
//------------------------------------------------------------------------------