mirror of
https://github.com/ddworken/hishtory.git
synced 2024-12-02 05:03:12 +01:00
94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
|
|
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
|
|
"math"
|
|
"net/http"
|
|
pprofhttp "net/http/pprof"
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
)
|
|
|
|
func getMaximumNumberOfAllowedUsers() int {
|
|
maxNumUsersStr := os.Getenv("HISHTORY_MAX_NUM_USERS")
|
|
if maxNumUsersStr == "" {
|
|
return math.MaxInt
|
|
}
|
|
maxNumUsers, err := strconv.Atoi(maxNumUsersStr)
|
|
if err != nil {
|
|
return math.MaxInt
|
|
}
|
|
return maxNumUsers
|
|
}
|
|
|
|
func configureObservability(mux *httptrace.ServeMux, releaseVersion string) func() {
|
|
// Profiler
|
|
err := profiler.Start(
|
|
profiler.WithService("hishtory-api"),
|
|
profiler.WithVersion(releaseVersion),
|
|
profiler.WithAPIKey(os.Getenv("DD_API_KEY")),
|
|
profiler.WithUDS("/var/run/datadog/apm.socket"),
|
|
profiler.WithProfileTypes(
|
|
profiler.CPUProfile,
|
|
profiler.HeapProfile,
|
|
),
|
|
)
|
|
if err != nil {
|
|
fmt.Printf("Failed to start DataDog profiler: %v\n", err)
|
|
}
|
|
// Tracer
|
|
tracer.Start(
|
|
tracer.WithRuntimeMetrics(),
|
|
tracer.WithService("hishtory-api"),
|
|
tracer.WithUDS("/var/run/datadog/apm.socket"),
|
|
)
|
|
// TODO: should this be here?
|
|
defer tracer.Stop()
|
|
|
|
// Pprof
|
|
mux.HandleFunc("/debug/pprof/", pprofhttp.Index)
|
|
mux.HandleFunc("/debug/pprof/cmdline", pprofhttp.Cmdline)
|
|
mux.HandleFunc("/debug/pprof/profile", pprofhttp.Profile)
|
|
mux.HandleFunc("/debug/pprof/symbol", pprofhttp.Symbol)
|
|
mux.HandleFunc("/debug/pprof/trace", pprofhttp.Trace)
|
|
|
|
// Func to stop all of the above
|
|
return func() {
|
|
profiler.Stop()
|
|
tracer.Stop()
|
|
}
|
|
}
|
|
|
|
func getHishtoryVersion(r *http.Request) string {
|
|
return r.Header.Get("X-Hishtory-Version")
|
|
}
|
|
|
|
func getRemoteAddr(r *http.Request) string {
|
|
addr, ok := r.Header["X-Real-Ip"]
|
|
if !ok || len(addr) == 0 {
|
|
return "UnknownIp"
|
|
}
|
|
return addr[0]
|
|
}
|
|
|
|
func getRequiredQueryParam(r *http.Request, queryParam string) string {
|
|
val := r.URL.Query().Get(queryParam)
|
|
if val == "" {
|
|
panic(fmt.Sprintf("request to %s is missing required query param=%#v", r.URL, queryParam))
|
|
}
|
|
return val
|
|
}
|
|
|
|
func checkGormError(err error) {
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
panic(fmt.Sprintf("DB error at %s:%d: %v", filename, line, err))
|
|
}
|