Document and add support to input plugins for logging alias (#6357)

This commit is contained in:
Greg
2019-09-23 16:39:50 -06:00
committed by Daniel Nelson
parent e42d2e39c6
commit 817c9a69a9
111 changed files with 961 additions and 659 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
@@ -34,6 +33,8 @@ type HTTPResponse struct {
Interface string
tls.ClientConfig
Log telegraf.Logger
compiledStringMatch *regexp.Regexp
client *http.Client
}
@@ -242,7 +243,7 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]
// HTTP error codes do not generate errors in the net/http library
if err != nil {
// Log error
log.Printf("D! Network error while polling %s: %s", u, err.Error())
h.Log.Debugf("Network error while polling %s: %s", u, err.Error())
// Get error details
netErr := setError(err, fields, tags)
@@ -271,7 +272,7 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("D! Failed to read body of HTTP Response : %s", err)
h.Log.Debugf("Failed to read body of HTTP Response : %s", err.Error())
setResult("body_read_error", fields, tags)
fields["content_length"] = len(bodyBytes)
if h.ResponseStringMatch != "" {
@@ -322,7 +323,7 @@ func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
if h.Address == "" {
h.URLs = []string{"http://localhost"}
} else {
log.Printf("W! [inputs.http_response] 'address' deprecated in telegraf 1.12, please use 'urls'")
h.Log.Warn("'address' deprecated in telegraf 1.12, please use 'urls'")
h.URLs = []string{h.Address}
}
}

View File

@@ -150,6 +150,7 @@ func TestHeaders(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL,
Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 2},
@@ -185,6 +186,7 @@ func TestFields(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -246,6 +248,7 @@ func TestInterface(t *testing.T) {
require.NoError(t, err)
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -284,6 +287,7 @@ func TestRedirects(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/redirect",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -314,6 +318,7 @@ func TestRedirects(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/badredirect",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -350,6 +355,7 @@ func TestMethod(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/mustbepostmethod",
Body: "{ 'test': 'data'}",
Method: "POST",
@@ -380,6 +386,7 @@ func TestMethod(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/mustbepostmethod",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -411,6 +418,7 @@ func TestMethod(t *testing.T) {
//check that lowercase methods work correctly
h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/mustbepostmethod",
Body: "{ 'test': 'data'}",
Method: "head",
@@ -447,6 +455,7 @@ func TestBody(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/musthaveabody",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -477,6 +486,7 @@ func TestBody(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/musthaveabody",
Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
@@ -510,6 +520,7 @@ func TestStringMatch(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -547,6 +558,7 @@ func TestStringMatchJson(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/jsonresponse",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -584,6 +596,7 @@ func TestStringMatchFail(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -626,6 +639,7 @@ func TestTimeout(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/twosecondnap",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -659,6 +673,7 @@ func TestBadRegex(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good",
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -682,6 +697,7 @@ func TestBadRegex(t *testing.T) {
func TestNetworkErrors(t *testing.T) {
// DNS error
h := &HTTPResponse{
Log: testutil.Logger{},
Address: "https://nonexistent.nonexistent", // Any non-resolvable URL works here
Body: "",
Method: "GET",
@@ -708,6 +724,7 @@ func TestNetworkErrors(t *testing.T) {
// Connecton failed
h = &HTTPResponse{
Log: testutil.Logger{},
Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here
Body: "",
Method: "GET",
@@ -739,6 +756,7 @@ func TestContentLength(t *testing.T) {
defer ts.Close()
h := &HTTPResponse{
Log: testutil.Logger{},
URLs: []string{ts.URL + "/good"},
Body: "{ 'test': 'data'}",
Method: "GET",
@@ -769,6 +787,7 @@ func TestContentLength(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{
Log: testutil.Logger{},
URLs: []string{ts.URL + "/musthaveabody"},
Body: "{ 'test': 'data'}",
Method: "GET",