MCPcopy
hub / github.com/grafana/tempo / ValidateAndSanitizeRequest

Function ValidateAndSanitizeRequest

pkg/api/http.go:782–857  ·  view source on GitHub ↗

ValidateAndSanitizeRequest validates params for trace by id api return values are (blockStart, blockEnd, queryMode, start, end, error)

(r *http.Request)

Source from the content-addressed store, hash-verified

780// ValidateAndSanitizeRequest validates params for trace by id api
781// return values are (blockStart, blockEnd, queryMode, start, end, error)
782func ValidateAndSanitizeRequest(r *http.Request) (string, string, string, int64, int64, error) {
783 vals := r.URL.Query()
784
785 q, _ := extractQueryParam(vals, QueryModeKey)
786
787 // validate queryMode. it should either be empty or one of (QueryModeIngesters|QueryModeBlocks|QueryModeAll)
788 var queryMode string
789 var startTime int64
790 var endTime int64
791 var blockStart string
792 var blockEnd string
793
794 switch {
795 case len(q) == 0 || q == QueryModeAll:
796 queryMode = QueryModeAll
797 case q == QueryModeIngesters:
798 queryMode = QueryModeIngesters
799 case q == QueryModeBlocks:
800 queryMode = QueryModeBlocks
801 case q == QueryModeExternal:
802 queryMode = QueryModeExternal
803 default:
804 return "", "", "", 0, 0, fmt.Errorf("invalid value for mode %s", q)
805 }
806
807 // no need to validate/sanitize other parameters if queryMode == QueryModeIngesters
808 if queryMode == QueryModeIngesters {
809 return "", "", queryMode, 0, 0, nil
810 }
811
812 if start, ok := extractQueryParam(vals, BlockStartKey); ok {
813 _, err := uuid.Parse(start)
814 if err != nil {
815 return "", "", "", 0, 0, fmt.Errorf("invalid value for blockstart: %w", err)
816 }
817 blockStart = start
818 } else {
819 blockStart = tempodb.BlockIDMin
820 }
821
822 if end, ok := extractQueryParam(vals, BlockEndKey); ok {
823 _, err := uuid.Parse(end)
824 if err != nil {
825 return "", "", "", 0, 0, fmt.Errorf("invalid value for blockEnd: %w", err)
826 }
827 blockEnd = end
828 } else {
829 blockEnd = tempodb.BlockIDMax
830 }
831
832 if s, ok := extractQueryParam(vals, urlParamStart); ok {
833 var err error
834 startTime, err = strconv.ParseInt(s, 10, 64)
835 if err != nil {
836 return "", "", "", 0, 0, fmt.Errorf("invalid start: %w", err)
837 }
838 } else {
839 startTime = 0

Callers 5

newTraceIDHandlerFunction · 0.92
newTraceIDV2HandlerFunction · 0.92
TraceByIDHandlerMethod · 0.92
TraceByIDHandlerV2Method · 0.92

Calls 3

extractQueryParamFunction · 0.85
QueryMethod · 0.80
ParseMethod · 0.65

Tested by 1