Skip to content

Truncate long queries in error logs#2794

Merged
sougou merged 12 commits intovitessio:masterfrom
tinyspeck:truncate-long-queries-in-error-logs
May 1, 2017
Merged

Truncate long queries in error logs#2794
sougou merged 12 commits intovitessio:masterfrom
tinyspeck:truncate-long-queries-in-error-logs

Conversation

@demmer
Copy link
Copy Markdown
Member

@demmer demmer commented Apr 21, 2017

This change is Reviewable

response.Write([]byte(fmt.Sprintf("Length: %d\n", len(keys))))
for _, v := range keys {
response.Write([]byte(fmt.Sprintf("%#v\n", utils.TruncateQuery(v))))
response.Write([]byte(fmt.Sprintf("%#v\n", utils.TruncateQuery(v, 512))))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be fine with using sqldb.SQLErrorTruncateLen everywhere uniformly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only thought on that is that they are somewhat orthogonal -- truncating in the UI isn't actually affecting errors so I'd want to name it something different.

@sougou
Copy link
Copy Markdown
Contributor

sougou commented Apr 21, 2017

@michael-berlin Do you have any thoughts? Change looks good to me, except for that one comment.

@mberlin-bot
Copy link
Copy Markdown

Overall change looks good to me. See my comments on the flags discussion below.


Reviewed 5 of 5 files at r2, 3 of 3 files at r3.
Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


go/sqldb/sql_error.go, line 10 at r3 (raw file):

	"bytes"
	"fmt"
	"github.com/youtube/vitess/go/vt/utils"

Go readability: This must go in a separate group.


go/sqldb/sql_error.go, line 22 at r3 (raw file):

var (
	// Maximum length for a query in a sqlerror string. 0 means unlimited.
	SQLErrorTruncateLen int = 0

I don't think you need this variable. It adds an extra indirection.

Instead, you can just export sqlErrorTruncateLen and use it directly.


go/vt/vttablet/endtoend/misc_test.go, line 536 at r3 (raw file):

		map[string]interface{}{"data": buf.String()},
	)
	if (err == nil){

Go readability: Surrounding parentheses are not needed.

It looks like you're skipping the Git precommit hook. I thought it would complain about this? Can you please double check?


go/vt/vttablet/tabletserver/query_engine.go, line 473 at r3 (raw file):

Previously, demmer (Michael Demmer) wrote…

My only thought on that is that they are somewhat orthogonal -- truncating in the UI isn't actually affecting errors so I'd want to name it something different.

The 512 should be a constant because you're using it in several places and somebody may change it in one place and forget about the others.

In the name of the constant you could reflect that the limit is meant as default for UI purposes.

In general, I think it makes sense to have two different values for UI truncating and log truncating.

How about just adding a second flag (instead of a constant)? The default value for the second flag could be 512 then.


Comments from Reviewable

@sougou-bot
Copy link
Copy Markdown

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


go/sqldb/sql_error.go, line 22 at r3 (raw file):

Previously, mberlin-bot (Michael Berlin) wrote…

I don't think you need this variable. It adds an extra indirection.

Instead, you can just export sqlErrorTruncateLen and use it directly.

I think what you mean here is define the flag locally here and use it internally? I don't think it needs to be exported out of here since nobody else reads the value.


Comments from Reviewable

@mberlin-bot
Copy link
Copy Markdown

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


go/sqldb/sql_error.go, line 22 at r3 (raw file):

Previously, sougou-bot (Sugu Sougoumarane) wrote…

I think what you mean here is define the flag locally here and use it internally? I don't think it needs to be exported out of here since nobody else reads the value.

Yes, SGTM.

What do you think about my other comment i.e. moving the 512 default value to a flag as well? Where should we define that flag?


Comments from Reviewable

@sougou-bot
Copy link
Copy Markdown

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


go/sqldb/sql_error.go, line 22 at r3 (raw file):

Previously, mberlin-bot (Michael Berlin) wrote…

Yes, SGTM.

What do you think about my other comment i.e. moving the 512 default value to a flag as well? Where should we define that flag?

I see that the value is used once in sqlparser and twice in tabletserver. If at all, maybe a constant in tabletserver and leave the one in sqlparser alone.


Comments from Reviewable

@mberlin-bot
Copy link
Copy Markdown

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


go/sqldb/sql_error.go, line 22 at r3 (raw file):

Previously, sougou-bot (Sugu Sougoumarane) wrote…

I see that the value is used once in sqlparser and twice in tabletserver. If at all, maybe a constant in tabletserver and leave the one in sqlparser alone.

It's also used in vtgate. Let's definitely make this a constant (ideally in go/vt/utils/utils.go) and not duplicate this magic value of 512.

Instead of a constant, we could also add a function for that.

// TruncateQueryForUI is used when displaying queries on various Vitess status pages.
// By truncating the query to 512 characters, we avoid that loading the page takes too long.
func TruncateQueryForUI(query string) string {
  return TruncateQuery(query, 512)
}

Comments from Reviewable

@demmer
Copy link
Copy Markdown
Member Author

demmer commented Apr 22, 2017

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


go/sqldb/sql_error.go, line 22 at r3 (raw file):

Previously, mberlin-bot (Michael Berlin) wrote…

It's also used in vtgate. Let's definitely make this a constant (ideally in go/vt/utils/utils.go) and not duplicate this magic value of 512.

Instead of a constant, we could also add a function for that.

// TruncateQueryForUI is used when displaying queries on various Vitess status pages.
// By truncating the query to 512 characters, we avoid that loading the page takes too long.
func TruncateQueryForUI(query string) string {
  return TruncateQuery(query, 512)
}

I had the same thought actually and did basically the same thing.

Will polish up the tests and make sure it covers the cases that have been biting us in production.


Comments from Reviewable

@demmer demmer force-pushed the truncate-long-queries-in-error-logs branch from 0014ee7 to a4543ce Compare April 22, 2017 05:31
@demmer
Copy link
Copy Markdown
Member Author

demmer commented Apr 22, 2017

Done


Review status: 0 of 12 files reviewed at latest revision, 4 unresolved discussions.


go/sqldb/sql_error.go, line 10 at r3 (raw file):

Previously, mberlin-bot (Michael Berlin) wrote…

Go readability: This must go in a separate group.

No longer relevant.


go/vt/vttablet/endtoend/misc_test.go, line 536 at r3 (raw file):

Previously, mberlin-bot (Michael Berlin) wrote…

Go readability: Surrounding parentheses are not needed.

It looks like you're skipping the Git precommit hook. I thought it would complain about this? Can you please double check?

I ended up moving around repositories to work with my VM and the symlinks were pointed at dead-ends.

However even after fixing them I don't see to be able to get one of these to fail.


go/vt/vttablet/tabletserver/query_engine.go, line 473 at r3 (raw file):

Previously, mberlin-bot (Michael Berlin) wrote…

The 512 should be a constant because you're using it in several places and somebody may change it in one place and forget about the others.

In the name of the constant you could reflect that the limit is meant as default for UI purposes.

In general, I think it makes sense to have two different values for UI truncating and log truncating.

How about just adding a second flag (instead of a constant)? The default value for the second flag could be 512 then.

I ended up adding two functions and separate command line args for each.


Comments from Reviewable

@demmer
Copy link
Copy Markdown
Member Author

demmer commented Apr 25, 2017

@sougou @michael-berlin When manually forcing various errors I realized my previous attempts didn't actually truncate as expected.

Reworked the implementation and verified this properly truncates errors from vttablet and vtgate.

@sougou
Copy link
Copy Markdown
Contributor

sougou commented Apr 25, 2017

I was thinking you could also use sqlparser.SplitTrailingComment (https://github.com/youtube/vitess/blob/master/go/vt/sqlparser/comments.go#L14), and then truncate the query part only.

@demmer
Copy link
Copy Markdown
Member Author

demmer commented Apr 25, 2017

@sougou That's a good idea... though perhaps for cleanliness I should move all the truncate functions into the sqlparser package as opposed to sqldb?

@sougou
Copy link
Copy Markdown
Contributor

sougou commented Apr 26, 2017

Yeah. That sounds like a good idea.

@demmer
Copy link
Copy Markdown
Member Author

demmer commented Apr 26, 2017

@sougou Updated as per your suggestion to preserve the comments.

Copy link
Copy Markdown
Contributor

@sougou sougou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good. Couple of minor comments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all these tests, it may be simpler and more robust to do equality comparison with the exact output we want.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to TruncateForLog?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in the other comment, you can build a wantVars variable and perform an equality comparison using reflect.DeepEqual.

@demmer demmer force-pushed the truncate-long-queries-in-error-logs branch from 1d09b58 to 223b887 Compare May 1, 2017 19:06
@demmer
Copy link
Copy Markdown
Member Author

demmer commented May 1, 2017

@sougou Updated with your suggestions.

@sougou sougou merged commit 33ee8b1 into vitessio:master May 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants