add http method to span name

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
This commit is contained in:
Ziqi Zhao 2024-03-13 13:25:36 +08:00
parent 02154293c7
commit 1aeb0ba314
2 changed files with 10 additions and 10 deletions

View File

@ -39,7 +39,7 @@ func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler {
if !exist { if !exist {
return "KubernetesAPI" return "KubernetesAPI"
} }
return getSpanNameFromRequestInfo(info) return getSpanNameFromRequestInfo(info, r)
}), }),
} }
wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -55,9 +55,9 @@ func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler {
return otelhttp.NewHandler(wrappedHandler, "KubernetesAPI", opts...) return otelhttp.NewHandler(wrappedHandler, "KubernetesAPI", opts...)
} }
func getSpanNameFromRequestInfo(info *request.RequestInfo) string { func getSpanNameFromRequestInfo(info *request.RequestInfo, r *http.Request) string {
if !info.IsResourceRequest { if !info.IsResourceRequest {
return info.Path return r.Method + " " + info.Path
} }
spanName := "/" + info.APIPrefix spanName := "/" + info.APIPrefix
@ -75,5 +75,5 @@ func getSpanNameFromRequestInfo(info *request.RequestInfo) string {
if info.Subresource != "" { if info.Subresource != "" {
spanName += "/" + info.Subresource spanName += "/" + info.Subresource
} }
return spanName return r.Method + " " + spanName
} }

View File

@ -309,7 +309,7 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil {
}, },
expectedTrace: []*spanExpectation{ expectedTrace: []*spanExpectation{
{ {
name: "/api/v1/nodes", name: "POST /api/v1/nodes",
attributes: map[string]func(*commonv1.AnyValue) bool{ attributes: map[string]func(*commonv1.AnyValue) bool{
"http.user_agent": func(v *commonv1.AnyValue) bool { "http.user_agent": func(v *commonv1.AnyValue) bool {
return strings.HasPrefix(v.GetStringValue(), "tracing.test") return strings.HasPrefix(v.GetStringValue(), "tracing.test")
@ -428,7 +428,7 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil {
}, },
expectedTrace: []*spanExpectation{ expectedTrace: []*spanExpectation{
{ {
name: "/api/v1/nodes/{:name}", name: "GET /api/v1/nodes/{:name}",
attributes: map[string]func(*commonv1.AnyValue) bool{ attributes: map[string]func(*commonv1.AnyValue) bool{
"http.user_agent": func(v *commonv1.AnyValue) bool { "http.user_agent": func(v *commonv1.AnyValue) bool {
return strings.HasPrefix(v.GetStringValue(), "tracing.test") return strings.HasPrefix(v.GetStringValue(), "tracing.test")
@ -518,7 +518,7 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil {
}, },
expectedTrace: []*spanExpectation{ expectedTrace: []*spanExpectation{
{ {
name: "/api/v1/nodes", name: "GET /api/v1/nodes",
attributes: map[string]func(*commonv1.AnyValue) bool{ attributes: map[string]func(*commonv1.AnyValue) bool{
"http.user_agent": func(v *commonv1.AnyValue) bool { "http.user_agent": func(v *commonv1.AnyValue) bool {
return strings.HasPrefix(v.GetStringValue(), "tracing.test") return strings.HasPrefix(v.GetStringValue(), "tracing.test")
@ -636,7 +636,7 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil {
}, },
expectedTrace: []*spanExpectation{ expectedTrace: []*spanExpectation{
{ {
name: "/api/v1/nodes/{:name}", name: "PUT /api/v1/nodes/{:name}",
attributes: map[string]func(*commonv1.AnyValue) bool{ attributes: map[string]func(*commonv1.AnyValue) bool{
"http.user_agent": func(v *commonv1.AnyValue) bool { "http.user_agent": func(v *commonv1.AnyValue) bool {
return strings.HasPrefix(v.GetStringValue(), "tracing.test") return strings.HasPrefix(v.GetStringValue(), "tracing.test")
@ -780,7 +780,7 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil {
}, },
expectedTrace: []*spanExpectation{ expectedTrace: []*spanExpectation{
{ {
name: "/api/v1/nodes/{:name}", name: "PATCH /api/v1/nodes/{:name}",
attributes: map[string]func(*commonv1.AnyValue) bool{ attributes: map[string]func(*commonv1.AnyValue) bool{
"http.user_agent": func(v *commonv1.AnyValue) bool { "http.user_agent": func(v *commonv1.AnyValue) bool {
return strings.HasPrefix(v.GetStringValue(), "tracing.test") return strings.HasPrefix(v.GetStringValue(), "tracing.test")
@ -901,7 +901,7 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil {
}, },
expectedTrace: []*spanExpectation{ expectedTrace: []*spanExpectation{
{ {
name: "/api/v1/nodes/{:name}", name: "DELETE /api/v1/nodes/{:name}",
attributes: map[string]func(*commonv1.AnyValue) bool{ attributes: map[string]func(*commonv1.AnyValue) bool{
"http.user_agent": func(v *commonv1.AnyValue) bool { "http.user_agent": func(v *commonv1.AnyValue) bool {
return strings.HasPrefix(v.GetStringValue(), "tracing.test") return strings.HasPrefix(v.GetStringValue(), "tracing.test")