🌱🏠😈 Common background service doing the heavy lifting for various user-facing greenhouse client applications
https://greenhouse.server.garden/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var telemetryClient *http.Client
|
||
|
|
||
|
func postTelemetry(tyype string, account string, daemonId string, content string) {
|
||
|
if account != "" {
|
||
|
account = fmt.Sprintf("&account=%s", account)
|
||
|
}
|
||
|
if daemonId != "" {
|
||
|
daemonId = fmt.Sprintf("&daemon=%s", daemonId)
|
||
|
}
|
||
|
response, err := getTelemetryClient().Post(
|
||
|
fmt.Sprintf("https://telemetry.greenhouse-alpha.server.garden/?type=%s&ip=auto%s%s", tyype, account, daemonId),
|
||
|
"text/plain",
|
||
|
bytes.NewBufferString(content),
|
||
|
)
|
||
|
if err != nil {
|
||
|
log.Printf("postTelemetry: %s\n", err)
|
||
|
} else if response.StatusCode > 299 {
|
||
|
responseString := "<read error>"
|
||
|
responseBytes, err := ioutil.ReadAll(response.Body)
|
||
|
if err == nil {
|
||
|
responseString = string(responseBytes)
|
||
|
}
|
||
|
log.Printf("postTelemetry: HTTP %d: %s\n", response.StatusCode, responseString)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getTelemetryClient() *http.Client {
|
||
|
if telemetryClient == nil {
|
||
|
telemetryClient = &http.Client{
|
||
|
Timeout: time.Second * 10,
|
||
|
}
|
||
|
}
|
||
|
return telemetryClient
|
||
|
}
|
||
|
|
||
|
func fatalfWithTelemetry(format string, args ...interface{}) {
|
||
|
formatWithoutStackTraces := strings.ReplaceAll(format, "%+v", "%s")
|
||
|
postTelemetry("daemon-crashed", daemonTelemetryAccount, daemonTelemetryId, fmt.Sprintf(formatWithoutStackTraces, args...))
|
||
|
log.Fatalf(format, args...)
|
||
|
}
|