@@ -24,10 +24,12 @@ import (
2424 "encoding/pem"
2525 "errors"
2626 "fmt"
27+ "io"
2728 "net/http"
2829 "net/http/httptest"
2930 "os"
3031 "path/filepath"
32+ "strings"
3133 "testing"
3234 "time"
3335
@@ -285,6 +287,46 @@ func TestNewInstrumentation(t *testing.T) {
285287 assert .Equal (t , "Bearer secret" , auth )
286288}
287289
290+ func TestNewInstrumentationWithSampling (t * testing.T ) {
291+ runSampled := func (rate float32 ) {
292+ var events int
293+ s := httptest .NewTLSServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
294+ if r .URL .Path == "/intake/v2/events" {
295+ zr , _ := zlib .NewReader (r .Body )
296+ b , _ := io .ReadAll (zr )
297+ // Skip metadata and transaction keys, only count span.
298+ events = strings .Count (string (b ), "\n " ) - 2
299+ }
300+ w .WriteHeader (http .StatusOK )
301+ }))
302+ defer s .Close ()
303+ cfg := agentconfig .MustNewConfigFrom (map [string ]interface {}{
304+ "instrumentation" : map [string ]interface {}{
305+ "enabled" : true ,
306+ "hosts" : []string {s .URL },
307+ "tls" : map [string ]interface {}{
308+ "skipverify" : true ,
309+ },
310+ "samplingrate" : fmt .Sprintf ("%f" , rate ),
311+ },
312+ })
313+ i , err := newInstrumentation (cfg )
314+ require .NoError (t , err )
315+ tracer := i .Tracer ()
316+ tr := tracer .StartTransaction ("name" , "type" )
317+ tr .StartSpan ("span" , "type" , nil ).End ()
318+ tr .End ()
319+ tracer .Flush (nil )
320+ assert .Equal (t , int (rate ), events )
321+ }
322+ t .Run ("100% sampling" , func (t * testing.T ) {
323+ runSampled (1.0 )
324+ })
325+ t .Run ("0% sampling" , func (t * testing.T ) {
326+ runSampled (0.0 )
327+ })
328+ }
329+
288330func TestProcessMemoryLimit (t * testing.T ) {
289331 l := logp .NewLogger ("test" )
290332 const gb = 1 << 30
0 commit comments