Skip to content

Commit 1795844

Browse files
authored
Merge pull request #50 from r-lib/apiv2
2 parents 3dc89fa + adf35b8 commit 1795844

File tree

11 files changed

+94
-57
lines changed

11 files changed

+94
-57
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Suggests:
3838
webfakes,
3939
withr
4040
Remotes:
41-
r-lib/otel
41+
r-lib/otel@apiv2
4242
Config/testthat/edition: 3
4343
URL: https://otelsdk.r-lib.org, https://github.com/r-lib/otelsdk
4444
Config/Needs/website: tidyverse/tidytemplate

R/span.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ span_new <- function(
44
attributes = NULL,
55
links = NULL,
66
options = NULL,
7-
scope = parent.frame(),
8-
activation_scope = parent.frame()
7+
scope = NULL,
8+
activation_scope = NULL
99
) {
1010
name <- name %||% default_span_name
1111
name <- as_string(name)
@@ -117,9 +117,19 @@ span_new <- function(
117117
invisible(self)
118118
},
119119

120-
activate = function(activation_scope = parent.frame()) {
120+
activate = function(
121+
activation_scope = parent.frame(),
122+
end_on_exit = FALSE
123+
) {
124+
force(end_on_exit)
121125
cscope <- ccall(otel_scope_start, self$xptr)
122-
defer(ccall(otel_scope_end, cscope), envir = activation_scope)
126+
defer(
127+
{
128+
ccall(otel_scope_end, cscope)
129+
if (end_on_exit) self$end(status_code = "auto")
130+
},
131+
envir = activation_scope
132+
)
123133
},
124134

125135
name = NULL

R/tracer.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ tracer_new <- function(
1616
self <- new_object(
1717
"otel_tracer",
1818
start_span = function(
19+
name = NULL,
20+
attributes = NULL,
21+
links = NULL,
22+
options = NULL
23+
) {
24+
span_new(
25+
self,
26+
name = name,
27+
attributes = attributes,
28+
links = links,
29+
options = options,
30+
scope = NULL,
31+
activation_scope = NULL
32+
)
33+
},
34+
start_local_active_span = function(
1935
name = NULL,
2036
attributes = NULL,
2137
links = NULL,

man/otelsdk-package.Rd

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-logger.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test_that("span_context", {
120120
expect_true(any(grepl("trace_id\\s*:\\s*0{32}", lns)))
121121
expect_true(any(grepl("span_id\\s*:\\s*0{16}", lns)))
122122

123-
sp1 <- trc$start_span("s")
123+
sp1 <- trc$start_local_active_span("s")
124124
lgr$info("span context test")
125125
lp$flush()
126126
sp1$end()
@@ -133,7 +133,7 @@ test_that("span_context", {
133133
)))
134134
expect_true(any(grepl(paste0("span_id\\s*:\\s*", spns[["s"]]$span_id), lns)))
135135

136-
sp2 <- trc$start_span("s2")
136+
sp2 <- trc$start_local_active_span("s2")
137137
lgr$info("span context test", span_context = sp2)
138138
lgr$flush()
139139
sp2$end()

tests/testthat/test-print.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_that("format.otel_attributes", {
5353

5454
test_that("format.otel_span_data, print.otel_span_data", {
5555
spns <- with_otel_record(function() {
56-
otel::start_span("s", tracer = "org.r-lib.otel")
56+
otel::start_local_active_span("s", tracer = "org.r-lib.otel")
5757
trc <- otel::get_tracer("org.r-lib.otel")
5858
})[["traces"]]
5959

@@ -65,7 +65,7 @@ test_that("format.otel_span_data, print.otel_span_data", {
6565

6666
test_that("format.otel_instrumentation_scope_data", {
6767
spns <- with_otel_record(function() {
68-
otel::start_span("s", tracer = "org.r-lib.otel")
68+
otel::start_local_active_span("s", tracer = "org.r-lib.otel")
6969
trc <- otel::get_tracer("org.r-lib.otel")
7070
})[["traces"]]
7171

@@ -80,7 +80,7 @@ test_that("format.otel_instrumentation_scope_data", {
8080
schema_url = "https://opentelemetry.io/schemas/1.13.0",
8181
attributes = list(foo = 1:5, bar = "that")
8282
)
83-
sp <- trc$start_span("s")
83+
sp <- trc$start_local_active_span("s")
8484
sp$end()
8585
spns <- tp$get_spans()
8686

tests/testthat/test-sessions.R

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,41 @@ test_that("sessions auto-close", {
88
}
99

1010
serial2 <- function() {
11-
otel::start_span("serial2")
11+
otel::start_local_active_span("serial2")
1212
}
1313

1414
serial <- function() {
15-
ser <- otel::start_span("serial")
15+
ser <- otel::start_local_active_span("serial")
1616
serial2()
1717
}
1818

1919
start_sess <- function() {
2020
opts <- new.env(parent = emptyenv())
21-
opts$otel_session <- otel::start_span("sess", scope = NULL)
22-
s1 <- otel::start_span("1")
21+
opts$otel_session <- otel::start_span("sess")
22+
otel::local_active_span(opts$otel_session)
23+
s1 <- otel::start_local_active_span("1")
2324
opts
2425
}
2526

2627
work_sess <- function(opts) {
2728
opts$otel_session$activate()
28-
sp2 <- otel::start_span("2")
29+
sp2 <- otel::start_local_active_span("2")
2930
serial()
3031
}
3132

3233
end_sess <- function(opts) {
3334
opts$otel_session$activate()
34-
sp3 <- otel::start_span("3")
35+
sp3 <- otel::start_local_active_span("3")
3536
opts$otel_session$end()
3637
}
3738

3839
spns <- with_otel_record({
3940
fun <- function() {
40-
spn0 <- otel::start_span("0")
41+
spn0 <- otel::start_local_active_span("0")
4142
opts <- start_sess()
42-
spn01 <- otel::start_span("01")
43+
spn01 <- otel::start_local_active_span("01")
4344
work_sess(opts)
44-
spn02 <- otel::start_span("02")
45+
spn02 <- otel::start_local_active_span("02")
4546
end_sess(opts)
4647
}
4748
fun()
@@ -72,40 +73,41 @@ test_that("sessions, suggested practices", {
7273
}
7374

7475
serial2 <- function() {
75-
otel::start_span("serial2")
76+
otel::start_local_active_span("serial2")
7677
}
7778

7879
serial <- function() {
79-
ser <- otel::start_span("serial")
80+
ser <- otel::start_local_active_span("serial")
8081
serial2()
8182
}
8283

8384
start_sess <- function() {
8485
opts <- new.env(parent = emptyenv())
85-
opts$otel_session <- otel::start_span("sess", scope = NULL)
86-
s1 <- otel::start_span("1")
86+
opts$otel_session <- otel::start_span("sess")
87+
otel::local_active_span(opts$otel_session)
88+
s1 <- otel::start_local_active_span("1")
8789
opts
8890
}
8991

9092
work_sess <- function(opts) {
9193
otel::local_active_span(opts$otel_session)
92-
sp2 <- otel::start_span("2")
94+
sp2 <- otel::start_local_active_span("2")
9395
serial()
9496
}
9597

9698
end_sess <- function(opts) {
9799
otel::local_active_span(opts$otel_session)
98-
sp3 <- otel::start_span("3")
100+
sp3 <- otel::start_local_active_span("3")
99101
opts$otel_session$end()
100102
}
101103

102104
spns <- with_otel_record({
103105
fun <- function() {
104-
spn0 <- otel::start_span("0")
106+
spn0 <- otel::start_local_active_span("0")
105107
opts <- start_sess()
106-
spn01 <- otel::start_span("01")
108+
spn01 <- otel::start_local_active_span("01")
107109
work_sess(opts)
108-
spn02 <- otel::start_span("02")
110+
spn02 <- otel::start_local_active_span("02")
109111
end_sess(opts)
110112
}
111113
fun()
@@ -128,13 +130,14 @@ test_that("sessions, suggested practices", {
128130

129131
test_that("nested sessions", {
130132
create_inner_span <- function(name, tracer) {
131-
tracer$start_span(name, scope = NULL)
133+
tracer$start_span(name)
132134
}
133135

134136
spans <- otelsdk::with_otel_record({
135137
trc <- otel::get_tracer("test")
136138

137-
outer <- trc$start_span("outer", scope = NULL, options = list(parent = NA))
139+
outer <- trc$start_span("outer", options = list(parent = NA))
140+
otel::local_active_span(outer)
138141

139142
# Emulate two "concurrent" operations that create session spans.
140143
inner1 <- create_inner_span("inner1", trc)

tests/testthat/test-span.R

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
test_that("named and unnamed spans", {
22
spns <- with_otel_record({
33
trc <- otel::get_tracer("mytracer")
4-
spn1 <- trc$start_span()
5-
spn2 <- trc$start_span("my")
4+
spn1 <- trc$start_local_active_span()
5+
spn2 <- trc$start_local_active_span("my")
66
spn2$end()
77
spn1$end()
88
})[["traces"]]
@@ -15,7 +15,7 @@ test_that("close span automatically", {
1515
spns <- with_otel_record({
1616
trc <- otel::get_tracer("mytracer")
1717
do <- function(name = NULL) {
18-
spn1 <- trc$start_span(name)
18+
spn1 <- trc$start_local_active_span(name)
1919
}
2020
do("1")
2121
do("2")
@@ -32,7 +32,7 @@ test_that("close span automatically, on error", {
3232
spns <- with_otel_record({
3333
trc <- otel::get_tracer("mytracer")
3434
do <- function(name = NULL) {
35-
spn1 <- trc$start_span(name)
35+
spn1 <- trc$start_local_active_span(name)
3636
stop("oops")
3737
}
3838
try(do("1"), silent = TRUE)
@@ -49,15 +49,15 @@ test_that("close span automatically, on error", {
4949
test_that("is_recording", {
5050
trc_prv <- tracer_provider_memory_new()
5151
trc <- trc_prv$get_tracer("mytracer")
52-
spn1 <- trc$start_span()
52+
spn1 <- trc$start_local_active_span()
5353
expect_true(spn1$is_recording())
5454
})
5555

5656
test_that("set_attribute", {
5757
spns <- with_otel_record({
5858
trc <- otel::get_tracer("mytracer")
59-
spn1 <- trc$start_span()
60-
spn2 <- trc$start_span("my")
59+
spn1 <- trc$start_local_active_span()
60+
spn2 <- trc$start_local_active_span("my")
6161
spn2$set_attribute("key", letters[1:3])
6262
spn1$set_attribute("key", "gone")
6363
spn2$end()
@@ -78,8 +78,8 @@ test_that("set_attribute", {
7878
test_that("add_event", {
7979
spns <- with_otel_record({
8080
trc <- otel::get_tracer("mytracer")
81-
spn1 <- trc$start_span()
82-
spn2 <- trc$start_span("my")
81+
spn1 <- trc$start_local_active_span()
82+
spn2 <- trc$start_local_active_span("my")
8383
spn2$add_event("ev", attributes = list(key = "value", key2 = 1:5))
8484
spn2$add_event("ev2", attributes = list(x = letters[1:4]))
8585
spn2$end()
@@ -103,7 +103,7 @@ test_that("set_status", {
103103
spns <- with_otel_record({
104104
trc <- otel::get_tracer("mytracer")
105105
do <- function() {
106-
spn1 <- trc$start_span()
106+
spn1 <- trc$start_local_active_span()
107107
spn1$set_status("Unset", description = "Testing preset Unset")
108108
}
109109
do()
@@ -117,7 +117,7 @@ test_that("set_status", {
117117
test_that("update_name", {
118118
spns <- with_otel_record({
119119
trc <- otel::get_tracer("mytracer")
120-
spn1 <- trc$start_span()
120+
spn1 <- trc$start_local_active_span()
121121
spn1$update_name("good")
122122
spn1$end()
123123
})[["traces"]]
@@ -131,7 +131,7 @@ test_that("record_exception", {
131131
error_obj <- base_error()
132132
spns <- with_otel_record({
133133
trc <- otel::get_tracer("mytracer")
134-
spn1 <- trc$start_span()
134+
spn1 <- trc$start_local_active_span()
135135
spn1$record_exception(error_obj)
136136
spn1$end()
137137
})[["traces"]]
@@ -173,8 +173,8 @@ test_that("format_exception", {
173173
test_that("create a root span", {
174174
spns <- with_otel_record({
175175
trc <- otel::get_tracer("mytracer")
176-
spn1 <- trc$start_span("1")
177-
spn2 <- trc$start_span("2", options = list(parent = NA))
176+
spn1 <- trc$start_local_active_span("1")
177+
spn2 <- trc$start_local_active_span("2", options = list(parent = NA))
178178
spn2$end()
179179
spn1$end()
180180
})[["traces"]]
@@ -188,7 +188,7 @@ test_that("get_context", {
188188
spid1 <- spid2 <- NULL
189189
spns <- with_otel_record(function() {
190190
trc <- otel::get_tracer()
191-
spn <- trc$start_span("1")
191+
spn <- trc$start_local_active_span("1")
192192
spid1 <<- spn$get_context()$get_span_id()
193193
spid2 <<- trc$get_active_span_context()$get_span_id()
194194
})[["traces"]]
@@ -202,15 +202,15 @@ test_that("get_context", {
202202
test_that("is_valid", {
203203
spns <- with_otel_record(function() {
204204
trc <- otel::get_tracer()
205-
spn <- trc$start_span("1")
205+
spn <- trc$start_local_active_span("1")
206206
expect_true(spn$is_valid())
207207
})[["traces"]]
208208
})
209209

210210
test_that("span_context", {
211211
spns <- with_otel_record(function() {
212212
trc <- otel::get_tracer()
213-
spn <- trc$start_span("1")
213+
spn <- trc$start_local_active_span("1")
214214
ctx <- spn$get_context()
215215
expect_true(ctx$is_valid())
216216
expect_snapshot(ctx$get_trace_flags())

tests/testthat/test-tracer-provider-file.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test_that("tracer_provider_file", {
77

88
tp <- tracer_provider_file_new()
99
trc <- tp$get_tracer()
10-
sp1 <- trc$start_span("s1")
10+
sp1 <- trc$start_local_active_span("s1")
1111
sp1$end()
1212
tp$flush()
1313

tests/testthat/test-tracer-provider-stdout.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test_that("writing to a file", {
99
on.exit(unlink(tmp), add = TRUE)
1010
tp <- tracer_provider_stdstream_new(list(output = tmp))
1111
trc <- tp$get_tracer()
12-
sp1 <- trc$start_span("testspan")
12+
sp1 <- trc$start_local_active_span("testspan")
1313
sp1$end()
1414
tp$flush()
1515

0 commit comments

Comments
 (0)