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

add-http_response 添加trace数据 #1133

Merged
merged 2 commits into from
Jan 20, 2025
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
8 changes: 7 additions & 1 deletion conf/input.http_response/http_response.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
[[instances]]
targets = [
# "http://localhost",
# "https://www.baidu.com"
]

## append some labels for series
Expand All @@ -30,6 +29,13 @@ targets = [
## Set response_timeout (default 5 seconds)
# response_timeout = "5s"

## enalbe http trace(defaults to false)
## add traceid for traceing
## add http_response_remote_addr for remote add and port
## add fields http_response_dns_time -> http_response_connect_time -> http_response_tls_time -> http_response_first_response_time -> http_response_end_response_time
## all the fields add equal to http_response_response_time
#trace = false

## Whether to follow redirects from the server (defaults to false)
# follow_redirects = false

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gophercloud/gophercloud v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg=
Expand Down
37 changes: 37 additions & 0 deletions inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package http_response

import (
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -44,6 +46,7 @@ type Instance struct {
ExpectResponseRegularExpression string `toml:"expect_response_regular_expression"`
ExpectResponseStatusCode *int `toml:"expect_response_status_code"`
ExpectResponseStatusCodes string `toml:"expect_response_status_codes"`
Trace *bool `toml:"trace"`
config.HTTPProxy

client httpClient
Expand Down Expand Up @@ -248,10 +251,44 @@ func (ins *Instance) httpGather(target string) (map[string]string, map[string]in

// Start Timer
start := time.Now()
dns_time := start
conn_time := start
tls_time := start
first_res_time := start

if ins.Trace != nil && *ins.Trace {
trace := &httptrace.ClientTrace{
// request
DNSDone: func(info httptrace.DNSDoneInfo) {
dns_time = time.Now()
fields["dns_time"] = time.Since(start).Milliseconds()
},
ConnectDone: func(network, addr string, err error) {
conn_time = time.Now()
tags["remote_addr"] = addr
fields["connect_time"] = time.Since(dns_time).Milliseconds()
},
TLSHandshakeDone: func(info tls.ConnectionState, err error) {
tls_time = time.Now()
fields["tls_time"] = time.Since(conn_time).Milliseconds()
},
GotFirstResponseByte: func() {
first_res_time = time.Now()
if tls_time == start {
fields["first_response_time"] = time.Since(conn_time).Milliseconds()
} else {
fields["first_response_time"] = time.Since(tls_time).Milliseconds()
}
},
}
request = request.WithContext(httptrace.WithClientTrace(request.Context(), trace))
}
resp, err := ins.client.Do(request)

// metric: response_time
fields["end_response_time"] = time.Since(first_res_time).Milliseconds()
fields["response_time"] = time.Since(start).Seconds()
fields["response_time_ms"] = time.Since(start).Milliseconds()

// If an error in returned, it means we are dealing with a network error, as
// HTTP error codes do not generate errors in the net/http library
Expand Down
Loading