@@ -11,6 +11,8 @@ import (
11
11
"encoding/json"
12
12
"fmt"
13
13
"io/ioutil"
14
+ "math"
15
+ "net/url"
14
16
"os"
15
17
"runtime"
16
18
"sync"
@@ -194,7 +196,7 @@ type staticMetrics struct {
194
196
allowedIP ,
195
197
allowedPath ,
196
198
callCounts * metrics.TimeHistogram
197
- requestTime , sqreenTime , sqreenOverheadPercentage * metrics.PerfHistogram
199
+ requestTime , sqreenTime , sqreenOverheadRate * metrics.PerfHistogram
198
200
}
199
201
200
202
// Error channel buffer length.
@@ -256,7 +258,12 @@ func New(cfg *config.Config) *AgentType {
256
258
if err != nil {
257
259
logger .Error (sqerrors .Wrap (err , "`req` performance histogram constructor error" ))
258
260
}
259
- sqOverheadPercentage , err := metrics .PerfHistogram ("pct" , perfHistogramUnit , perfHistogramBase , perfHistogramPeriod )
261
+
262
+ const (
263
+ sqreenOverheadRateBase = 1.3
264
+ sqreenOverheadRateUnit = 1.0
265
+ )
266
+ sqOverheadRate , err := metrics .PerfHistogram ("pct" , sqreenOverheadRateUnit , sqreenOverheadRateBase , perfHistogramPeriod )
260
267
if err != nil {
261
268
logger .Error (sqerrors .Wrap (err , "`pct` performance histogram constructor error" ))
262
269
}
@@ -269,14 +276,14 @@ func New(cfg *config.Config) *AgentType {
269
276
isDone : make (chan struct {}),
270
277
metrics : metrics ,
271
278
staticMetrics : staticMetrics {
272
- sdkUserLoginSuccess : metrics .TimeHistogram ("sdk-login-success" , sdkMetricsPeriod , 60000 ),
273
- sdkUserLoginFailure : metrics .TimeHistogram ("sdk-login-fail" , sdkMetricsPeriod , 60000 ),
274
- sdkUserSignup : metrics .TimeHistogram ("sdk-signup" , sdkMetricsPeriod , 60000 ),
275
- allowedIP : metrics .TimeHistogram ("whitelisted" , sdkMetricsPeriod , 60000 ),
276
- allowedPath : metrics .TimeHistogram ("whitelisted_paths" , sdkMetricsPeriod , 60000 ),
277
- requestTime : req ,
278
- sqreenTime : sq ,
279
- sqreenOverheadPercentage : sqOverheadPercentage ,
279
+ sdkUserLoginSuccess : metrics .TimeHistogram ("sdk-login-success" , sdkMetricsPeriod , 60000 ),
280
+ sdkUserLoginFailure : metrics .TimeHistogram ("sdk-login-fail" , sdkMetricsPeriod , 60000 ),
281
+ sdkUserSignup : metrics .TimeHistogram ("sdk-signup" , sdkMetricsPeriod , 60000 ),
282
+ allowedIP : metrics .TimeHistogram ("whitelisted" , sdkMetricsPeriod , 60000 ),
283
+ allowedPath : metrics .TimeHistogram ("whitelisted_paths" , sdkMetricsPeriod , 60000 ),
284
+ requestTime : req ,
285
+ sqreenTime : sq ,
286
+ sqreenOverheadRate : sqOverheadRate ,
280
287
},
281
288
ctx : ctx ,
282
289
cancel : cancel ,
@@ -322,9 +329,10 @@ func (a *AgentType) sendClosedHTTPProtectionContext(ctx http_protection_types.Cl
322
329
a .logger .Error (sqerrors .Wrap (err , "could not add sqreen's execution time" ))
323
330
}
324
331
325
- sqOverhead := 100 * sq / (req - sq )
326
- if err := a .staticMetrics .sqreenOverheadPercentage .Add (sqOverhead ); err != nil {
327
- a .logger .Error (sqerrors .Wrap (err , "could not add sqreen's execution time" ))
332
+ if overheadRate , err := overheadRate (req , sq ); err == nil {
333
+ if err := a .staticMetrics .sqreenOverheadRate .Add (overheadRate ); err != nil {
334
+ a .logger .Error (sqerrors .Wrap (err , "could not add sqreen overhead rate" ))
335
+ }
328
336
}
329
337
330
338
event := newClosedHTTPRequestContextEvent (a .RulespackID (), start , finish , ctx .Response (), ctx .Request (), events )
@@ -334,6 +342,27 @@ func (a *AgentType) sendClosedHTTPProtectionContext(ctx http_protection_types.Cl
334
342
a .eventMng .send (event )
335
343
}
336
344
345
+ func overheadRate (req float64 , sq float64 ) (rate float64 , err error ) {
346
+ if req <= 0 || math .IsNaN (req ) || math .IsInf (req , 0 ) {
347
+ return 0 , sqerrors .Errorf ("unexpected req value `%v`" , req )
348
+ }
349
+ if sq <= 0 || math .IsNaN (sq ) || math .IsInf (sq , 0 ) {
350
+ return 0 , sqerrors .Errorf ("unexpected sq value `%v`" , sq )
351
+ }
352
+ if req < sq {
353
+ return 0 , sqerrors .Errorf ("unexpected req `%v` and sq `%v` values: req should be greater or equal to sq" , req , sq )
354
+ }
355
+ if req == sq {
356
+ return 100 , nil
357
+ }
358
+
359
+ userTime := req - sq
360
+ if userTime == 0 {
361
+ return 100 , nil
362
+ }
363
+ return 100 * sq / userTime , nil
364
+ }
365
+
337
366
type withNotificationError struct {
338
367
error
339
368
}
@@ -349,7 +378,8 @@ func (a *AgentType) Serve() error {
349
378
350
379
token := a .config .BackendHTTPAPIToken ()
351
380
appName := a .config .AppName ()
352
- appLoginRes , err := appLogin (a .ctx , a .logger , a .client , token , appName , a .appInfo , a .config .DisableSignalBackend ())
381
+ ingestionUrl , _ := url .Parse (a .config .IngestionBackendHTTPAPIBaseURL ())
382
+ appLoginRes , err := appLogin (a .ctx , a .logger , a .client , token , appName , a .appInfo , a .config .DisableSignalBackend (), ingestionUrl )
353
383
if err != nil {
354
384
if xerrors .Is (err , context .Canceled ) {
355
385
a .logger .Debug (err )
0 commit comments