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

feat(plugins.nvidia_smi): Implement Probe() #16305

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ func (a *Agent) startInputs(

return nil, fmt.Errorf("starting input %s: %w", input.LogName(), err)
}
if err := input.Probe(); err != nil {
// Probe failures are not fatal to Telegraf itself, so simply skip
// adding the input.
log.Printf("I! [agent] Failed to probe %s, shutting down plugin: %s", input.LogName(), err)
continue
}
unit.inputs = append(unit.inputs, input)
}

Expand Down
51 changes: 51 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -257,3 +258,53 @@ func collect(ctx context.Context, a *Agent, wait time.Duration) ([]telegraf.Metr
}
return received, nil
}

type mockProbingInput struct {
probeReturn error
}

func (m *mockProbingInput) SampleConfig() string {
return ""
}

Comment on lines +261 to +269
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need the test here as the testing in running_input_test.go already covers the case...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be good to test that the agent is actually calling Probe but if you prefer not to have this test, I'll remove it.

func (m *mockProbingInput) Gather(_ telegraf.Accumulator) error {
return nil
}

func (m *mockProbingInput) Probe() error {
return m.probeReturn
}

func TestAgentstartInputsProbing(t *testing.T) {
for _, tt := range []struct {
name string
probeReturnError error
expectedInputLength int
}{
{
name: "probe failed",
probeReturnError: errors.New("probe failure"),
expectedInputLength: 0,
},
{
name: "probe succeeded",
expectedInputLength: 1,
},
} {
t.Run(tt.name, func(t *testing.T) {
a := NewAgent(config.NewConfig())
require.Empty(t, a.Config.Outputs)

inputs := []*models.RunningInput{
models.NewRunningInput(&mockProbingInput{
probeReturn: tt.probeReturnError,
}, &models.InputConfig{
StartupErrorBehavior: "probe",
}),
}
inputUnit, err := a.startInputs(make(chan<- telegraf.Metric), inputs)
require.NoError(t, err)
require.Len(t, inputUnit.inputs, tt.expectedInputLength)
})
}
}
14 changes: 12 additions & 2 deletions models/running_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *RunningInput) LogName() string {

func (r *RunningInput) Init() error {
switch r.Config.StartupErrorBehavior {
case "", "error", "retry", "ignore":
case "", "error", "retry", "ignore", "probe":
default:
return fmt.Errorf("invalid 'startup_error_behavior' setting %q", r.Config.StartupErrorBehavior)
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (r *RunningInput) Start(acc telegraf.Accumulator) error {
}
r.log.Infof("Startup failed: %v; retrying...", err)
return nil
case "ignore":
case "ignore", "probe":
return &internal.FatalError{Err: serr}
default:
r.log.Errorf("Invalid 'startup_error_behavior' setting %q", r.Config.StartupErrorBehavior)
Expand All @@ -170,6 +170,16 @@ func (r *RunningInput) Start(acc telegraf.Accumulator) error {
return err
}

func (r *RunningInput) Probe() error {
p, ok := r.Input.(telegraf.ProbePlugin)
if !ok || r.Config.StartupErrorBehavior != "probe" {
r.log.Debug("Not probing plugin")
return nil
}
r.log.Debug("Probing plugin")
return p.Probe()
}

Comment on lines +173 to +182
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the debug messages here as they don't add value for the user!

Suggested change
func (r *RunningInput) Probe() error {
p, ok := r.Input.(telegraf.ProbePlugin)
if !ok || r.Config.StartupErrorBehavior != "probe" {
r.log.Debug("Not probing plugin")
return nil
}
r.log.Debug("Probing plugin")
return p.Probe()
}
func (r *RunningInput) Probe() error {
p, ok := r.Input.(telegraf.ProbePlugin)
if !ok || r.Config.StartupErrorBehavior != "probe" {
return nil
}
return p.Probe()
}

func (r *RunningInput) Stop() {
if plugin, ok := r.Input.(telegraf.ServiceInput); ok {
plugin.Stop()
Expand Down
67 changes: 67 additions & 0 deletions models/running_input_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package models

import (
"errors"
"testing"
"time"

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

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -487,6 +489,71 @@ func TestRunningInputMakeMetricWithGatherEndTimeSource(t *testing.T) {
require.Equal(t, expected, actual)
}

func TestRunningInputProbing(t *testing.T) {
probeErr := errors.New("probing error")
for _, tt := range []struct {
name string
input telegraf.Input
startupErrorBehavior string
expectedError bool
}{
{
name: "non-probing plugin with probe value set",
input: &mockInput{},
startupErrorBehavior: "probe",
expectedError: false,
},
{
name: "non-probing plugin with probe value not set",
input: &mockInput{},
startupErrorBehavior: "ignore",
expectedError: false,
},
{
name: "probing plugin with probe value set",
input: &mockProbingInput{probeErr},
startupErrorBehavior: "probe",
expectedError: true,
},
{
name: "probing plugin with probe value not set",
input: &mockProbingInput{probeErr},
startupErrorBehavior: "ignore",
expectedError: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
ri := NewRunningInput(tt.input, &InputConfig{
Name: "TestRunningInput",
StartupErrorBehavior: tt.startupErrorBehavior,
})
ri.log = testutil.Logger{}
err := ri.Probe()
if tt.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

type mockProbingInput struct {
probeReturn error
}

func (m *mockProbingInput) SampleConfig() string {
return ""
}

func (m *mockProbingInput) Gather(_ telegraf.Accumulator) error {
return nil
}

func (m *mockProbingInput) Probe() error {
return m.probeReturn
}

type mockInput struct{}

func (t *mockInput) SampleConfig() string {
Expand Down
6 changes: 6 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ type StatefulPlugin interface {
// initialization (after Init() function).
SetState(state interface{}) error
}

// ProbePlugin is an interface that all input/output plugins need to
// implement in order to support the `probe` value of `startup_error_behavior`.
type ProbePlugin interface {
Probe() error
}
21 changes: 16 additions & 5 deletions plugins/inputs/nvidia_smi/nvidia_smi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ type NvidiaSMI struct {
Timeout config.Duration `toml:"timeout"`
Log telegraf.Logger `toml:"-"`

ignorePlugin bool
once sync.Once
nvidiaSMIArgs []string
ignorePlugin bool
once sync.Once
}

func (*NvidiaSMI) SampleConfig() string {
Expand All @@ -53,14 +54,23 @@ func (smi *NvidiaSMI) Start(telegraf.Accumulator) error {

func (smi *NvidiaSMI) Stop() {}

func (smi *NvidiaSMI) Probe() error {
// Construct and execute metrics query
_, err := internal.CombinedOutputTimeout(exec.Command(smi.BinPath, smi.nvidiaSMIArgs...), time.Duration(smi.Timeout))
if err != nil {
return fmt.Errorf("calling %q failed: %w", smi.BinPath, err)
}
return nil
}

// Gather implements the telegraf interface
func (smi *NvidiaSMI) Gather(acc telegraf.Accumulator) error {
if smi.ignorePlugin {
return nil
}

// Construct and execute metrics query
data, err := internal.CombinedOutputTimeout(exec.Command(smi.BinPath, "-q", "-x"), time.Duration(smi.Timeout))
data, err := internal.CombinedOutputTimeout(exec.Command(smi.BinPath, smi.nvidiaSMIArgs...), time.Duration(smi.Timeout))
if err != nil {
return fmt.Errorf("calling %q failed: %w", smi.BinPath, err)
}
Expand Down Expand Up @@ -119,8 +129,9 @@ func (smi *NvidiaSMI) parse(acc telegraf.Accumulator, data []byte) error {
func init() {
inputs.Add("nvidia_smi", func() telegraf.Input {
return &NvidiaSMI{
BinPath: "/usr/bin/nvidia-smi",
Timeout: config.Duration(5 * time.Second),
BinPath: "/usr/bin/nvidia-smi",
Timeout: config.Duration(5 * time.Second),
nvidiaSMIArgs: []string{"-q", "-x"},
}
})
}
50 changes: 50 additions & 0 deletions plugins/inputs/nvidia_smi/nvidia_smi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,66 @@ import (
"errors"
"os"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func TestProbe(t *testing.T) {
var binPath string
var nvidiaSMIArgsPrefix []string
if runtime.GOOS == "windows" {
binPath = `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`
nvidiaSMIArgsPrefix = []string{"-Command"}
} else {
binPath = "/bin/bash"
nvidiaSMIArgsPrefix = []string{"-c"}
}

for _, tt := range []struct {
name string
args string
expectError bool
}{
{
name: "probe success",
args: "exit 0",
expectError: false,
},
{
name: "probe error",
args: "exit 1",
expectError: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
plugin := &NvidiaSMI{
BinPath: binPath,
nvidiaSMIArgs: append(nvidiaSMIArgsPrefix, tt.args),
Log: &testutil.Logger{},
Timeout: config.Duration(5 * time.Second),
}
model := models.NewRunningInput(plugin, &models.InputConfig{
Name: "nvidia_smi",
StartupErrorBehavior: "probe",
})
err := model.Probe()
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func TestErrorBehaviorDefault(t *testing.T) {
// make sure we can't find nvidia-smi in $PATH somewhere
os.Unsetenv("PATH")
Expand Down
Loading