Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren committed Sep 20, 2023
1 parent 1f04884 commit b8aecb2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ server/html

# autogenerated with `cd cli; make docgen`
docs/docs/cli/reference/

__debug*
4 changes: 2 additions & 2 deletions agent/workers/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func convertTraceInToProtoSpans(trace traces.Trace) []*proto.Span {
ParentId: getParentID(span),
Name: span.Name,
Kind: string(span.Kind),
StartTime: span.StartTime.UnixMicro(),
EndTime: span.EndTime.UnixMicro(),
StartTime: span.StartTime.UnixNano(),
EndTime: span.EndTime.UnixNano(),
Attributes: attributes,
}
spans = append(spans, &protoSpan)
Expand Down
2 changes: 2 additions & 0 deletions server/executor/trigger_result_processor_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (r triggerResultProcessorWorker) ProcessItem(ctx context.Context, job Job)
}

job.Run.State = test.RunStateAwaitingTrace

r.handleDBError(job.Run, r.updater.Update(ctx, job.Run))

r.outputQueue.Enqueue(ctx, job)
Expand Down Expand Up @@ -120,6 +121,7 @@ func (r triggerResultProcessorWorker) emitMismatchEndpointEvent(ctx context.Cont
}

func (r triggerResultProcessorWorker) handleExecutionResult(run test.Run) test.Run {
run = run.TriggerCompleted(run.TriggerResult)
if run.TriggerResult.Error != nil {
run = run.TriggerFailed(fmt.Errorf(run.TriggerResult.Error.ErrorMessage))

Expand Down
17 changes: 13 additions & 4 deletions server/http/mappings/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package mappings

import (
"strconv"
"time"

"github.com/kubeshop/tracetest/server/openapi"
"github.com/kubeshop/tracetest/server/pkg/timing"
"github.com/kubeshop/tracetest/server/traces"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -81,21 +81,30 @@ func (m Model) Trace(in openapi.Trace) *traces.Trace {
flat[sid] = &span
}

tree := m.Span(in.Tree, nil)
if spanIsEmpty(tree) && len(flat) == 0 {
return nil
}

return &traces.Trace{
ID: tid,
RootSpan: m.Span(in.Tree, nil),
RootSpan: tree,
Flat: flat,
}
}

func spanIsEmpty(span traces.Span) bool {
return span.ID == trace.SpanID{} && len(span.Attributes) == 0 && span.Name == "" && len(span.Children) == 0
}

func (m Model) Span(in openapi.Span, parent *traces.Span) traces.Span {
sid, _ := trace.SpanIDFromHex(in.Id)
span := traces.Span{
ID: sid,
Attributes: in.Attributes,
Name: in.Name,
StartTime: time.UnixMilli(int64(in.StartTime)),
EndTime: time.UnixMilli(int64(in.EndTime)),
StartTime: timing.ParseUnix(int64(in.StartTime)),
EndTime: timing.ParseUnix(int64(in.EndTime)),
Parent: parent,
}
span.Children = m.Spans(in.Children, &span)
Expand Down
26 changes: 14 additions & 12 deletions server/pkg/timing/timing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package timing

import (
"math"
"time"
)

Expand All @@ -19,18 +18,21 @@ func TimeDiff(start, end time.Time) time.Duration {
return endDate.Sub(start)
}

func DurationInMillieconds(d time.Duration) int {
return int(d.Milliseconds())
}

func DurationInNanoseconds(d time.Duration) int {
return int(d.Nanoseconds())
func dateIsZero(in time.Time) bool {
return in.IsZero() || in.Unix() == 0
}

func DurationInSeconds(d time.Duration) int {
return int(math.Ceil(d.Seconds()))
}
// ParseUnix parses a unix timestamp into a time.Time
// it accepts an integer which can be either milli or nano
func ParseUnix(timestamp int64) time.Time {
// Determine the range of the timestamp to know if it is nano or milli
// we can assume that any timestamp less than this is milliq
const threshold = int64(1e12)

func dateIsZero(in time.Time) bool {
return in.IsZero() || in.Unix() == 0
if timestamp < threshold {
// is milli
return time.Unix(0, timestamp*int64(time.Millisecond))
}
// is nano
return time.Unix(0, timestamp)
}
33 changes: 5 additions & 28 deletions server/test/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kubeshop/tracetest/server/linter/analyzer"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/pkg/timing"
"github.com/kubeshop/tracetest/server/test/trigger"
"github.com/kubeshop/tracetest/server/traces"
"github.com/kubeshop/tracetest/server/variableset"
Expand Down Expand Up @@ -49,37 +50,13 @@ func (r Run) Copy() Run {
}

func (r Run) ExecutionTime() int {
return durationInSeconds(
timeDiff(r.CreatedAt, r.CompletedAt),
)
diff := timing.TimeDiff(r.CreatedAt, r.CompletedAt)
return int(math.Ceil(diff.Seconds()))
}

func (r Run) TriggerTime() int {
return durationInMillieconds(
timeDiff(r.ServiceTriggeredAt, r.ServiceTriggerCompletedAt),
)
}

func timeDiff(start, end time.Time) time.Duration {
var endDate time.Time
if !dateIsZero(end) {
endDate = end
} else {
endDate = Now()
}
return endDate.Sub(start)
}

func durationInMillieconds(d time.Duration) int {
return int(d.Milliseconds())
}

func durationInSeconds(d time.Duration) int {
return int(math.Ceil(d.Seconds()))
}

func dateIsZero(in time.Time) bool {
return in.IsZero() || in.Unix() == 0
diff := timing.TimeDiff(r.ServiceTriggeredAt, r.ServiceTriggerCompletedAt)
return int(diff.Microseconds())
}

func (r Run) Start() Run {
Expand Down
17 changes: 9 additions & 8 deletions server/traces/span_entitiess.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"time"

"github.com/kubeshop/tracetest/server/pkg/timing"
"github.com/kubeshop/tracetest/server/test/trigger"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -124,8 +125,8 @@ func encodeSpan(s Span) encodedSpan {
return encodedSpan{
ID: s.ID.String(),
Name: s.Name,
StartTime: fmt.Sprintf("%d", s.StartTime.UnixMilli()),
EndTime: fmt.Sprintf("%d", s.EndTime.UnixMilli()),
StartTime: strconv.FormatInt(s.StartTime.UnixMilli(), 10),
EndTime: strconv.FormatInt(s.EndTime.UnixMilli(), 10),
Attributes: s.Attributes,
Children: encodeChildren(s.Children),
}
Expand Down Expand Up @@ -180,7 +181,7 @@ func (s *Span) decodeSpan(aux encodedSpan) error {
}

func getTimeFromString(value string) (time.Time, error) {
milliseconds, err := strconv.Atoi(value)
parsedValue, err := strconv.Atoi(value)
if err != nil {
// Maybe it is in RFC3339 format. Convert it for compatibility sake
output, err := time.Parse(time.RFC3339, value)
Expand All @@ -191,7 +192,7 @@ func getTimeFromString(value string) (time.Time, error) {
return output, nil
}

return time.UnixMilli(int64(milliseconds)), nil
return timing.ParseUnix(int64(parsedValue)), nil
}

func decodeChildren(parent *Span, children []encodedSpan, cache spanCache) ([]*Span, error) {
Expand Down Expand Up @@ -233,8 +234,8 @@ func (span Span) setMetadataAttributes() Span {
span.Attributes[TracetestMetadataFieldName] = span.Name
span.Attributes[TracetestMetadataFieldType] = spanType(span.Attributes)
span.Attributes[TracetestMetadataFieldDuration] = spanDuration(span)
span.Attributes[TracetestMetadataFieldStartTime] = fmt.Sprintf("%d", span.StartTime.UnixNano())
span.Attributes[TracetestMetadataFieldEndTime] = fmt.Sprintf("%d", span.EndTime.UnixNano())
span.Attributes[TracetestMetadataFieldStartTime] = strconv.FormatInt(span.StartTime.UTC().UnixNano(), 10)
span.Attributes[TracetestMetadataFieldEndTime] = strconv.FormatInt(span.EndTime.UTC().UnixNano(), 10)

if span.Status != nil {
span.Attributes[TracetestMetadataFieldStatusCode] = span.Status.Code
Expand All @@ -249,13 +250,13 @@ func (span Span) setTriggerResultAttributes(result trigger.TriggerResult) Span {
case trigger.TriggerTypeHTTP:
resp := result.HTTP
jsonheaders, _ := json.Marshal(resp.Headers)
span.Attributes["tracetest.response.status"] = fmt.Sprintf("%d", resp.StatusCode)
span.Attributes["tracetest.response.status"] = strconv.Itoa(resp.StatusCode)
span.Attributes["tracetest.response.body"] = resp.Body
span.Attributes["tracetest.response.headers"] = string(jsonheaders)
case trigger.TriggerTypeGRPC:
resp := result.GRPC
jsonheaders, _ := json.Marshal(resp.Metadata)
span.Attributes["tracetest.response.status"] = fmt.Sprintf("%d", resp.StatusCode)
span.Attributes["tracetest.response.status"] = strconv.Itoa(resp.StatusCode)
span.Attributes["tracetest.response.body"] = resp.Body
span.Attributes["tracetest.response.headers"] = string(jsonheaders)
}
Expand Down
3 changes: 2 additions & 1 deletion server/traces/trace_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"

"github.com/kubeshop/tracetest/server/pkg/id"
Expand Down Expand Up @@ -144,7 +145,7 @@ func spanType(attrs Attributes) string {

func spanDuration(span Span) string {
timeDifference := timing.TimeDiff(span.StartTime, span.EndTime)
return fmt.Sprintf("%d", timing.DurationInNanoseconds(timeDifference))
return strconv.FormatInt(timeDifference.Nanoseconds(), 10)
}

func (t *Trace) Sort() Trace {
Expand Down

0 comments on commit b8aecb2

Please sign in to comment.