Skip to content
Permalink
Browse files

rename cmd/collector to cmd/profefe

  • Loading branch information
narqo committed Feb 16, 2019
1 parent 94e79c2 commit b47b4584bbdcb7c6b8331f593f56df0e5dee82e3
@@ -280,7 +280,7 @@ func (a *agent) collectAndSend(ctx context.Context) {
if err := a.collectProfile(ctx, ptype, &buf); err != nil {
a.logf("failed to collect profiles: %v", err)
} else if err := a.sendProfile(ctx, ptype, &buf); err != nil {
a.logf("failed to send profiles to collector: %v", err)
a.logf("failed to send profiles: %v", err)
}

buf.Reset()
@@ -9,6 +9,7 @@ import (

"github.com/profefe/profefe/pkg/logger"
"github.com/profefe/profefe/pkg/profile"
"github.com/profefe/profefe/version"
)

type APIHandler struct {
@@ -30,6 +31,7 @@ func (api *APIHandler) RegisterRoutes(mux *http.ServeMux) {
func (api *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error

// TODO(narqo): maybe use github.com/go-chi/chi
switch r.URL.Path {
case "/api/0/profiles":
err = api.handleGetProfiles(w, r)
@@ -42,6 +44,8 @@ func (api *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
err = api.handleGetProfile(w, r)
}
case "/api/0/version":
err = api.handleGetVersion(w, r)
default:
http.NotFound(w, r)
return
@@ -201,3 +205,18 @@ func readGetProfileRequest(in *profile.GetProfileRequest, r *http.Request) (err

return nil
}

func (api *APIHandler) handleGetVersion(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Content-Type", "application/json")

resp := struct {
Version string `json:"version"`
Commit string `json:"commit"`
BuildTime string `json:"build_time"`
}{
Version: version.Version,
Commit: version.Commit,
BuildTime: version.BuildTime,
}
return json.NewEncoder(w).Encode(resp)
}
File renamed without changes.
File renamed without changes.
@@ -12,26 +12,32 @@ import (
"syscall"
"time"

_ "github.com/lib/pq"
"github.com/profefe/profefe/agent"
"github.com/profefe/profefe/cmd/collector/api"
"github.com/profefe/profefe/cmd/collector/middleware"
"github.com/profefe/profefe/cmd/profefe/api"
"github.com/profefe/profefe/cmd/profefe/middleware"
"github.com/profefe/profefe/pkg/config"
"github.com/profefe/profefe/pkg/logger"
"github.com/profefe/profefe/pkg/profile"
pgstorage "github.com/profefe/profefe/pkg/storage/postgres"
"github.com/profefe/profefe/version"
"go.uber.org/zap"
)

const addr = ":10100"
_ "github.com/lib/pq"
pgstorage "github.com/profefe/profefe/pkg/storage/postgres"
)

func main() {
printVersion := flag.Bool("version", false, "print version and exit")

var conf config.Config
conf.RegisterFlags(flag.CommandLine)

flag.Parse()

if *printVersion {
fmt.Println(version.String())
os.Exit(1)
}

// TODO: init base logger
baseLogger := zap.NewExample()
defer baseLogger.Sync()
@@ -56,7 +62,7 @@ func run(ctx context.Context, log *logger.Logger, conf config.Config) error {
return fmt.Errorf("could not ping db: %v", err)
}

pgStorage, err := pgstorage.New(db)
pgStorage, err := pgstorage.New(log.With("svc", "db"), db)
if err != nil {
return fmt.Errorf("could not create new pg storage: %v", err)
}
@@ -75,21 +81,22 @@ func run(ctx context.Context, log *logger.Logger, conf config.Config) error {
handler = middleware.RecoveryHandler(handler)

server := http.Server{
Addr: addr,
Addr: conf.Addr,
Handler: handler,
}

errc := make(chan error, 1)
go func() {
log.Infow("server is running", "addr", addr)
log.Infow("server is running", "addr", server.Addr)
errc <- server.ListenAndServe()
}()

// start agent after server, because it sends to itself
// TODO: wait server to be ready to accept connections from the agent
agentLogger := log.With("svc", "profefe")
agent.Start(
"profefe_collector",
agent.WithCollector("http://"+addr),
agent.WithCollector("http://"+conf.Addr),
agent.WithCPUProfile(20*time.Second),
agent.WithLabels("az", "home", "host", "localhost", "version", version.Version, "commit", version.Commit, "build", version.BuildTime),
agent.WithLogger(func(format string, args ...interface{}) {
@@ -15,6 +15,10 @@ func init() {

const headerRequestID = "X-Request-Id"

func newRequestID() string {
return fmt.Sprintf("%08x%08x", rand.Uint32(), rand.Uint32())
}

type responseWriter struct {
http.ResponseWriter
statusCode int
@@ -34,6 +38,7 @@ func LoggingHandler(out io.Writer, next http.Handler) http.Handler {
rid := r.Header.Get(headerRequestID)
if rid == "" {
rid = newRequestID()
r.Header.Set(headerRequestID, rid)
}

next.ServeHTTP(resp, r)
@@ -65,7 +70,3 @@ func LoggingHandler(out io.Writer, next http.Handler) http.Handler {
}
return http.HandlerFunc(h)
}

func newRequestID() string {
return fmt.Sprintf("%08x%08x", rand.Uint32(), rand.Uint32())
}
File renamed without changes.
@@ -6,10 +6,13 @@ import (
)

type Config struct {
Addr string
Postgres PostgresConfig
}

func (conf *Config) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&conf.Addr, "addr", ":10100", "address to listen")

conf.Postgres.RegisterFlags(f)
}

@@ -53,7 +53,9 @@ func (labels *Labels) FromString(s string) (err error) {
*labels = append(*labels, Label{key, val})
}

sort.Sort(labels)
if len(*labels) != 0 {
sort.Sort(labels)
}

return nil
}
@@ -3,6 +3,7 @@ package profile
import (
"testing"

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

@@ -46,3 +47,27 @@ func TestLabels_FromString(t *testing.T) {
require.Equal(t, tt.labels, labels)
}
}

func TestLabels_String(t *testing.T) {
cases := []struct {
labels Labels
wantStr string
}{
{
nil,
"",
},
{
Labels{{"alabel", "value1"}, {"blabel", "value2"}},
"alabel=value1,blabel=value2",
},
{
Labels{{"alabel", "val=val"}},
"alabel=val=val",
},
}

for _, tt := range cases {
assert.Equal(t, tt.wantStr, tt.labels.String())
}
}
@@ -33,7 +33,7 @@ const (
WHERE l.func_name IS NULL
ON CONFLICT (func_name, file_name, line) DO NOTHING;`

sqlInsertSamples = `
sqlInsertSamplesTmpl = `
INSERT INTO %[1]s (build_id, token, created_at, received_at, locations, %[2]s)
SELECT s.build_id, s.token, s.created_at, s.received_at, locations, %[2]s
FROM (values ($1, $2, $3::timestamp, $4::timestamp)) AS s (build_id, token, created_at, received_at),
@@ -319,5 +319,5 @@ func createInsertSamples(table string, cols ...string) string {
for n, col := range cols {
tCols = append(tCols, fmt.Sprintf("values_all[%d] AS %s", n+1, col))
}
return fmt.Sprintf(sqlInsertSamples, table, strings.Join(cols, ","), strings.Join(tCols, ","))
return fmt.Sprintf(sqlInsertSamplesTmpl, table, strings.Join(cols, ","), strings.Join(tCols, ","))
}
@@ -1,7 +1,23 @@
package version

import (
"fmt"
"runtime"
)

var (
Version string
Commit string
BuildTime string
)

// String returns version details as pretty printed string.
func String() string {
return fmt.Sprintf(
"profefe version %s, commit %s (%s), go version %s",
Version,
Commit,
BuildTime,
runtime.Version(),
)
}

0 comments on commit b47b458

Please sign in to comment.
You can’t perform that action at this time.