From 548da527e4199f62b5f53fd30e5dc1eaf30e9bcd Mon Sep 17 00:00:00 2001 From: Bryan Naegele Date: Mon, 22 Jul 2024 20:51:10 -0600 Subject: [PATCH] Add semconv stability opt-in fun --- .../lib/sem_conv.ex | 15 ++++++++++ .../mix.exs | 1 - .../src/opentelemetry_sem_conv.erl | 26 +++++++++++++++++ .../semantic_conventions/sem_conv_test.exs | 29 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 apps/opentelemetry_semantic_conventions/src/opentelemetry_sem_conv.erl create mode 100644 apps/opentelemetry_semantic_conventions/test/open_telemetry/semantic_conventions/sem_conv_test.exs diff --git a/apps/opentelemetry_semantic_conventions/lib/sem_conv.ex b/apps/opentelemetry_semantic_conventions/lib/sem_conv.ex index ca3e58da..4211434e 100644 --- a/apps/opentelemetry_semantic_conventions/lib/sem_conv.ex +++ b/apps/opentelemetry_semantic_conventions/lib/sem_conv.ex @@ -115,4 +115,19 @@ defmodule OpenTelemetry.SemConv do attributes of both stability levels will have two modules in this case. """ + + @typedoc """ + Stability opt-in value + """ + @type stability_option() :: String.t() + + @doc """ + List of stability opt-ins defined by the `OTEL_SEMCONV_STABILITY_OPT_IN` env var. + + Current valid options: + + * [http](migration-guide.md) + """ + @spec stability_opt_in() :: [stability_option()] + defdelegate stability_opt_in(), to: :opentelemetry_sem_conv end diff --git a/apps/opentelemetry_semantic_conventions/mix.exs b/apps/opentelemetry_semantic_conventions/mix.exs index 360a4e40..bf92ca10 100644 --- a/apps/opentelemetry_semantic_conventions/mix.exs +++ b/apps/opentelemetry_semantic_conventions/mix.exs @@ -100,6 +100,5 @@ defmodule OpenTelemetry.SemanticConventions.MixProject do "guides/attributes-registry/signalr.md" ] ++ Path.wildcard("guides/dotnet/*") ++ Path.wildcard("guides/mobile/*")) end) - |> IO.inspect() end end diff --git a/apps/opentelemetry_semantic_conventions/src/opentelemetry_sem_conv.erl b/apps/opentelemetry_semantic_conventions/src/opentelemetry_sem_conv.erl new file mode 100644 index 00000000..ae6ae001 --- /dev/null +++ b/apps/opentelemetry_semantic_conventions/src/opentelemetry_sem_conv.erl @@ -0,0 +1,26 @@ +-module(opentelemetry_sem_conv). + +-export([stability_opt_in/0]). + +-export_type([stability_option/0]). + +-type stability_option() :: binary(). + +-spec stability_opt_in() -> [stability_option()]. +stability_opt_in() -> + OptInsList = string:split(os:getenv("OTEL_SEMCONV_STABILITY_OPT_IN", ""), ","), + HttpStability = http_stability(OptInsList), + OptIns = [HttpStability], + lists:filter(fun(Opt) -> (Opt =/= undefined) end, OptIns). + +http_stability(OptIns) -> + Dup = lists:member("http/dup", OptIns), + Http = lists:member("http", OptIns), + case {Dup, Http} of + {true, _} -> + <<"http/dup">>; + {_, true} -> + <<"http">>; + _ -> + undefined + end. diff --git a/apps/opentelemetry_semantic_conventions/test/open_telemetry/semantic_conventions/sem_conv_test.exs b/apps/opentelemetry_semantic_conventions/test/open_telemetry/semantic_conventions/sem_conv_test.exs new file mode 100644 index 00000000..7620497d --- /dev/null +++ b/apps/opentelemetry_semantic_conventions/test/open_telemetry/semantic_conventions/sem_conv_test.exs @@ -0,0 +1,29 @@ +defmodule OpenTelemetry.SemConvTest do + use ExUnit.Case, async: false + + alias OpenTelemetry.SemConv + + describe "stability_opt_in" do + test "http" do + assert SemConv.stability_opt_in() == [] + + System.put_env("OTEL_SEMCONV_STABILITY_OPT_IN", "") + assert SemConv.stability_opt_in() == [] + + System.put_env("OTEL_SEMCONV_STABILITY_OPT_IN", "unsupported") + assert SemConv.stability_opt_in() == [] + + System.put_env("OTEL_SEMCONV_STABILITY_OPT_IN", "http") + assert SemConv.stability_opt_in() == ["http"] + + System.put_env("OTEL_SEMCONV_STABILITY_OPT_IN", "http/dup,http") + assert SemConv.stability_opt_in() == ["http/dup"] + + # dup takes precedence + System.put_env("OTEL_SEMCONV_STABILITY_OPT_IN", "http,http/dup") + assert SemConv.stability_opt_in() == ["http/dup"] + + System.delete_env("OTEL_SEMCONV_STABILITY_OPT_IN") + end + end +end