Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: endpoint to verify which spans are selected by selector #488

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,40 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestRunResult"
/tests/{testId}/results/{resultId}/select:
get:
tags:
- api
parameters:
- in: path
name: testId
schema:
type: string
format: uuid
required: true
- in: path
name: resultId
schema:
type: string
format: uuid
required: true
- in: query
name: query
schema:
type: string
summary: "retrieve spans that will be selected by selector"
description: "get the spans ids that would be selected by a specific selector query"
operationId: getTestResultSelectedSpans
responses:
200:
description: successful operation
content:
application/json:
schema:
type: array
items:
type: string

/tests/{testId}/results:
get:
tags:
Expand Down
29 changes: 29 additions & 0 deletions server/http/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package http

import (
"context"
"encoding/hex"
"errors"
"net/http"

"github.com/kubeshop/tracetest/analytics"
"github.com/kubeshop/tracetest/assertions/selectors"
"github.com/kubeshop/tracetest/executor"
"github.com/kubeshop/tracetest/openapi"
"github.com/kubeshop/tracetest/testdb"
"github.com/kubeshop/tracetest/tracedb"
"github.com/kubeshop/tracetest/traces"
)

type controller struct {
Expand Down Expand Up @@ -263,3 +266,29 @@ func (s *controller) GetAssertions(ctx context.Context, testID string) (openapi.

return openapi.Response(http.StatusOK, assertions), nil
}

func (s *controller) GetTestResultSelectedSpans(ctx context.Context, testID string, resultID string, selectorQuery string) (openapi.ImplResponse, error) {
selector, err := selectors.New(selectorQuery)
if err != nil {
return openapi.Response(http.StatusBadRequest, "invalid selector query"), nil
}

result, err := s.testDB.GetResult(ctx, resultID)
if err != nil {
return openapi.Response(http.StatusInternalServerError, ""), nil
}

trace, err := traces.FromOtel(result.Trace)
if err != nil {
return openapi.Response(http.StatusInternalServerError, ""), nil
}

selectedSpans := selector.Filter(trace)
selectedSpanIds := make([]string, len(selectedSpans))

for i, span := range selectedSpans {
selectedSpanIds[i] = hex.EncodeToString(span.ID[:])
}

return openapi.Response(http.StatusOK, selectedSpanIds), nil
}
2 changes: 2 additions & 0 deletions server/openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ApiApiRouter interface {
GetAssertions(http.ResponseWriter, *http.Request)
GetTest(http.ResponseWriter, *http.Request)
GetTestResult(http.ResponseWriter, *http.Request)
GetTestResultSelectedSpans(http.ResponseWriter, *http.Request)
GetTestResults(http.ResponseWriter, *http.Request)
GetTests(http.ResponseWriter, *http.Request)
RunTest(http.ResponseWriter, *http.Request)
Expand All @@ -45,6 +46,7 @@ type ApiApiServicer interface {
GetAssertions(context.Context, string) (ImplResponse, error)
GetTest(context.Context, string) (ImplResponse, error)
GetTestResult(context.Context, string, string) (ImplResponse, error)
GetTestResultSelectedSpans(context.Context, string, string, string) (ImplResponse, error)
GetTestResults(context.Context, string, int32, int32) (ImplResponse, error)
GetTests(context.Context, int32, int32) (ImplResponse, error)
RunTest(context.Context, string) (ImplResponse, error)
Expand Down
26 changes: 26 additions & 0 deletions server/openapi/api_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func (c *ApiApiController) Routes() Routes {
"/api/tests/{testId}/results/{resultId}",
c.GetTestResult,
},
{
"GetTestResultSelectedSpans",
strings.ToUpper("Get"),
"/api/tests/{testId}/results/{resultId}/select",
c.GetTestResultSelectedSpans,
},
{
"GetTestResults",
strings.ToUpper("Get"),
Expand Down Expand Up @@ -266,6 +272,26 @@ func (c *ApiApiController) GetTestResult(w http.ResponseWriter, r *http.Request)

}

// GetTestResultSelectedSpans - retrieve spans that will be selected by selector
func (c *ApiApiController) GetTestResultSelectedSpans(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
testIdParam := params["testId"]

resultIdParam := params["resultId"]

queryParam := query.Get("query")
result, err := c.service.GetTestResultSelectedSpans(r.Context(), testIdParam, resultIdParam, queryParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)

}

// GetTestResults - get the results for a test
func (c *ApiApiController) GetTestResults(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
Expand Down