Skip to content

Instantly share code, notes, and snippets.

@maplebed
Last active October 5, 2018 17:23
Show Gist options
  • Save maplebed/042daefc157747ad2ba2499fad30c68c to your computer and use it in GitHub Desktop.
Save maplebed/042daefc157747ad2ba2499fad30c68c to your computer and use it in GitHub Desktop.
An event source and an event sink to show libhoney's behavior in the face of a slow API server
package main
// source creates events as quickly as possible and sends them. Libhoney's queue
// quickly fills up and goroutine count stabalizes and it drops additional
// incoming events.
import (
"context"
"fmt"
"runtime"
"time"
_ "github.com/go-sql-driver/mysql"
beeline "github.com/honeycombio/beeline-go"
"github.com/honeycombio/hound/util"
)
func main() {
beeline.Init(beeline.Config{
APIHost: "http://localhost:8080/",
WriteKey: "stub-write-key",
Dataset: "goroutines",
ServiceName: "source",
})
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
for range ticker.C {
var mem runtime.MemStats
util.ReadMemStats(&mem)
fmt.Printf("%s, goroutines: %d memory: %d\n", time.Now(), runtime.NumGoroutine(), mem.Alloc)
}
}()
i := 0
for {
ctx, span := beeline.StartSpan(context.Background(), "myspan")
beeline.AddField(ctx, "counter", i)
i++
if i%100000 == 0 {
fmt.Printf("sent %dk events\n", i/1000)
}
span.Send()
}
}
package main
// sink accepts HTTP connections, holds on to them for 10 seconds, then replies.
import (
"fmt"
"io"
"math/rand"
"net/http"
"runtime"
"time"
)
func main() {
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
for range ticker.C {
fmt.Printf("%s, %d\n", time.Now(), runtime.NumGoroutine())
}
}()
http.HandleFunc("/", hold)
http.ListenAndServe(":8080", nil)
}
var i int
func hold(w http.ResponseWriter, req *http.Request) {
i++
if rand.Intn(100) == 0 {
fmt.Printf("got request %d\n", i)
}
w.WriteHeader(http.StatusAccepted)
io.WriteString(w, "got request\n")
time.Sleep(10 * time.Second)
}
package main
// source creates events as quickly as possible and sends them. Libhoney's queue
// quickly fills up and goroutine count stabalizes and it drops additional
// incoming events.
import (
"fmt"
"runtime"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/honeycombio/hound/util"
libhoney "github.com/honeycombio/libhoney-go"
)
func main() {
libhoney.Init(libhoney.Config{
APIHost: "http://localhost:8080/",
WriteKey: "stub-write-key",
Dataset: "goroutines",
// MaxConcurrentBatches: 2000,
// Logger: &libhoney.DefaultLogger{},
})
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
for range ticker.C {
var mem runtime.MemStats
util.ReadMemStats(&mem)
fmt.Printf("%s, goroutines: %d memory: %d\n", time.Now(), runtime.NumGoroutine(), mem.Alloc)
}
}()
i := 0
for {
ev := libhoney.NewEvent()
ev.AddField("counter", i)
ev.Send()
i++
if i%100000 == 0 {
fmt.Printf("sent %dk events\n", i/1000)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment