Skip to content

Instantly share code, notes, and snippets.

@simitt
Last active January 12, 2021 08:53
Show Gist options
  • Save simitt/7ece7a741ed8fb35c59447659f88cbba to your computer and use it in GitHub Desktop.
Save simitt/7ece7a741ed8fb35c59447659f88cbba to your computer and use it in GitHub Desktop.
Sampling testing
apm-server:
sampling:
tail:
enabled: true
interval: 20s
policies:
- trace.name: no_spans
sample_rate: 0.7
- trace.name: with_spans
sample_rate: 0.5
PUT /apm-sampled-traces
{
"mappings": {
"properties": {
"observer": {
"properties": {
"id": {
"type": "keyword"
}
}
},
"trace": {
"properties": {
"id": {
"type": "keyword"
}
}
}
}
}
}
package main
import (
"fmt"
"math"
"sync"
"time"
"go.elastic.co/apm"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
tracer, err := apm.NewTracer("goagent", "1.0.0")
if err != nil {
panic(err)
}
for i := 0; i < 1000; i++ {
withTransaction(tracer)
if math.Mod(float64(i), 50) == 0 {
fmt.Println("flush only transactions")
tracer.Flush(nil)
}
}
time.Sleep(10 * time.Second)
wg.Done()
fmt.Println(fmt.Sprintf("%+v", tracer.Stats()))
}()
wg.Add(1)
go func() {
tracer, err := apm.NewTracer("goagent", "1.0.0")
if err != nil {
panic(err)
}
for i := 0; i < 1000; i++ {
withSpan(tracer)
if math.Mod(float64(i), 50) == 0 {
fmt.Println("flush with spans")
tracer.Flush(nil)
}
}
time.Sleep(10 * time.Second)
wg.Done()
fmt.Println(fmt.Sprintf("%+v", tracer.Stats()))
}()
wg.Wait()
}
func withTransaction(tracer *apm.Tracer) {
tx := tracer.StartTransaction("no_spans", "request")
defer tx.End()
time.Sleep(5 * time.Millisecond)
tx.Result = "HTTP 2xx"
}
func withSpan(tracer *apm.Tracer) {
tx := tracer.StartTransaction("with_spans", "request")
defer tx.End()
span := tx.StartSpan("SELECT FROM foo", "db.mysql.query", nil)
defer span.End()
time.Sleep(5 * time.Millisecond)
tx.Result = "HTTP 2xx"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment