Skip to content

Commit

Permalink
Merge pull request #132 from instana/enable_profiler_via_env
Browse files Browse the repository at this point in the history
Enable AutoProfile™ via an env var
  • Loading branch information
Andrew Slotin authored Jun 26, 2020
2 parents 6b4e46b + bbff88b commit 59ff282
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ To learn more, see the [Events API](https://github.com/instana/go-sensor/blob/ma

AutoProfile™ generates and reports process profiles to Instana. Unlike development-time and on-demand profilers, where a user must manually initiate profiling, AutoProfile™ automatically schedules and continuously performs profiling appropriate for critical production environments.

### Activation from within the application code

To enable continuous profiling for your service provide `EnableAutoProfile: true` while initializing the sensor:

```go
func main() {
instana.InitSensor(&instana.Options{
EnableAutoProfile: true,
// ...other options
})

// ...
}
```

To temporarily turn AutoProfile™ on and off from your code, call `autoprofile.Enable()` and `autoprofile.Disable()`.

### Activation without code changes

To enable AutoProfile™ for an app without code changes, set `INSTANA_AUTO_PROFILE=true` env variable. Note that this value takes precedence and overrides any attempt to disable profiling from inside the application code.

## Examples

Following examples are included in the `example` folder:
Expand Down
7 changes: 7 additions & 0 deletions autoprofile/auto_profiler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package autoprofile

import (
"os"

"github.com/instana/go-sensor/autoprofile/internal"
"github.com/instana/go-sensor/autoprofile/internal/logger"
instalogger "github.com/instana/go-sensor/logger"
Expand Down Expand Up @@ -82,6 +84,11 @@ func Disable() {
return
}

if _, ok := os.LookupEnv("INSTANA_AUTO_PROFILE"); ok {
logger.Info("INSTANA_AUTO_PROFILE is set, ignoring the attempt to disable AutoProfile™")
return
}

profileRecorder.Stop()
cpuSamplerScheduler.Stop()
allocationSamplerScheduler.Stop()
Expand Down
38 changes: 21 additions & 17 deletions sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,27 @@ func InitSensor(options *Options) {

sensor = newSensor(options)

// enable auto-profiling
if options.EnableAutoProfile {
autoprofile.SetLogger(sensor.logger)
autoprofile.SetOptions(autoprofile.Options{
IncludeProfilerFrames: options.IncludeProfilerFrames,
MaxBufferedProfiles: options.MaxBufferedProfiles,
})

autoprofile.SetSendProfilesFunc(func(profiles []autoprofile.Profile) error {
if !sensor.agent.Ready() {
return errors.New("sender not ready")
}

sensor.logger.Debug("sending profiles to agent")

return sensor.agent.SendProfiles(profiles)
})
// configure auto-profiling
autoprofile.SetLogger(sensor.logger)
autoprofile.SetOptions(autoprofile.Options{
IncludeProfilerFrames: options.IncludeProfilerFrames,
MaxBufferedProfiles: options.MaxBufferedProfiles,
})

autoprofile.SetSendProfilesFunc(func(profiles []autoprofile.Profile) error {
if !sensor.agent.Ready() {
return errors.New("sender not ready")
}

sensor.logger.Debug("sending profiles to agent")

return sensor.agent.SendProfiles(profiles)
})

if _, ok := os.LookupEnv("INSTANA_AUTO_PROFILE"); ok || options.EnableAutoProfile {
if !options.EnableAutoProfile {
sensor.logger.Info("INSTANA_AUTO_PROFILE is set, activating AutoProfile™")
}

autoprofile.Enable()
}
Expand Down

0 comments on commit 59ff282

Please sign in to comment.