Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

profiler: add profiler package #598

Merged
merged 8 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package profiler

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -43,7 +42,6 @@ type config struct {
service, env string
hostname string
statsd StatsdClient
log *log.Logger
tags []string
types map[ProfileType]struct{}
period time.Duration
Expand All @@ -65,7 +63,6 @@ func defaultConfig() *config {
env: defaultEnv,
service: filepath.Base(os.Args[0]),
statsd: noopStatsdClient{},
log: log.New(os.Stderr, "Datadog Profiler: ", log.LstdFlags),
period: DefaultPeriod,
cpuDuration: DefaultDuration,
blockRate: DefaultBlockRate,
Expand Down Expand Up @@ -178,10 +175,3 @@ func WithStatsd(client StatsdClient) Option {
cfg.statsd = client
}
}

// WithLogger specifies a custom logger for logging errors.
func WithLogger(logger *log.Logger) Option {
return func(cfg *config) {
cfg.log = logger
}
}
11 changes: 5 additions & 6 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"runtime"
"sync"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)

// outChannelSize specifies the size of the profile output channel.
Expand Down Expand Up @@ -115,7 +117,7 @@ func (p *profiler) collect(ticker <-chan time.Time) {
for t := range p.cfg.types {
prof, err := p.runProfile(t)
if err != nil {
p.log("Error getting %s profile: %v; skipping.\n", t, err)
log.Error("Error getting %s profile: %v; skipping.\n", t, err)
p.cfg.statsd.Count("datadog.profiler.go.collect_error", 1, append(p.cfg.tags, fmt.Sprintf("profile_type:%v", t)), 1)
continue
}
Expand All @@ -128,9 +130,6 @@ func (p *profiler) collect(ticker <-chan time.Time) {
}
}

// log logs the given message using the configured logger.
func (p *profiler) log(fmt string, v ...interface{}) { p.cfg.log.Printf(fmt, v...) }

// enqueueUpload pushes a batch of profiles onto the queue to be uploaded. If there is no room, it will
// evict the oldest profile to make some. Typically a batch would be one of each enabled profile.
func (p *profiler) enqueueUpload(bat batch) {
Expand All @@ -143,7 +142,7 @@ func (p *profiler) enqueueUpload(bat batch) {
select {
case <-p.out:
p.cfg.statsd.Count("datadog.profiler.go.queue_full", 1, p.cfg.tags, 1)
p.log("Evicting one profile batch from the upload queue to make room.\n")
log.Warn("Evicting one profile batch from the upload queue to make room.\n")
default:
// queue is empty; contents likely got uploaded
}
Expand All @@ -156,7 +155,7 @@ func (p *profiler) send() {
defer close(p.exit)
for bat := range p.out {
if err := p.uploadFunc(bat); err != nil {
p.log("Failed to upload profile: %v\n", err)
log.Error("Failed to upload profile: %v\n", err)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion profiler/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"net/http"
"strings"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
dbenamydd marked this conversation as resolved.
Show resolved Hide resolved
)

// maxRetries specifies the maximum number of retries to have when an error occurs.
Expand Down Expand Up @@ -46,7 +48,7 @@ func (p *profiler) upload(bat batch) error {
if rerr, ok := err.(*retriableError); ok {
statsd.Count("datadog.profiler.go.upload_retry", 1, nil, 1)
wait := backoffDuration(i+1, p.cfg.cpuDuration)
p.log("Upload failed: %v. Trying again in %s...", rerr, wait)
log.Error("Uploading profile failed: %v. Trying again in %s...", rerr, wait)
dbenamydd marked this conversation as resolved.
Show resolved Hide resolved
time.Sleep(wait)
continue
}
Expand Down