Skip to content

Commit

Permalink
add-http_response 添加trace数据
Browse files Browse the repository at this point in the history
  • Loading branch information
lianggao committed Jan 10, 2025
1 parent ea21107 commit a86f4ed
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
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
45 changes: 44 additions & 1 deletion 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 All @@ -18,6 +20,7 @@ import (
"flashcat.cloud/categraf/pkg/httpx"
"flashcat.cloud/categraf/pkg/netx"
"flashcat.cloud/categraf/types"
"github.com/google/uuid"
)

const (
Expand All @@ -44,6 +47,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 @@ -192,7 +196,13 @@ func (ins *Instance) gather(slist *types.SampleList, target string) {
log.Println("D! http_response... target:", target)
}

labels := map[string]string{"target": target}
var labels map[string]string
if ins.Trace != nil && *ins.Trace {
traceid := uuid.New().String()
labels = map[string]string{"target": target, "traceid": traceid}
} else {
labels = map[string]string{"target": target}
}
fields := map[string]interface{}{}
// Add extra tags in batches
if m, ok := ins.Mappings[target]; ok {
Expand Down Expand Up @@ -248,9 +258,42 @@ 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).Seconds()
},
ConnectDone: func(network, addr string, err error) {
conn_time = time.Now()
tags["remote_addr"] = addr
fields["connect_time"] = time.Since(dns_time).Seconds()
},
TLSHandshakeDone: func(info tls.ConnectionState, err error) {
tls_time = time.Now()
fields["tls_time"] = time.Since(conn_time).Seconds()
},
GotFirstResponseByte: func() {
first_res_time = time.Now()
if tls_time == start {
fields["first_response_time"] = time.Since(conn_time).Seconds()
} else {
fields["first_response_time"] = time.Since(tls_time).Seconds()
}
},
}
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).Seconds()
fields["response_time"] = time.Since(start).Seconds()

// If an error in returned, it means we are dealing with a network error, as
Expand Down

0 comments on commit a86f4ed

Please sign in to comment.