Skip to content

Commit

Permalink
allow enabling a profile
Browse files Browse the repository at this point in the history
  • Loading branch information
mredolatti committed Nov 6, 2023
1 parent 2c2e488 commit 60c149b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
29 changes: 29 additions & 0 deletions cmd/splitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"flag"
"fmt"
"os"
"strconv"
"strings"

"github.com/splitio/go-toolkit/v5/logging"
"github.com/splitio/splitd/splitio"
"github.com/splitio/splitd/splitio/conf"
"github.com/splitio/splitd/splitio/link"
"github.com/splitio/splitd/splitio/sdk"
"github.com/splitio/splitd/splitio/util"

"github.com/splitio/splitd/splitio/provisional/profiler"
)

func main() {
Expand Down Expand Up @@ -47,6 +51,14 @@ func main() {
})
defer shutdown.Wait()

if p := setupProfiler(); p != nil {
go func() {
if err := p.ListenAndServe(); err != nil {
panic(err.Error())
}
}()
}

// Wait for connection to end (either gracefully of because of an error)
err = <-errc
exitOnErr("shutdown: ", err)
Expand All @@ -66,6 +78,23 @@ func handleFlags(cfg *conf.Config) {
}
}

func setupProfiler() *profiler.HTTPProfileInterface {
switch strings.ToLower(os.Getenv("SPLITD_PROFILING")) {
case "on", "true", "enabled", "1":
host := "localhost"
if h := os.Getenv("SPLITD_PROFILING_HOSTNAME"); h != "" {
host = h
}
port := 8888
if p, err := strconv.Atoi(os.Getenv("SPLITD_PROFILING_PORT")); err != nil {
port = p
}
return profiler.New(host, port)
}

return nil
}

func exitOnErr(ctxStr string, err error) {
if err != nil {
fmt.Printf("%s: startup error: %s\n", ctxStr, err.Error())
Expand Down
25 changes: 25 additions & 0 deletions cmd/splitd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSetupprofiler(t *testing.T) {

assert.Nil(t, setupProfiler())

os.Setenv("SPLITD_PROFILING", "true")
assert.NotNil(t, setupProfiler())
os.Setenv("SPLITD_PROFILING", "1")
assert.NotNil(t, setupProfiler())
os.Setenv("SPLITD_PROFILING", "on")
assert.NotNil(t, setupProfiler())
os.Setenv("SPLITD_PROFILING", "On")
assert.NotNil(t, setupProfiler())
os.Setenv("SPLITD_PROFILING", "EnabLed")
assert.NotNil(t, setupProfiler())

}
2 changes: 1 addition & 1 deletion splitio/commitsha.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package splitio

const CommitSHA = "1472745"
const CommitSHA = "2c2e488"
36 changes: 36 additions & 0 deletions splitio/provisional/profiler/profiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package profiler

import (
"fmt"
"net/http"
"net/http/pprof"
)

func init() {
http.DefaultServeMux = http.NewServeMux()
}

type HTTPProfileInterface struct {
server http.Server
//server http.ServeMux
}

func New(host string, port int) *HTTPProfileInterface {
mux := http.NewServeMux()

mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
return &HTTPProfileInterface{
http.Server{
Addr: fmt.Sprintf("%s:%d", host, port),
Handler: mux,
},
}
}

func (h *HTTPProfileInterface) ListenAndServe() error {
return h.server.ListenAndServe()
}

0 comments on commit 60c149b

Please sign in to comment.