-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
add-http_response 添加trace数据 #1133
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
|
@@ -18,6 +20,7 @@ import ( | |
"flashcat.cloud/categraf/pkg/httpx" | ||
"flashcat.cloud/categraf/pkg/netx" | ||
"flashcat.cloud/categraf/types" | ||
"github.com/google/uuid" | ||
) | ||
|
||
const ( | ||
|
@@ -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 | ||
|
@@ -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 { | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 时间可以都修改为按照毫秒 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个插件原有指标也可以修改为毫秒吗,如http_response_response_time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以改,为了兼容老的用户告警,最好新增一个 xxx_time_ms 指标 |
||
}, | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceid不建议添加到label,对于时序指标来说不是稳态
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
主要考虑是将同一个请求通过traceid给关联到一起,至于稳态不太理解。能否解释下。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
比如每10秒拨测一次。 这样这个十秒和下一个十秒的指标 label不同,这样的指标会造成tsdb的存储压力