Skip to content

Commit faeb4c4

Browse files
authored
fix: otp28 removes the ability to cache compiled regex in macros (#904)
the compiled regex is stored in a persistent term to not have to recompile on each tracestate creation or addition. Use of pterm also removes the need to pass the regexes around as either part of tracestate record or in the tracer.
1 parent 63f9952 commit faeb4c4

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

.github/workflows/erlang.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
runs-on: ${{ matrix.os }}
2323
strategy:
2424
matrix:
25-
otp_version: ["27.2", "26.2.5.6", "25.3.2.16"]
25+
otp_version: ["28.1", "27.2", "26.2.5.6"]
2626
rebar3_version: ["3.24.0"]
2727
os: [ubuntu-24.04]
2828
env:
@@ -91,7 +91,7 @@ jobs:
9191
runs-on: ${{ matrix.os }}
9292
strategy:
9393
matrix:
94-
otp_version: ["27.2"]
94+
otp_version: ["28.1"]
9595
rebar3_version: ["3.24.0"]
9696
os: [ubuntu-24.04]
9797
steps:
@@ -119,7 +119,7 @@ jobs:
119119
runs-on: ${{ matrix.os }}
120120
strategy:
121121
matrix:
122-
otp_version: ["27.2"]
122+
otp_version: ["28.1"]
123123
rebar3_version: ["3.24.0"]
124124
os: [ubuntu-24.04]
125125
steps:
@@ -152,7 +152,7 @@ jobs:
152152
name: Test SemConv on (OTP ${{ matrix.otp_version }}) and ${{ matrix.os }}
153153
strategy:
154154
matrix:
155-
otp_version: ["27.2", "25.3.2.16"]
155+
otp_version: ["28.1", "26.2.5.6"]
156156
rebar3_version: ["3.24.0"]
157157
os: [ubuntu-24.04]
158158
defaults:

apps/opentelemetry_api/src/otel_tracestate.erl

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,23 @@
4545
%% See https://www.w3.org/TR/trace-context/#tracestate-header
4646
%% for the limits and string requirements that make up the regexes
4747
-define(MAX_MEMBERS, 32).
48-
%% re:compile("^(([a-z][_0-9a-z\-\*\/]{0,255})|([a-z0-9][_0-9a-z-*/]{0,240}@[a-z][_0-9a-z-*/]{0,13}))$")
49-
-define(KEY_MP, {re_pattern,3,0,0,
50-
<<69,82,67,80,59,1,0,0,16,0,0,0,1,0,0,0,255,255,255,255,
51-
255,255,255,255,0,0,0,0,0,0,3,0,0,0,64,0,0,0,0,0,0,0,
52-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,131,0,247,
53-
27,133,0,84,0,1,133,0,76,0,2,110,0,0,0,0,0,0,0,0,0,0,
54-
0,0,254,255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,110,
55-
0,0,0,0,0,164,255,3,0,0,0,128,254,255,255,7,0,0,0,0,0,
56-
0,0,0,0,0,0,0,0,0,0,0,104,0,0,0,255,120,0,76,119,0,
57-
155,133,0,149,0,3,110,0,0,0,0,0,0,255,3,0,0,0,0,254,
58-
255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,110,0,0,0,0,
59-
0,164,255,3,0,0,0,128,254,255,255,7,0,0,0,0,0,0,0,0,0,
60-
0,0,0,0,0,0,0,109,0,0,0,240,29,64,110,0,0,0,0,0,0,0,0,
61-
0,0,0,0,254,255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
62-
110,0,0,0,0,0,164,255,3,0,0,0,128,254,255,255,7,0,0,0,
63-
0,0,0,0,0,0,0,0,0,0,0,0,0,104,0,0,0,13,120,0,149,120,
64-
0,239,25,120,0,247,0>>}).
65-
%% re:compile("^([ -+--<>-~]{0,255}[!-+--<>-~])$")
66-
-define(VALUE_MP, {re_pattern,1,0,0,
67-
<<69,82,67,80,152,0,0,0,16,0,0,0,1,0,0,0,255,255,255,
68-
255,255,255,255,255,0,0,0,0,0,0,1,0,0,0,64,0,0,0,0,0,
69-
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,131,0,
70-
84,27,133,0,76,0,1,110,0,0,0,0,255,239,255,223,255,
71-
255,255,255,255,255,255,127,0,0,0,0,0,0,0,0,0,0,0,0,0,
72-
0,0,0,104,0,0,0,255,110,0,0,0,0,254,239,255,223,255,
73-
255,255,255,255,255,255,127,0,0,0,0,0,0,0,0,0,0,0,0,0,
74-
0,0,0,120,0,76,25,120,0,84,0>>}).
48+
%%
49+
-define(KEY_MP, persistent_term:get({otel_tracestate, key_mp})).
50+
-define(VALUE_MP, persistent_term:get({otel_tracestate, value_mp})).
7551

7652
-define(IS_STRING, (is_atom(Key) orelse is_string(Key) orelse is_binary(Key))).
7753

7854
-spec new() -> t().
7955
new() ->
56+
%% api had no setup so we have to ensure the persistent term for tracestate
57+
%% regexes are setup each time `new' is called
58+
ensure_regex_pterms(),
59+
8060
#tracestate{members=[]}.
8161

8262
-spec new([{unicode:latin1_chardata(), unicode:latin1_chardata()}]) -> t().
8363
new(List) ->
64+
ensure_regex_pterms(),
8465
Members = [Element || {Key, Value}=Element <- List, is_valid(Key, Value)],
8566
#tracestate{members=Members}.
8667

@@ -119,6 +100,7 @@ update(Key, Value, Tracestate=#tracestate{members=TracestateList}) ->
119100
decode_header(undefined) ->
120101
new();
121102
decode_header(Value) ->
103+
ensure_regex_pterms(),
122104
#tracestate{members=parse_pairs(string:lexemes(Value, [$,]))}.
123105

124106
-spec encode_header(t()) -> unicode:latin1_binary().
@@ -130,6 +112,18 @@ encode_header(_) ->
130112

131113
%%
132114

115+
ensure_regex_pterms() ->
116+
try
117+
_ = ?KEY_MP
118+
catch
119+
error:badarg ->
120+
{ok, KeyMP} = re:compile("^(([a-z][_0-9a-z\-\*\/]{0,255})|([a-z0-9][_0-9a-z-*/]{0,240}@[a-z][_0-9a-z-*/]{0,13}))$"),
121+
{ok, ValueMP} = re:compile("^([ -+--<>-~]{0,255}[!-+--<>-~])$"),
122+
123+
persistent_term:put({otel_tracestate, key_mp}, KeyMP),
124+
persistent_term:put({otel_tracestate, value_mp}, ValueMP)
125+
end.
126+
133127
is_valid(Key, Value) ->
134128
try
135129
re:run(Key, ?KEY_MP, [{capture, none}]) =:= match

0 commit comments

Comments
 (0)