Skip to content

Commit

Permalink
Return missing sampled and traceparent headers
Browse files Browse the repository at this point in the history
  • Loading branch information
muralov committed Sep 11, 2023
1 parent 5e50866 commit e014286
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions e2e/sink/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ import (
cev2http "github.com/cloudevents/sdk-go/v2/protocol/http"
)

const (
SampledHeader = "X-B3-Sampled"
TraceparentHeader = "Traceparent"
)

// Handler interface for the SinkHandler.
type Handler interface {
Start() error
}

type SinkHandler struct {
logger *zap.Logger
events map[string]*cev2event.Event
cache map[string]*cev2event.Event
events map[string]*sinkEvent
}

func NewSinkHandler(logger *zap.Logger) *SinkHandler {
return &SinkHandler{
logger: logger,
events: make(map[string]*cev2event.Event),
events: make(map[string]*sinkEvent),
}
}

Expand All @@ -38,6 +42,12 @@ func (h *SinkHandler) Start() error {
return http.ListenAndServe(":8080", router)
}

type sinkEvent struct {
// Header stores the non CE events, e.g. X-B3-Sampled and Traceparent
http.Header
cev2event.Event
}

func (h *SinkHandler) StoreEvent(w http.ResponseWriter, r *http.Request) {
event, err := extractCloudEventFromRequest(r)
if err != nil {
Expand All @@ -49,7 +59,15 @@ func (h *SinkHandler) StoreEvent(w http.ResponseWriter, r *http.Request) {
return
}

h.events[event.ID()] = event
// store the event in memory
h.events[event.ID()] = &sinkEvent{
Header: http.Header{
SampledHeader: r.Header[SampledHeader],
TraceparentHeader: r.Header[TraceparentHeader],
},
Event: *event,
}

err = writeResponse(w, http.StatusNoContent, []byte(""))
if err != nil {
h.namedLogger().Error("failed to write response", zap.Error(err))
Expand Down Expand Up @@ -78,6 +96,10 @@ func (h *SinkHandler) GetEvent(w http.ResponseWriter, r *http.Request) {
return
}

// fill the non CE headers
w.Header().Set(SampledHeader, event.Header.Get(SampledHeader))
w.Header().Set(TraceparentHeader, event.Header.Get(TraceparentHeader))
w.Header().Set("Content-Type", "application/json")
err = writeResponse(w, http.StatusOK, respBody)
if err != nil {
h.namedLogger().Error("failed to write response", zap.Error(err))
Expand Down

0 comments on commit e014286

Please sign in to comment.