6
6
package profiler
7
7
8
8
import (
9
+ "bytes"
9
10
"context"
10
11
"encoding/json"
11
12
"fmt"
12
13
"io"
14
+ "math/rand"
13
15
"net"
14
16
"net/http"
15
17
"net/http/httptest"
@@ -451,12 +453,22 @@ func TestExecutionTrace(t *testing.T) {
451
453
server := httptest .NewServer (& mockBackend {t : t , profiles : got })
452
454
defer server .Close ()
453
455
456
+ // cpuProfileRate is picked randomly so we can check for it in the trace
457
+ // data to reduce the chance that it occurs in the trace data for some other
458
+ // reduce. In theory we could use the entire int64 space, but when we do
459
+ // this the runtime can crash with the error shown below.
460
+ //
461
+ // runtime: kevent on fd 3 failed with 60
462
+ // fatal error: runtime: netpoll failed
463
+ cpuProfileRate := int (9999 + rand .Int63n (9999 ))
464
+
454
465
t .Setenv ("DD_PROFILING_EXECUTION_TRACE_ENABLED" , "true" )
455
466
t .Setenv ("DD_PROFILING_EXECUTION_TRACE_PERIOD" , "3s" )
456
467
err := Start (
457
468
WithAgentAddr (server .Listener .Addr ().String ()),
458
469
WithProfileTypes (CPUProfile ),
459
470
WithPeriod (1 * time .Second ),
471
+ CPUProfileRate (int (cpuProfileRate )),
460
472
)
461
473
require .NoError (t , err )
462
474
defer Stop ()
@@ -475,6 +487,7 @@ func TestExecutionTrace(t *testing.T) {
475
487
t .Log (m .event .Attachments , m .tags )
476
488
if contains (m .event .Attachments , "go.trace" ) && contains (m .tags , "go_execution_traced:yes" ) {
477
489
seenTraces ++
490
+ assertContainsCPUProfileRateLog (t , m .attachments ["go.trace" ], cpuProfileRate )
478
491
}
479
492
}
480
493
// With a trace frequency of 3 seconds and a profiling period of 1
@@ -485,6 +498,13 @@ func TestExecutionTrace(t *testing.T) {
485
498
}
486
499
}
487
500
501
+ // assertContainsCPUProfileRateLog checks for the presence of the log written by
502
+ // traceLogCPUProfileRate. It's a bit hacky, but probably good enough for now :).
503
+ func assertContainsCPUProfileRateLog (t * testing.T , traceData []byte , cpuProfileRate int ) {
504
+ assert .True (t , bytes .Contains (traceData , []byte ("cpuProfileRate" )))
505
+ assert .True (t , bytes .Contains (traceData , []byte (fmt .Sprintf ("%d" , cpuProfileRate ))))
506
+ }
507
+
488
508
// TestEndpointCounts verfies that the unit of work feature works end to end.
489
509
func TestEndpointCounts (t * testing.T ) {
490
510
for _ , enabled := range []bool {true , false } {
0 commit comments