Skip to content

feat: add tracing for sql #1610

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

Merged
merged 17 commits into from
May 20, 2022
Merged

feat: add tracing for sql #1610

merged 17 commits into from
May 20, 2022

Conversation

f0ssel
Copy link
Contributor

@f0ssel f0ssel commented May 19, 2022

This adds tracing for the postgres data layer. It also continues to improve on the http tracing by using a lot more built-in functions from semconv.

Datadog example:
SQL -
image

HTTP -
image

@f0ssel f0ssel marked this pull request as ready for review May 19, 2022 23:43
@f0ssel f0ssel requested review from a team, mafredri and coadler May 20, 2022 00:13

sqlDriver, err = tracing.PostgresDriver(tracerProvider, "coderd.database")
if err != nil {
logger.Warn(cmd.Context(), "failed to start postgres tracing driver", slog.Error(err))
Copy link
Member

Choose a reason for hiding this comment

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

potential bug: If tracing.PostgresDriver encounters an error here, sqlDriver will be the empty string; I believe that would cause us to fail to connect to the DB. We should probably fall back to the original driver name in this case and degrade gracefully.

Suggested change
logger.Warn(cmd.Context(), "failed to start postgres tracing driver", slog.Error(err))
logger.Warn(cmd.Context(), "failed to start postgres tracing driver", slog.Error(err))
sqlDriver = "postgres"

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Very clean, and good catch with reassigning the request context. LGTM!

span.SetAttributes(semconv.NetAttributesFromHTTPRequest("tcp", r)...)
span.SetAttributes(semconv.EndUserAttributesFromHTTPRequest(r)...)
span.SetAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", route, r)...)
span.SetAttributes(semconv.HTTPRouteKey.String(route))
Copy link
Member

Choose a reason for hiding this comment

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

Nice change with the semconv package!

return driverName, nil
}

func formatPostgresSpan(ctx context.Context, op string) string {
Copy link
Member

Choose a reason for hiding this comment

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

Should we add the sql: prefix (or coder-db:), like in https://pkg.go.dev/github.com/nhatthm/otelsql#readme-span-name-formatter? Wondering if it'd help with narrowing down the spans.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just copying the format I've seen before with other database tracing tools (New Relic) and also mimicing the format of the http spans. Since an http span is METHOD route, I followed a similar format of OP name so the spans look like QUERY GetAllUsersById.

I don't love the prefix because I already can see where the span is coming from by the instrumentation library (see the screenshots in description), but I also don't know if this is the case on other tools like Jaeger, which I'd like to setup eventually. I'm down to change it if we find it hard to parse as we use it, does that sound ok?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that sounds perfectly reasonable. 👍🏻

return strings.ToUpper(op)
}

s := strings.Split(strings.TrimPrefix(q, qPrefix), " ")[0]
Copy link
Member

Choose a reason for hiding this comment

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

Just a small perf suggestion to avoid processing the entire query.

Suggested change
s := strings.Split(strings.TrimPrefix(q, qPrefix), " ")[0]
s := strings.SplitN(strings.TrimPrefix(q, qPrefix), " ", 1)[0]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

of course! I was only paying attention to the first line lol

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

This looks great! I just have one concern regarding the case where we fail to register the tracing driver.

@f0ssel f0ssel force-pushed the f0ssel/otel-sql branch from 2c344ee to a4c4683 Compare May 20, 2022 15:33
@f0ssel f0ssel merged commit 0effb71 into main May 20, 2022
@f0ssel f0ssel deleted the f0ssel/otel-sql branch May 20, 2022 15:51
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
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.

4 participants