Skip to content

Commit

Permalink
Set HTTP Response attributes on telemetry spans (#30)
Browse files Browse the repository at this point in the history
**Problem:**

1. When an HTTP response is created no information is attached to the
spans that are created.

**Solution:**

1. Adding a method to pull out the HTTP response and set some attributes
about the response on both the parent span, and the response span.

**Testing:**

1. I verified that the content is set on both the root and the response
spans (Via Signoz):

![image](https://github.com/user-attachments/assets/564d2115-cac2-4261-8b61-c2dd5a71fa12)


![image](https://github.com/user-attachments/assets/c7bb0ac1-50c5-4084-9590-c3d9eb265a12)
  • Loading branch information
BryanFauble authored Sep 18, 2024
1 parent 8e05d1a commit 0e60e26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/httpsrv/adapter/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func (srv *Server) ServeHTTP(resWriter http.ResponseWriter, httpReq *http.Reques

rootSpan.SetEventAttributes(req)
respSpan.SetEventAttributes(resp)
httpResp := resp.HTTPResponse()

rootSpan.SetHTTPResponseAttributes(httpResp)
respSpan.SetHTTPResponseAttributes(httpResp)

rootSpan.End()

Expand Down
12 changes: 12 additions & 0 deletions telemetry/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/binary"
"fmt"
"math/rand"
"net/http"
"reflect"
sync "sync"
"time"
Expand Down Expand Up @@ -231,6 +232,17 @@ func (s *Span) SetEventAttributes(evt *core.Event) {
}
}

func (s *Span) SetHTTPResponseAttributes(httpResp *http.Response) {
if s == nil || httpResp == nil {
return
}

s.SetAttributes(
Attr(AttrKeyHTTPRespStatusCode, httpResp.StatusCode),
Attr(AttrKeyHTTPRespBodySize, httpResp.ContentLength),
)
}

func (s *Span) Record() bool {
if s.TraceState == "kf=1" {
return true
Expand Down

0 comments on commit 0e60e26

Please sign in to comment.