-
Notifications
You must be signed in to change notification settings - Fork 78
feat: Add support for OpenTelemetry #551
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
Open
shikokuchuo
wants to merge
19
commits into
r-dbi:main
Choose a base branch
from
shikokuchuo:otel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9d66707
OpenTelemetry concept
shikokuchuo 2bbc16c
Add table creation, removal and queries
shikokuchuo 791e073
Adhere closer to semantic conventions
shikokuchuo d6213f6
Move otel to suggests and cache tracer on package load
shikokuchuo 631e53b
Refactor `otel_local_active_span()`
shikokuchuo d1ac9b2
Add testing infrastructure
shikokuchuo cca88e4
Add dbWriteTable, dbAppendTable, dbReadTable; handle names more robustly
shikokuchuo cfc7ec7
Rename some parameters
shikokuchuo fababe1
Merge branch 'main' into otel
krlmlr 28c98df
Use updated `otel_refresh_tracer()`
shikokuchuo 2f62ee7
Do not record query text
shikokuchuo 4288bc1
Use `INSERT INTO` for `dbAppendTable()`
shikokuchuo d0e5f6c
Implement `collection_name()` helper
shikokuchuo d2996e3
Use safe subsetting
shikokuchuo f009219
Simplify `modify_binding()` helper
shikokuchuo 989afe0
Simplify `otel_refresh_tracer()`
shikokuchuo c9296bf
Use local scope to cache tracer; add testing helper
shikokuchuo 2968b5d
Simplify `otel_cache_tracer()`
shikokuchuo 8f2db6b
Drop `otel::as_attributes()`
shikokuchuo 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,8 @@ Suggests: | |
| knitr, | ||
| magrittr, | ||
| nanoarrow (>= 0.3.0.1), | ||
| otel, | ||
| otelsdk, | ||
| RMariaDB, | ||
| rmarkdown, | ||
| rprojroot, | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| otel_tracer_name <- "org.r-dbi.DBI" | ||
|
|
||
| # Generic otel helpers: | ||
|
|
||
| otel_cache_tracer <- NULL | ||
| otel_local_active_span <- NULL | ||
|
|
||
| local({ | ||
| otel_tracer <- NULL | ||
| otel_is_tracing <- FALSE | ||
|
|
||
| otel_cache_tracer <<- function() { | ||
| requireNamespace("otel", quietly = TRUE) || return() | ||
| otel_tracer <<- otel::get_tracer(otel_tracer_name) | ||
| otel_is_tracing <<- tracer_enabled(otel_tracer) | ||
| } | ||
|
|
||
| otel_local_active_span <<- function( | ||
| name, | ||
| conn, | ||
| label = NULL, | ||
| attributes = NULL, | ||
| activation_scope = parent.frame() | ||
| ) { | ||
| otel_is_tracing || return() | ||
| dbname <- get_dbname(conn) | ||
| otel::start_local_active_span( | ||
| name = sprintf("%s %s", name, if (length(label)) label else dbname), | ||
| attributes = c(attributes, list(db.system.name = dbname)), | ||
| options = list(kind = "client"), | ||
| tracer = otel_tracer, | ||
| activation_scope = activation_scope | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| tracer_enabled <- function(tracer) { | ||
| .subset2(tracer, "is_enabled")() | ||
| } | ||
|
|
||
| with_otel_record <- function(expr) { | ||
| on.exit(otel_cache_tracer()) | ||
| otelsdk::with_otel_record({ | ||
| otel_cache_tracer() | ||
| expr | ||
| }) | ||
| } | ||
|
|
||
| # DBI-specific helpers: | ||
|
|
||
| get_dbname <- function(conn) { | ||
| dbname <- attr(class(conn), "package") | ||
| if (is.null(dbname)) "unknown" else dbname | ||
| } | ||
|
|
||
| collection_name <- function(name, conn) { | ||
| if (is.character(name)) name else dbQuoteIdentifier(conn, x = name) | ||
| } | ||
|
|
||
| make_query_attributes <- function(statement) { | ||
| query <- strsplit(statement, " ", fixed = TRUE)[[1L]] | ||
| list( | ||
| db.operation.name = query[1L], | ||
| db.collection.name = query[which(query == "FROM") + 1L] | ||
| ) | ||
| } |
This file contains hidden or 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 hidden or 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,45 @@ | ||
| test_that("OpenTelemetry tracing works", { | ||
| skip_if_not_installed("otelsdk") | ||
|
|
||
| record <- with_otel_record({ | ||
|
|
||
| con <- dbConnect(RSQLite::SQLite(), ":memory:") | ||
| dbWriteTable(con, "mtcars", mtcars) | ||
| dbGetQuery(con, "SELECT * FROM mtcars") | ||
| dbGetQuery( | ||
| con, | ||
| "SELECT COUNT(*) FROM mtcars WHERE cyl = ?", | ||
| params = list(1:8) | ||
| ) | ||
| dbReadTable(con, "mtcars") | ||
| dbRemoveTable(con, "mtcars") | ||
| dbDisconnect(con) | ||
| }) | ||
|
|
||
| traces <- record$traces | ||
|
|
||
| expect_length(traces, 10L) | ||
| expect_equal(traces[[1L]]$name, "dbConnect RSQLite") | ||
| expect_equal(traces[[1L]]$kind, "client") | ||
| expect_equal(traces[[1L]]$attributes$db.system.name, "RSQLite") | ||
| expect_equal(traces[[2L]]$name, "CREATE TABLE mtcars") | ||
| expect_equal(traces[[2L]]$attributes$db.collection.name, "mtcars") | ||
| expect_equal(traces[[2L]]$attributes$db.operation.name, "CREATE TABLE") | ||
| expect_equal(traces[[3L]]$name, "INSERT INTO mtcars") | ||
| expect_equal(traces[[3L]]$attributes$db.collection.name, "mtcars") | ||
| expect_equal(traces[[3L]]$attributes$db.operation.name, "INSERT INTO") | ||
| expect_equal(traces[[4L]]$name, "dbWriteTable mtcars") | ||
| expect_equal(traces[[4L]]$attributes$db.collection.name, "mtcars") | ||
| expect_equal(traces[[5L]]$name, "SELECT mtcars") | ||
| expect_equal(traces[[5L]]$attributes$db.collection.name, "mtcars") | ||
| expect_equal(traces[[5L]]$attributes$db.operation.name, "SELECT") | ||
| expect_equal(traces[[6L]]$name, "SELECT mtcars") | ||
| expect_equal(traces[[7L]]$name, "SELECT `mtcars`") | ||
| expect_equal(traces[[8L]]$name, "dbReadTable mtcars") | ||
| expect_equal(traces[[8L]]$attributes$db.collection.name, "mtcars") | ||
| expect_equal(traces[[9L]]$name, "DROP TABLE mtcars") | ||
| expect_equal(traces[[9L]]$attributes$db.collection.name, "mtcars") | ||
| expect_equal(traces[[9L]]$attributes$db.operation.name, "DROP TABLE") | ||
| expect_equal(traces[[10L]]$name, "dbDisconnect RSQLite") | ||
| expect_equal(traces[[10L]]$attributes$db.system.name, "RSQLite") | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.