Skip to content
Permalink
Browse files

agent: support for threadcreate profiles

Following #45
  • Loading branch information
narqo committed Dec 4, 2019
1 parent 90e9b62 commit e84af4f9ea19bd3935db21962d834f1f333a7a8d
Showing with 32 additions and 20 deletions.
  1. +25 −20 agent/agent.go
  2. +6 −0 agent/options.go
  3. +1 −0 examples/server/main.go
@@ -47,12 +47,13 @@ type httpClient interface {
}

type Agent struct {
CPUProfile bool
CPUProfileDuration time.Duration
HeapProfile bool
BlockProfile bool
MutexProfile bool
GoroutineProfile bool
CPUProfile bool
CPUProfileDuration time.Duration
HeapProfile bool
BlockProfile bool
MutexProfile bool
GoroutineProfile bool
ThreadcreateProfile bool

service string
instanceID profile.InstanceID
@@ -140,27 +141,26 @@ func (a *Agent) collectProfile(ctx context.Context, ptype profile.ProfileType, b
if err != nil {
return fmt.Errorf("failed to write heap profile: %v", err)
}
case profile.BlockProfile:
return a.writeProfile("block", buf)
case profile.MutexProfile:
return a.writeProfile("mutex", buf)
case profile.GoroutineProfile:
return a.writeProfile("goroutine", buf)
case profile.BlockProfile,
profile.MutexProfile,
profile.GoroutineProfile,
profile.ThreadcreateProfile:

p := pprof.Lookup(ptype.String())
if p == nil {
return fmt.Errorf("unknown profile type %v", ptype)
}
err := p.WriteTo(buf, 0)
if err != nil {
return fmt.Errorf("failed to write %s profile: %v", ptype, err)
}
default:
return fmt.Errorf("unknown profile type %v", ptype)
}

return nil
}

func (a *Agent) writeProfile(name string, w io.Writer) error {
err := pprof.Lookup(name).WriteTo(w, 0)
if err != nil {
err = fmt.Errorf("failed to write %s profile: %v", name, err)
}
return err
}

func (a *Agent) sendProfile(ctx context.Context, ptype profile.ProfileType, buf *bytes.Buffer) error {
q := url.Values{}
q.Set("service", a.service)
@@ -288,6 +288,11 @@ func (a *Agent) nextProfileType(ptype profile.ProfileType) profile.ProfileType {
return ptype
}
case profile.GoroutineProfile:
ptype = profile.ThreadcreateProfile
if a.ThreadcreateProfile {
return ptype
}
case profile.ThreadcreateProfile:
ptype = profile.CPUProfile
if a.CPUProfile {
return ptype
@@ -39,6 +39,12 @@ func WithGoroutineProfile() Option {
}
}

func WithThreadcreateProfile() Option {
return func(a *Agent) {
a.ThreadcreateProfile = true
}
}

func WithLabels(args ...string) Option {
if len(args)%2 != 0 {
panic("agent.WithLabels: uneven number of arguments, expected key-value pairs")
@@ -28,6 +28,7 @@ func main() {
agent.WithBlockProfile(),
agent.WithMutexProfile(),
agent.WithGoroutineProfile(),
agent.WithThreadcreateProfile(),
agent.WithLogger(agentLogger),
agent.WithLabels(
"region", "europe-west3",

0 comments on commit e84af4f

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