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

fix(inputs.phpfpm): Use logger without causing panic #14489

Merged
Merged
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
4 changes: 2 additions & 2 deletions plugins/inputs/phpfpm/phpfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type phpfpm struct {

tls.ClientConfig
client *http.Client
log telegraf.Logger
Log telegraf.Logger
}

func (*phpfpm) SampleConfig() string {
Expand Down Expand Up @@ -300,7 +300,7 @@ func parseLines(r io.Reader, acc telegraf.Accumulator, addr string) {
func (p *phpfpm) parseJSON(r io.Reader, acc telegraf.Accumulator, addr string) {
var metrics JSONMetrics
if err := json.NewDecoder(r).Decode(&metrics); err != nil {
p.log.Errorf("Unable to decode JSON response: %s", err)
p.Log.Errorf("Unable to decode JSON response: %s", err)
return
}
timestamp := time.Now()
Expand Down
37 changes: 34 additions & 3 deletions plugins/inputs/phpfpm/phpfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package phpfpm

import (
"bytes"
"crypto/rand"
_ "embed"
"encoding/binary"
"fmt"
"log"
"net"
"net/http"
"net/http/fcgi"
Expand All @@ -19,6 +22,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/plugins/common/shim"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/testutil"
)
Expand Down Expand Up @@ -76,8 +80,6 @@ func TestPhpFpmGeneratesMetrics_From_Http(t *testing.T) {
}

func TestPhpFpmGeneratesJSONMetrics_From_Http(t *testing.T) {
outputSampleJSON, err := os.ReadFile("testdata/phpfpm.json")
require.NoError(t, err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/json")
w.Header().Set("Content-Length", strconv.Itoa(len(outputSampleJSON)))
Expand All @@ -94,7 +96,7 @@ func TestPhpFpmGeneratesJSONMetrics_From_Http(t *testing.T) {
input := &phpfpm{
Urls: []string{server.URL + "?full&json"},
Format: "json",
log: testutil.Logger{},
Log: testutil.Logger{},
}
require.NoError(t, input.Init())

Expand Down Expand Up @@ -358,3 +360,32 @@ max active processes: 1
max children reached: 2
slow requests: 1
`

//go:embed testdata/phpfpm.json
var outputSampleJSON []byte

func TestPhpFpmParseJSON_Log_Error_Without_Panic_When_When_JSON_Is_Invalid(t *testing.T) {
p := &phpfpm{}
// AddInput sets the Logger
if err := shim.New().AddInput(p); err != nil {
t.Error(err)
return
}

// capture log output
var logOutput bytes.Buffer
log.SetOutput(&logOutput)
defer func() {
log.SetOutput(os.Stderr)
}()

// parse valid JSON without panic and without log output
validJSON := outputSampleJSON
require.NotPanics(t, func() { p.parseJSON(bytes.NewReader(validJSON), &testutil.NopAccumulator{}, "") })
require.Equal(t, "", logOutput.String())

// parse invalid JSON without panic but with log output
invalidJSON := []byte("X")
require.NotPanics(t, func() { p.parseJSON(bytes.NewReader(invalidJSON), &testutil.NopAccumulator{}, "") })
require.Contains(t, logOutput.String(), "E! Unable to decode JSON response: invalid character 'X' looking for beginning of value")
}
srebhan marked this conversation as resolved.
Show resolved Hide resolved
Loading