Skip to content

Instantly share code, notes, and snippets.

@akvanhar
Last active May 2, 2020 05:29
Show Gist options
  • Save akvanhar/a352925c1d59ada15ce46e194bad8d55 to your computer and use it in GitHub Desktop.
Save akvanhar/a352925c1d59ada15ce46e194bad8d55 to your computer and use it in GitHub Desktop.
REPL example app with Honeycomb events
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"time"
libhoney "github.com/honeycombio/libhoney-go"
)
func main() {
br := bufio.NewReader(os.Stdin)
libhoney.Init(libhoney.Config{
WriteKey: "HoneycombWriteKey",
Dataset: "HoneycombDatasetName",
})
defer libhoney.Close()
for {
evt := libhoney.NewEvent()
if err := readEvaluateProcess(evt, br); err != nil {
if err == io.EOF {
return
}
evt.AddField("error", err)
}
evt.Send()
}
}
func readEvaluateProcess(evt *libhoney.Event, br *bufio.Reader) error {
t1 := time.Now()
fmt.Printf("> ")
line, err := readLine(evt, br)
if err != nil {
evt.AddField("error", err)
return err
}
out, err := processLine(evt, line)
if err != nil {
evt.AddField("error", err)
return err
}
fmt.Printf("< %s\n\n", out)
t2 := time.Now()
duration := int64(t2.Sub(t1) / time.Millisecond)
evt.AddField("duration_ms", duration)
return nil
}
func readLine(evt *libhoney.Event, br *bufio.Reader) ([]byte, error) {
line, _, err := br.ReadLine()
evt.AddField("input", string(line))
if err != nil {
return nil, err
}
return line, nil
}
func processLine(evt *libhoney.Event, in []byte) (out string, err error) {
outputBytes := bytes.ToUpper(in)
output := string(outputBytes)
evt.AddField("output", output)
return output, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment