-
Notifications
You must be signed in to change notification settings - Fork 66
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
Support pganalyze tracestate to set start time of the span #475
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package querysample | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/pganalyze/collector/state" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type startAndEndTimeTestPair struct { | ||
testName string | ||
traceState trace.TraceState | ||
sample state.PostgresQuerySample | ||
startTime time.Time | ||
endTime time.Time | ||
} | ||
|
||
func TestStartAndEndTime(t *testing.T) { | ||
currentTime, err := time.Parse("2006-01-02", "2023-01-01") | ||
if err != nil { | ||
t.Fatalf("Failed to initialize object: %v", err) | ||
} | ||
traceState := trace.TraceState{} | ||
otelTraceState, err := traceState.Insert("ot", "p:8;r:62") | ||
if err != nil { | ||
t.Fatalf("Failed to initialize object: %v", err) | ||
} | ||
pganalyzeTraceStateWithoutT, err := traceState.Insert("pganalyze", "x:foo;y:bar") | ||
if err != nil { | ||
t.Fatalf("Failed to initialize object: %v", err) | ||
} | ||
// inserting the same key will update the value | ||
pganalyzeTraceState, err := traceState.Insert("pganalyze", "t:1697666938.6297212") | ||
keiko713 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
t.Fatalf("Failed to initialize object: %v", err) | ||
} | ||
// 1697666938.6297212 = 2023-10-18 22:08:58.6297212 | ||
pganalyzeTime, err := time.Parse("2006-01-02T15:04:05.999999999", "2023-10-18T22:08:58.6297212") | ||
if err != nil { | ||
t.Fatalf("Failed to initialize object: %v", err) | ||
} | ||
// due to the limitation of the floating point, the result won't exactly like above, so tweaking to pass the test | ||
pganalyzeTime = pganalyzeTime.Add(-1 * 112) | ||
|
||
var startAndEndTimeTests = []startAndEndTimeTestPair{ | ||
{ | ||
"No trace state", | ||
trace.TraceState{}, | ||
state.PostgresQuerySample{RuntimeMs: 1000, OccurredAt: currentTime}, | ||
currentTime.Add(-1 * 1000 * time.Millisecond), | ||
currentTime, | ||
}, | ||
{ | ||
"No pganalyze trace state", | ||
otelTraceState, | ||
state.PostgresQuerySample{RuntimeMs: 1000, OccurredAt: currentTime}, | ||
currentTime.Add(-1 * 1000 * time.Millisecond), | ||
currentTime, | ||
}, | ||
{ | ||
"pganalyze trace state without t", | ||
pganalyzeTraceStateWithoutT, | ||
state.PostgresQuerySample{RuntimeMs: 1000, OccurredAt: currentTime}, | ||
currentTime.Add(-1 * 1000 * time.Millisecond), | ||
currentTime, | ||
}, | ||
{ | ||
"pganalyze trace state", | ||
pganalyzeTraceState, | ||
state.PostgresQuerySample{RuntimeMs: 1000, OccurredAt: currentTime}, | ||
pganalyzeTime, | ||
pganalyzeTime.Add(1000 * time.Millisecond), | ||
}, | ||
} | ||
|
||
for _, pair := range startAndEndTimeTests { | ||
startTime, endTime := startAndEndTime(pair.traceState, pair.sample) | ||
if pair.startTime != startTime { | ||
t.Errorf("For %s: expected startTime to be %v, but was %v\n", pair.testName, pair.startTime, startTime) | ||
} | ||
if pair.endTime != endTime { | ||
t.Errorf("For %s: expected endTime to be %v, but was %v\n", pair.testName, pair.endTime, endTime) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really feel like there should be some easy way to do this, I might be simply missing something.
Also, I could try some another way, like splitting
keyAndValue[1]
with.
then parsing both of them to int. In that way, I don't need to fight with float in test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for parsing this as two separate values. It looks like Go's
time.Time
has an internal representation that allows it more nanosecond precision than can be stored in a 64-bit float. I don't think it will really matter in practice, but we might as well get this right. And simplifying the test is a nice bonus.Since that's going to complicate this branch, I think we should extract that to a separate function like
func parseTraceStateStartTime(value string) (time.Time, error)
(no strong feelings on the name, just throwing something out there).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah @lfittl sorry looks like you just approved (forgot to push) but I did some refactoring around here and put that functionality to util. Well hope you're okay with that? Or feel free to 👀 again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keiko713 Refactoring looks good!