Skip to content

Commit

Permalink
Merge pull request #627 from albertored/instrument-name-validation
Browse files Browse the repository at this point in the history
Update instrument name regex as per specs version 1.2.5
  • Loading branch information
Tristan Sloughter authored Sep 17, 2023
2 parents a673c0f + 44a6484 commit a5f1d52
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions apps/opentelemetry_experimental/src/otel_meter_default.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
-include_lib("opentelemetry_api_experimental/include/otel_metrics.hrl").
-include("otel_metrics.hrl").

-define(INSTRUMENT_NAME_REGEX, "^[A-Za-z]+[A-Za-z0-9/_.\-]{0,254}$").

-spec create_instrument(otel_meter:t(), otel_instrument:name(), otel_instrument:kind(), otel_meter:opts()) -> otel_instrument:t().
create_instrument(Meter, Name, Kind, Opts) ->
validate_name(Name),
Expand Down Expand Up @@ -68,17 +70,16 @@ scope({_, #meter{instrumentation_scope=Scope}}) ->
Scope.

validate_name(Name) when is_atom(Name) ->
Re = "^[A-Za-z]+[A-Za-z0-9_.\-]{0,62}$",
NameString = atom_to_list(Name),
case re:run(NameString, Re, [{capture, none}]) of
case re:run(NameString, ?INSTRUMENT_NAME_REGEX, [{capture, none}]) of
match ->
ok;
nomatch ->
?LOG_ERROR("Invalid instrument name, should be an atom matching '~s', but got '~s'", [NameString]),
?LOG_ERROR("Invalid instrument name, should be an atom matching '~s', but got '~s'", [?INSTRUMENT_NAME_REGEX, NameString]),
ok
end;
validate_name(Name) ->
?LOG_ERROR("Invalid instrument name, should be an atom matching '~s', but got ~p", [Name]),
?LOG_ERROR("Invalid instrument name, should be an atom matching '~s', but got ~p", [?INSTRUMENT_NAME_REGEX, Name]),
ok.
%%

Expand Down

0 comments on commit a5f1d52

Please sign in to comment.