This repository was archived by the owner on Nov 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathagent.go
758 lines (665 loc) · 21.5 KB
/
agent.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright (c) 2016 - 2019 Sqreen. All Rights Reserved.
// Please refer to our terms for more information:
// https://www.sqreen.io/terms.html
//sqreen:ignore
package internal
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/url"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/sqreen/go-agent/internal/actor"
"github.com/sqreen/go-agent/internal/app"
"github.com/sqreen/go-agent/internal/backend"
"github.com/sqreen/go-agent/internal/backend/api"
"github.com/sqreen/go-agent/internal/config"
"github.com/sqreen/go-agent/internal/metrics"
"github.com/sqreen/go-agent/internal/plog"
http_protection_types "github.com/sqreen/go-agent/internal/protection/http/types"
"github.com/sqreen/go-agent/internal/rule"
"github.com/sqreen/go-agent/internal/sqlib/sqerrors"
"github.com/sqreen/go-agent/internal/sqlib/sqsafe"
"github.com/sqreen/go-agent/internal/sqlib/sqsanitize"
"github.com/sqreen/go-agent/internal/sqlib/sqtime"
"github.com/sqreen/go-agent/internal/version"
"github.com/sqreen/go-libsqreen/waf"
"golang.org/x/xerrors"
)
func Start() {
agentInstance.start()
}
var agentInstance agentInstanceType
// agent instance holder type with synchronization
type agentInstanceType struct {
// The agent goroutine must be started once.
// It will asynchronously set the instance pointer.
startOnce sync.Once
// Instance pointer access R/W lock.
instanceAccessLock sync.RWMutex
instance *AgentType
}
func (instance *agentInstanceType) get() *AgentType {
instance.instanceAccessLock.RLock()
defer instance.instanceAccessLock.RUnlock()
return instance.instance
}
func (instance *agentInstanceType) set(agent *AgentType) {
instance.instanceAccessLock.Lock()
defer instance.instanceAccessLock.Unlock()
instance.instance = agent
}
// Start the agent when enabled and back-off restart it when unhandled errors
// or panics occur.
//
// The algorithm is based on multiple levels of try/catch equivalents called
// here "safe calls":
// - Level 1: a safe goroutine loop retrying the agent in case of unhandled
// error or panic.
// - Level 2: a safe call to the agent initialization.
// - Level 3: a safe call to the agent main loop.
// - Level 4, implicit here: internal agent errors that can be directly
// handled by the agent without having to stop it.
//
// Each level catches unhandled errors or panics of lower levels:
// - When the agent's main loop fails, it is caught and returned to the upper
// level to try to send it in a separate safe call, as the agent is no
// longer considered reliable.
// - If a panic occurs in this overall agent error handling, everything is
// considered unreliable and therefore aborted.
// - Otherwise, the overall agent initialization and main loop is re-executed
// with a backoff sleep.
// - If this backoff-retry loop fails, the outer-most safe goroutine captures
// it and silently return.
func (instance *agentInstanceType) start() {
instance.startOnce.Do(func() {
sqsafe.Go(func() error {
// Level 1
// Backoff-sleep loop to retry starting the agent
// To properly work, this level relies on:
// - the backoff.
// - the logger.
// - the correctness of sub-level error handling (ie. they don't panic).
// Any panics from these would stop the execution of this level.
backoff := sqtime.NewBackoff(time.Second, time.Hour, 2)
logger := plog.NewLogger(plog.Info, os.Stderr, nil)
for {
err := sqsafe.Call(func() error {
// Level 2
// Agent initialization and serve loop.
// To properly work, this level relies on:
// - the user configuration initialization.
// - the agent initialization.
// Any panics from these would stop the execution and would be returned
// to the outer level.
cfg, err := config.New(logger)
if err != nil {
logger.Error(sqerrors.Wrap(err, "agent disabled"))
return nil
}
agent := New(cfg)
if agent == nil {
return nil
} else {
instance.set(agent)
}
// Level 3 returns unhandled agent errors or panics
err = sqsafe.Call(agent.Serve)
if err == nil {
return nil
}
// Error ignored here
_ = sqsafe.Call(func() error {
// Send the error with a direct HTTP POST call without using the
// failed agent, but rather using the standard library's default
// HTTP client.
TrySendAppException(logger, cfg, err)
return nil
})
if panicErr, ok := err.(*sqsafe.PanicError); ok {
// agent.Serve() panic-ed: return the wrapped error in order to retry
return panicErr.Unwrap()
}
return err
})
// No error: regular exit case of the agent.
if err == nil {
return nil
}
if _, ok := err.(*sqsafe.PanicError); ok {
// Unexpected level 2 panic from its requirements: stop retrying as it
// is no longer reliable.
logger.Error(err)
return err
}
// An unhandled error was returned: retry
logger.Error(errors.Wrap(err, "unexpected agent error"))
d, max := backoff.Next()
if max {
logger.Error(errors.New("agent stopped: maximum agent retries reached"))
break
}
logger.Error(errors.Errorf("retrying to start the agent in %s", d))
time.Sleep(d)
}
return nil
}, nil)
})
}
type AgentType struct {
logger plog.DebugLevelLogger
eventMng *eventManager
metrics *metrics.Engine
staticMetrics staticMetrics
ctx context.Context
cancel context.CancelFunc
isDone chan struct{}
config *config.Config
appInfo *app.Info
client *backend.Client
actors *actor.Store
rules *rule.Engine
piiScrubber *sqsanitize.Scrubber
runningAccessLock sync.RWMutex
running bool
performanceBudget time.Duration
errLoggerChan chan error
}
type staticMetrics struct {
sdkUserLoginSuccess,
sdkUserLoginFailure,
sdkUser#,
allowedIP,
allowedPath,
callCounts *metrics.TimeHistogram
requestTime, sqreenTime, sqreenOverheadRate *metrics.PerfHistogram
}
// Error channel buffer length.
const errorChanBufferLength = 256
func New(cfg *config.Config) *AgentType {
errLoggerChan := make(chan error, errorChanBufferLength)
logger := plog.WithOptionalBackoff(plog.NewLogger(cfg.LogLevel(), os.Stderr, errLoggerChan))
agentVersion := version.Version()
logger.Infof("go agent v%s", agentVersion)
if cfg.Disabled() {
logger.Infof("agent disabled by the configuration")
return nil
}
metrics := metrics.NewEngine()
publicKey, err := rule.NewECDSAPublicKey(config.PublicKey)
if err != nil {
logger.Error(sqerrors.Wrap(err, "ecdsa public key"))
return nil
}
rulesEngine := rule.NewEngine(logger, nil, metrics, publicKey, perfHistogramUnit, perfHistogramBase, perfHistogramPeriod)
// Early health checking
if err := rulesEngine.Health(agentVersion); err != nil {
message := fmt.Sprintf("agent disabled: %s", err)
backend.SendAgentMessage(logger, cfg, message)
logger.Info(message)
return nil
}
if waf.Version() == nil {
message := "in-app waf disabled: cgo was disabled during the program compilation while required by the in-app waf"
backend.SendAgentMessage(logger, cfg, message)
logger.Info("agent: ", message)
}
// TODO: remove this SDK metrics period config when the corresponding js rule
// is supported
sdkMetricsPeriod := time.Duration(cfg.SDKMetricsPeriod()) * time.Second
logger.Debugf("agent: using sdk metrics store time period of %s", sdkMetricsPeriod)
piiScrubber := sqsanitize.NewScrubber(cfg.StripSensitiveKeyRegexp(), cfg.StripSensitiveValueRegexp(), config.ScrubberRedactedString)
client, err := backend.NewClient(cfg.BackendHTTPAPIBaseURL(), cfg.BackendHTTPAPIProxy(), logger)
if err != nil {
logger.Error(sqerrors.Wrap(err, "agent: could not create the backend client"))
return nil
}
sq, err := metrics.PerfHistogram("sq", perfHistogramUnit, perfHistogramBase, perfHistogramPeriod)
if err != nil {
logger.Error(sqerrors.Wrap(err, "`sq` performance histogram constructor error"))
}
req, err := metrics.PerfHistogram("req", perfHistogramUnit, perfHistogramBase, perfHistogramPeriod)
if err != nil {
logger.Error(sqerrors.Wrap(err, "`req` performance histogram constructor error"))
}
const (
sqreenOverheadRateBase = 1.3
sqreenOverheadRateUnit = 1.0
)
sqOverheadRate, err := metrics.PerfHistogram("pct", sqreenOverheadRateUnit, sqreenOverheadRateBase, perfHistogramPeriod)
if err != nil {
logger.Error(sqerrors.Wrap(err, "`pct` performance histogram constructor error"))
}
// AgentType graceful stopping using context cancellation.
ctx, cancel := context.WithCancel(context.Background())
return &AgentType{
logger: logger,
errLoggerChan: errLoggerChan,
isDone: make(chan struct{}),
metrics: metrics,
staticMetrics: staticMetrics{
sdkUserLoginSuccess: metrics.TimeHistogram("sdk-login-success", sdkMetricsPeriod, 60000),
sdkUserLoginFailure: metrics.TimeHistogram("sdk-login-fail", sdkMetricsPeriod, 60000),
sdkUser#: metrics.TimeHistogram("sdk-#", sdkMetricsPeriod, 60000),
allowedIP: metrics.TimeHistogram("whitelisted", sdkMetricsPeriod, 60000),
allowedPath: metrics.TimeHistogram("whitelisted_paths", sdkMetricsPeriod, 60000),
requestTime: req,
sqreenTime: sq,
sqreenOverheadRate: sqOverheadRate,
},
ctx: ctx,
cancel: cancel,
config: cfg,
appInfo: app.NewInfo(logger),
client: client,
actors: actor.NewStore(logger),
rules: rulesEngine,
piiScrubber: piiScrubber,
}
}
type AgentNotRunningError struct{}
func (AgentNotRunningError) Error() string {
return "agent not running"
}
func (a *AgentType) sendClosedHTTPProtectionContext(ctx http_protection_types.ClosedProtectionContextFace) {
if !a.isRunning() {
a.logger.Debug("agent not running: ignoring the closed http protection context")
return
}
// User events are not part of the request record
events := ctx.Events()
for _, event := range events.UserEvents {
a.addUserEvent(event)
}
start := ctx.Start()
duration := ctx.Duration()
finish := start.Add(duration)
// TODO: enforce start as the current time for the performance metrics?
req := float64(duration.Nanoseconds()) / float64(time.Millisecond)
if err := a.staticMetrics.requestTime.Add(req); err != nil {
a.logger.Error(sqerrors.Wrap(err, "could not add the request execution time"))
}
sq := float64(ctx.SqreenTime().Nanoseconds()) / float64(time.Millisecond)
if err := a.staticMetrics.sqreenTime.Add(sq); err != nil {
a.logger.Error(sqerrors.Wrap(err, "could not add sqreen's execution time"))
}
if overheadRate, err := overheadRate(req, sq); err == nil {
if err := a.staticMetrics.sqreenOverheadRate.Add(overheadRate); err != nil {
a.logger.Error(sqerrors.Wrap(err, "could not add sqreen overhead rate"))
}
}
event := newClosedHTTPRequestContextEvent(a.RulespackID(), start, finish, ctx.Response(), ctx.Request(), events)
if !event.shouldSend() {
return
}
a.eventMng.send(event)
}
func overheadRate(req float64, sq float64) (rate float64, err error) {
if req <= 0 || math.IsNaN(req) || math.IsInf(req, 0) {
return 0, sqerrors.Errorf("unexpected req value `%v`", req)
}
if sq <= 0 || math.IsNaN(sq) || math.IsInf(sq, 0) {
return 0, sqerrors.Errorf("unexpected sq value `%v`", sq)
}
if req < sq {
return 0, sqerrors.Errorf("unexpected req `%v` and sq `%v` values: req should be greater or equal to sq", req, sq)
}
if req == sq {
return 100, nil
}
userTime := req - sq
if userTime == 0 {
return 100, nil
}
return 100 * sq / userTime, nil
}
type withNotificationError struct {
error
}
func (e *withNotificationError) Unwrap() error { return e.error }
func (a *AgentType) Serve() error {
defer func() {
// Signal we are done
close(a.isDone)
a.logger.Info("agent stopped")
}()
token := a.config.BackendHTTPAPIToken()
appName := a.config.AppName()
ingestionUrl, _ := url.Parse(a.config.IngestionBackendHTTPAPIBaseURL())
appLoginRes, err := appLogin(a.ctx, a.logger, a.client, token, appName, a.appInfo, a.config.DisableSignalBackend(), ingestionUrl)
if err != nil {
if xerrors.Is(err, context.Canceled) {
a.logger.Debug(err)
return nil
}
if xerrors.As(err, &LoginError{}) {
a.logger.Info(err)
return nil
}
return err
}
// Load the rulepack side car
a.rules.SetRules(appLoginRes.PackID, appLoginRes.Rules)
// Load the actionpack side car
if err := a.actors.SetActions(appLoginRes.Actions); err != nil {
a.logger.Error(sqerrors.Wrap(err, "could not load the list of actions taken from the login response"))
}
// Create the command manager to process backend commands
commandMng := NewCommandManager(a, a.logger)
// Process commands that may have been received at login.
commandResults := commandMng.Do(appLoginRes.Commands)
heartbeat := time.Duration(appLoginRes.Features.HeartbeatDelay) * time.Second
if heartbeat == 0 {
heartbeat = config.BackendHTTPAPIDefaultHeartbeatDelay
}
batchSize := int(appLoginRes.Features.BatchSize)
if batchSize == 0 {
batchSize = config.EventBatchMaxEventsPerHeartbeat
}
maxStaleness := time.Duration(appLoginRes.Features.MaxStaleness) * time.Second
if maxStaleness == 0 {
maxStaleness = config.EventBatchMaxStaleness
}
// start the event manager's loop
queueLength := appLoginRes.Features.EventQueueLength
if queueLength == 0 {
queueLength = config.EventQueueDefaultLength
}
a.eventMng = newEventManager(a, queueLength, uint32(runtime.NumCPU()), batchSize, maxStaleness)
a.eventMng.Start()
a.setRunning(true)
defer a.setRunning(false)
a.logger.Debugf("agent: heartbeat ticker set to %s", heartbeat)
ticker := time.Tick(heartbeat)
a.logger.Info("agent: up and running")
// start the agent main loop
for {
select {
case <-ticker:
a.logger.Debug("heartbeat")
appBeatReq := api.AppBeatRequest{
Metrics: newMetricsAPIAdapter(a.logger, a.metrics.ReadyMetrics()),
CommandResults: commandResults,
}
appBeatRes, err := a.client.AppBeat(a.ctx, &appBeatReq)
if err != nil {
a.logger.Debug("heartbeat failed", err)
continue
}
// Perform commands that may be requested.
commandResults = commandMng.Do(appBeatRes.Commands)
case <-a.ctx.Done():
// The context was canceled because of a interrupt signal, logout and
// return.
err := a.client.AppLogout()
if err != nil {
a.logger.Debug("logout failed: ", err)
return nil
}
a.logger.Debug("successfully logged out")
return nil
case err := <-a.eventMng.errChan:
if err == nil {
continue
}
// Unexpected error from the event manager's loop as it should stop
// when the agent stops.
return err
case err := <-a.errLoggerChan:
// Logged errors.
if xerrors.As(err, &withNotificationError{}) {
t, ok := sqerrors.Timestamp(err)
if !ok {
t = time.Now()
}
_ = a.client.SendAgentMessage(a.ctx, t, err.Error(), nil)
}
a.addExceptionEvent(NewExceptionEvent(err, a.RulespackID()))
}
}
}
func (a *AgentType) EnableInstrumentation() (string, error) {
var id string
if a.rules.Count() == 0 {
var err error
id, err = a.ReloadRules()
if err != nil {
return "", err
}
} else {
id = a.RulespackID()
}
a.rules.Enable()
a.setRunning(true)
a.logger.Debug("agent: enabled")
return id, nil
}
// DisableInstrumentation disables the agent instrumentation, which includes for
// now the SDK.
func (a *AgentType) DisableInstrumentation() error {
a.setRunning(false)
a.rules.Disable()
err := a.actors.SetActions(nil)
a.logger.Debug("agent: disabled")
return err
}
func (a *AgentType) ReloadActions() error {
actions, err := a.client.ActionsPack()
if err != nil {
a.logger.Error(err)
return err
}
return a.actors.SetActions(actions.Actions)
}
func (a *AgentType) SendAppBundle() error {
deps, sig, _ := a.appInfo.Dependencies()
bundleDeps := make([]api.AppDependency, len(deps))
for i, dep := range deps {
bundleDeps[i] = api.AppDependency{
Name: dep.Path,
Version: dep.Version,
}
}
bundle := api.AppBundle{
Signature: sig,
Dependencies: bundleDeps,
}
return a.client.SendAppBundle(&bundle)
}
func (a *AgentType) SetCIDRIPPasslist(cidrs []string) error {
return a.actors.SetCIDRIPPasslist(cidrs)
}
func (a *AgentType) SetPathPasslist(paths []string) error {
a.actors.SetPathPasslist(paths)
return nil
}
func (a *AgentType) ReloadRules() (string, error) {
rulespack, err := a.client.RulesPack()
if err != nil {
a.logger.Error(err)
return "", err
}
// Insert local rules if any
if localRulesJSON := a.config.LocalRulesFile(); localRulesJSON != "" {
buf, err := ioutil.ReadFile(localRulesJSON)
if err == nil {
var localRules []api.Rule
err = json.Unmarshal(buf, &localRules)
if err == nil {
rulespack.Rules = append(rulespack.Rules, localRules...)
}
}
if err != nil {
a.logger.Error(sqerrors.Wrap(err, "config: could not read the local rules file"))
}
}
a.rules.SetRules(rulespack.PackID, rulespack.Rules)
return rulespack.PackID, nil
}
func (a *AgentType) SetPerformanceBudget(budget float64) error {
a.performanceBudget = time.Duration(budget * float64(time.Millisecond))
return nil
}
func (a *AgentType) gracefulStop() {
a.cancel()
<-a.isDone
}
type eventManager struct {
agent *AgentType
maxBatchLength int
eventsChan chan Event
maxStaleness time.Duration
stats *metrics.TimeHistogram
maxGoroutines uint32
nbGoroutines uint32
errChan chan error
}
func newEventManager(agent *AgentType, queueLength uint, maxGoroutines uint32, maxBatchLength int, maxStaleness time.Duration) *eventManager {
stats := agent.metrics.TimeHistogram("event_management", time.Minute, 10)
return &eventManager{
agent: agent,
eventsChan: make(chan Event, queueLength),
maxBatchLength: maxBatchLength,
maxStaleness: maxStaleness,
stats: stats,
maxGoroutines: maxGoroutines,
errChan: make(chan error, maxGoroutines),
}
}
func (m *eventManager) send(e Event) {
select {
case m.eventsChan <- e:
m.stats.Add("queue_ingress", 1)
default:
// The channel buffer is full - drop this event
m.scaleUp()
m.stats.Add("queue_dropped", 1)
}
}
func stopTimer(t *time.Timer) {
if !t.Stop() {
<-t.C
}
}
func (m *eventManager) ErrChan() <-chan error {
return m.errChan
}
func (m *eventManager) Start() {
atomic.StoreUint32(&m.nbGoroutines, 1)
m.start()
}
func (m *eventManager) start() {
sqsafe.Go(func() error {
m.loop()
return nil
}, m.errChan)
}
func (m *eventManager) scaleUp() {
if n := atomic.LoadUint32(&m.nbGoroutines); n == 0 || n == m.maxGoroutines {
return
}
atomic.AddUint32(&m.nbGoroutines, 1)
m.start()
m.stats.Add("scale", 1)
}
func (m *eventManager) loop() {
var (
// We can't create a stopped timer so we initialize it with a large value
// of 24 hours and stop it immediately. Calls to Reset() will correctly
// set the configured timer value.
stalenessTimer = time.NewTimer(24 * time.Hour)
stalenessChan <-chan time.Time
)
stopTimer(stalenessTimer)
defer stopTimer(stalenessTimer)
ctx := m.agent.ctx
client := m.agent.client
batch := make([]Event, 0, m.maxBatchLength)
req := &api.BatchRequest{
Batch: make([]api.BatchRequest_Event, 0, cap(batch)),
}
for {
select {
case <-ctx.Done():
return
case <-stalenessChan:
m.agent.logger.Debug("event batch data staleness reached")
m.sendBatch(ctx, client, batch, req)
batch = batch[0:0]
stalenessChan = nil
case event := <-m.eventsChan:
batch = append(batch, event)
m.stats.Add("queue_egress", 1)
m.agent.logger.Debugf("event `%T` added to the event batch", event)
batchLen := len(batch)
switch {
case batchLen == 1:
stalenessTimer.Reset(m.maxStaleness)
stalenessChan = stalenessTimer.C
m.agent.logger.Debug("batching events for ", m.maxStaleness)
case batchLen >= m.maxBatchLength:
// No more room in the batch
m.agent.logger.Debugf("sending the batch of %d events", batchLen)
m.sendBatch(ctx, client, batch, req)
batch = batch[0:0]
stalenessChan = nil
stopTimer(stalenessTimer)
}
}
}
}
func (m *eventManager) sendBatch(ctx context.Context, client *backend.Client, batch []Event, req *api.BatchRequest) {
defer func() {
req.Batch = req.Batch[0:0]
}()
for _, e := range batch {
var event api.BatchRequest_EventFace
switch actual := e.(type) {
case *closedHTTPRequestContextEvent:
cfg := m.agent.config
adapter := newProtectedHTTPRequestEventAPIAdapter(actual, cfg.StripHTTPReferer(), cfg.HTTPClientIPHeader())
event = api.RequestRecordEvent{api.NewRequestRecordFromFace(adapter)}
case *ExceptionEvent:
event = api.NewExceptionEventFromFace(actual)
}
// Scrub the value, along with the set of scrubbed string values.
if _, err := m.agent.piiScrubber.Scrub(event, nil); err != nil {
// Only log this unexpected error and keep the event that may have been
// partially scrubbed.
m.agent.logger.Error(errors.Wrap(err, "could not scrub the event"))
}
req.Batch = append(req.Batch, *api.NewBatchRequest_EventFromFace(event))
}
// Send the batch.
if err := client.Batch(ctx, req); err != nil {
m.stats.Add("backend_dropped", uint64(len(req.Batch)))
} else {
m.stats.Add("backend_egress", uint64(len(req.Batch)))
}
}
func (a *AgentType) setRunning(r bool) {
a.runningAccessLock.Lock()
defer a.runningAccessLock.Unlock()
a.running = r
}
func (a *AgentType) isRunning() bool {
a.runningAccessLock.RLock()
defer a.runningAccessLock.RUnlock()
return a.running
}
func (a *AgentType) addExceptionEvent(e *ExceptionEvent) {
if !a.isRunning() {
return
}
a.eventMng.send(e)
}
func (a *AgentType) RulespackID() string {
return a.rules.PackID()
}