Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira committed Apr 27, 2023
1 parent d8375c4 commit 2716783
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 deletions server/resourcemanager/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,33 @@ func (m *manager[T]) RegisterRoutes(r *mux.Router) *mux.Router {
enabledOps := m.EnabledOperations()

if slices.Contains(enabledOps, OperationList) {
subrouter.HandleFunc("", m.list).Methods(http.MethodGet).Name("list")
m.registerEndpoint(subrouter, "", m.list).Methods(http.MethodGet).Name("list")
}

if slices.Contains(enabledOps, OperationCreate) {
subrouter.HandleFunc("", m.create).Methods(http.MethodPost).Name(fmt.Sprintf("%s.Create", m.resourceTypePlural))
m.registerEndpoint(subrouter, "", m.create).Methods(http.MethodPost).Name(fmt.Sprintf("%s.Create", m.resourceTypePlural))
}

if slices.Contains(enabledOps, OperationUpdate) {
subrouter.HandleFunc("/{id}", m.update).Methods(http.MethodPut).Name(fmt.Sprintf("%s.Update", m.resourceTypePlural))
m.registerEndpoint(subrouter, "/{id}", m.update).Methods(http.MethodPut).Name(fmt.Sprintf("%s.Update", m.resourceTypePlural))
}

if slices.Contains(enabledOps, OperationGet) {
subrouter.HandleFunc("/{id}", m.get).Methods(http.MethodGet).Name(fmt.Sprintf("%s.Get", m.resourceTypePlural))
m.registerEndpoint(subrouter, "/{id}", m.get).Methods(http.MethodGet).Name(fmt.Sprintf("%s.Get", m.resourceTypePlural))
}

if slices.Contains(enabledOps, OperationDelete) {
subrouter.HandleFunc("/{id}", m.delete).Methods(http.MethodDelete).Name(fmt.Sprintf("%s.Delete", m.resourceTypePlural))
m.registerEndpoint(subrouter, "/{id}", m.delete).Methods(http.MethodDelete).Name(fmt.Sprintf("%s.Delete", m.resourceTypePlural))
}

return subrouter
}

func (m *manager[T]) tracingMiddleware(next http.Handler) http.Handler {
func (m *manager[T]) registerEndpoint(subrouter *mux.Router, path string, handler http.HandlerFunc) *mux.Route {
return subrouter.HandleFunc(path, m.tracingMiddleware(path, handler))
}

func (m *manager[T]) tracingMiddleware(path string, next http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if m.config.tracer == nil {
next.ServeHTTP(w, r)
Expand All @@ -160,7 +164,7 @@ func (m *manager[T]) tracingMiddleware(next http.Handler) http.Handler {
method := r.Method

ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header))
ctx, span := m.config.tracer.Start(ctx, fmt.Sprintf("%s %s", method, r.URL.Path))
ctx, span := m.config.tracer.Start(ctx, fmt.Sprintf("%s %s", method, r.URL.RawPath))
defer span.End()

params := make(map[string]interface{}, 0)
Expand All @@ -184,7 +188,7 @@ func (m *manager[T]) tracingMiddleware(next http.Handler) http.Handler {

span.SetAttributes(
attribute.String(string(semconv.HTTPMethodKey), r.Method),
attribute.String(string(semconv.HTTPRouteKey), r.URL.Path),
attribute.String(string(semconv.HTTPRouteKey), path),
attribute.String(string(semconv.HTTPTargetKey), r.URL.String()),
attribute.String("http.request.params", string(paramsJson)),
attribute.String("http.request.query", string(queryStringJson)),
Expand Down
4 changes: 2 additions & 2 deletions tracetesting/features/environment/01_create_environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ spec:
- selector: span[name = "Tracetest trigger"]
assertions:
- attr:tracetest.selected_spans.count = 1
- attr:tracetest.response.status = 200
- attr:tracetest.response.status = 201
# ensure we can reference outputs declared in the same test
- attr:tracetest.response.body | json_path '$.id' = env:ENVIRONMENT_ID
- attr:tracetest.response.body | json_path '$.spec.id' = env:ENVIRONMENT_ID
- selector: span[name="POST /api/environments" tracetest.span.type="http"]
assertions:
- attr:tracetest.selected_spans.count = 1
Expand Down
2 changes: 1 addition & 1 deletion tracetesting/features/environment/02_list_environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
assertions:
- attr:tracetest.selected_spans.count = 1
- attr:tracetest.response.status = 200
- attr:tracetest.response.body | json_path '$[*].id' contains env:ENVIRONMENT_ID # check if the environment is listed
- attr:tracetest.response.body | json_path '$.items[*].spec.id' contains env:ENVIRONMENT_ID # check if the environment is listed
- selector: span[name="GET /api/environments" tracetest.span.type="http"]
assertions:
- attr:tracetest.selected_spans.count = 1
Expand Down

0 comments on commit 2716783

Please sign in to comment.