Skip to content

Instantly share code, notes, and snippets.

@akvanhar
Created October 17, 2018 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akvanhar/641551f235cc11619ebb5b85fe148a22 to your computer and use it in GitHub Desktop.
Save akvanhar/641551f235cc11619ebb5b85fe148a22 to your computer and use it in GitHub Desktop.
OpenCensus Honeycomb Exporter example app
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"github.com/honeycombio/opencensus-exporter/honeycomb"
"go.opencensus.io/trace"
)
func main() {
exporter := honeycomb.NewExporter("YOUR-HONEYCOMB-WRITE-KEY", "YOUR-DATASET-NAME")
defer exporter.Close()
trace.RegisterExporter(exporter)
br := bufio.NewReader(os.Stdin)
sampleFraction := 0.5
trace.ApplyConfig(trace.Config{DefaultSampler: trace.ProbabilitySampler(sampleFraction)})
// If you use the Open Census Probability Sampler, be sure to pass that sampleFraction to the exporter
// so that Honeycomb can pick it up and make sure we handle your sampling properly.
// Note: The Probability Sampler uses a fraction, whereas Honeycomb uses an integer, which is the inverse of that fraction.
exporter.SampleFraction = sampleFraction
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess(br); err != nil {
if err == io.EOF {
return
}
log.Fatal(err)
}
}
}
func readEvaluateProcess(br *bufio.Reader) error {
ctx, span := trace.StartSpan(context.Background(), "repl")
defer span.End()
fmt.Printf("> ")
line, err := readLine(ctx, br)
if err != nil {
return err
}
out, err := processLine(ctx, line)
if err != nil {
return err
}
fmt.Printf("< %s\n\n", out)
return nil
}
func readLine(ctx context.Context, br *bufio.Reader) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "readLine")
defer span.End()
line, _, err := br.ReadLine()
if err != nil {
return nil, err
}
return line, err
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine(ctx context.Context, in []byte) (out []byte, err error) {
_, span := trace.StartSpan(ctx, "processLine")
defer span.End()
return bytes.ToUpper(in), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment