Skip to content

Commit 75f6d81

Browse files
authored
fix: Ensure channel_name doesn't cause invalid identifiers (#4)
1 parent bec4711 commit 75f6d81

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ impl InstrumentationConfig {
7373
function_query,
7474
}
7575
}
76+
77+
#[must_use]
78+
pub fn get_identifier_name(&self) -> String {
79+
self.channel_name
80+
.chars()
81+
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
82+
.collect()
83+
}
7684
}
7785

7886
#[cfg_attr(

src/instrumentation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Instrumentation {
6363
}
6464

6565
fn create_tracing_channel(&self) -> Stmt {
66-
let ch_str = ident!(format!("tr_ch_apm${}", self.config.channel_name));
66+
let ch_str = ident!(format!("tr_ch_apm${}", self.config.get_identifier_name()));
6767
let channel_string = Expr::Lit(Lit::Str(Str {
6868
span: Span::default(),
6969
value: format!(
@@ -95,10 +95,11 @@ impl Instrumentation {
9595

9696
let traced_fn = self.new_fn(original_body);
9797

98-
let ch_ident = ident!(format!("tr_ch_apm${}", &self.config.channel_name));
98+
let id_name = self.config.get_identifier_name();
99+
let ch_ident = ident!(format!("tr_ch_apm${}", &id_name));
99100
let trace_ident = ident!(format!(
100101
"tr_ch_apm${}.{}",
101-
&self.config.channel_name,
102+
&id_name,
102103
self.config.function_query.kind().tracing_operator()
103104
));
104105

@@ -120,8 +121,9 @@ impl Instrumentation {
120121

121122
let original_stmts = std::mem::take(&mut body.stmts);
122123

123-
let ch_ident = ident!(format!("tr_ch_apm${}", &self.config.channel_name));
124-
let ctx_ident = ident!(format!("tr_ch_apm_ctx${}", &self.config.channel_name));
124+
let id_name = self.config.get_identifier_name();
125+
let ch_ident = ident!(format!("tr_ch_apm${}", &id_name));
126+
let ctx_ident = ident!(format!("tr_ch_apm_ctx${}", &id_name));
125127
let mut try_catch = quote!(
126128
"try {
127129
if ($ch.hasSubscribers) {

tests/class_method_cjs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn class_method_cjs() {
77
file!(),
88
false,
99
Config::new_single(InstrumentationConfig::new(
10-
"Undici_fetch",
10+
"Undici:fetch",
1111
test_module_matcher(),
1212
FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async),
1313
)),

tests/class_method_cjs/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
**/
55
const Undici = require('./instrumented.js');
66
const { assert, getContext } = require('../common/preamble.js');
7-
const context = getContext('orchestrion:undici:Undici_fetch');
7+
const context = getContext('orchestrion:undici:Undici:fetch');
88
(async () => {
99
const undici = new Undici;
1010
const result = await undici.fetch('https://example.com');

tests/wasm/testdata/expected.mjs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
import { tracingChannel as tr_ch_apm_tracingChannel } from "diagnostics_channel";
2-
const tr_ch_apm$up:fetch = tr_ch_apm_tracingChannel("orchestrion:one:up:fetch");
3-
const tr_ch_apm$up:constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:constructor");
2+
const tr_ch_apm$up_fetch = tr_ch_apm_tracingChannel("orchestrion:one:up:fetch");
3+
const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:constructor");
44
export class Up {
55
constructor(){
6-
const tr_ch_apm_ctx$up:constructor = {
6+
const tr_ch_apm_ctx$up_constructor = {
77
arguments
88
};
99
try {
10-
if (tr_ch_apm$up:constructor.hasSubscribers) {
11-
tr_ch_apm$up:constructor.start.publish(tr_ch_apm_ctx$up:constructor);
10+
if (tr_ch_apm$up_constructor.hasSubscribers) {
11+
tr_ch_apm$up_constructor.start.publish(tr_ch_apm_ctx$up_constructor);
1212
}
1313
console.log('constructor');
1414
} catch (tr_ch_err) {
15-
if (tr_ch_apm$up:constructor.hasSubscribers) {
16-
tr_ch_apm_ctx$up:constructor.error = tr_ch_err;
15+
if (tr_ch_apm$up_constructor.hasSubscribers) {
16+
tr_ch_apm_ctx$up_constructor.error = tr_ch_err;
1717
try {
18-
tr_ch_apm_ctx$up:constructor.self = this;
18+
tr_ch_apm_ctx$up_constructor.self = this;
1919
} catch (refErr) {}
20-
tr_ch_apm$up:constructor.error.publish(tr_ch_apm_ctx$up:constructor);
20+
tr_ch_apm$up_constructor.error.publish(tr_ch_apm_ctx$up_constructor);
2121
}
2222
throw tr_ch_err;
2323
} finally{
24-
if (tr_ch_apm$up:constructor.hasSubscribers) {
25-
tr_ch_apm_ctx$up:constructor.self = this;
26-
tr_ch_apm$up:constructor.end.publish(tr_ch_apm_ctx$up:constructor);
24+
if (tr_ch_apm$up_constructor.hasSubscribers) {
25+
tr_ch_apm_ctx$up_constructor.self = this;
26+
tr_ch_apm$up_constructor.end.publish(tr_ch_apm_ctx$up_constructor);
2727
}
2828
}
2929
}
3030
fetch() {
3131
const traced = ()=>{
3232
console.log('fetch');
3333
};
34-
if (!tr_ch_apm$up:fetch.hasSubscribers) return traced();
35-
return tr_ch_apm$up:fetch.traceSync(traced, {
34+
if (!tr_ch_apm$up_fetch.hasSubscribers) return traced();
35+
return tr_ch_apm$up_fetch.traceSync(traced, {
3636
arguments,
3737
self: this
3838
});

0 commit comments

Comments
 (0)