Skip to content

Instantly share code, notes, and snippets.

@asachs
Created July 13, 2015 11:27
Show Gist options
  • Save asachs/5f5c4786528fdac47833 to your computer and use it in GitHub Desktop.
Save asachs/5f5c4786528fdac47833 to your computer and use it in GitHub Desktop.
GELF stack trace from golang
package main
import (
"fmt"
"github.com/probkiizokna/go-gelf"
"os"
"runtime"
"time"
"encoding/json"
)
type GELFStackTrace struct {
Version string `json:"version"`
Host string `json:"host"`
Timestamp int64 `json:"timestamp"`
// Facility string `json:"_facility"`
ShortMessage string `json:"short_message"`
Message string `json:"full_message"`
StackTraceLength int `json:"_stack_trace_length"`
StackTrace string `json:"_stack_trace"`
}
func NewGelfLog(trace []byte, count int) ([]byte, error) {
st := GELFStackTrace{}
st.Version = "1.1"
hn, err := os.Hostname()
if err != nil {
fmt.Errorf("Hostname problem: %v", err)
os.Exit(1)
}
st.Host = hn
st.ShortMessage = "Golang Stack Trace"
st.Timestamp = time.Now().Unix()
st.Message = string(trace[:count])
message, err := json.Marshal(st)
fmt.Printf("JSON: %s", string(message))
return message, err
}
func main() {
tcpLog, err := gelf.NewTcpClient("127.0.0.1", 12201 )
if err != nil {
fmt.Printf("Error: %+v", err)
}
trace := make([]byte, 1024*1024)
count := runtime.Stack(trace, true)
msg, _ := NewGelfLog(trace, count)
tcpLog.SendMessageData(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment