功能标志服务

该服务是用Erlang/Elixir编写的,负责在PostgreSQL数据库中创建、读取、更新和删除功能标志。它被产品目录和运输服务调用。

功能标志服务源代码

追踪

初始化追踪

为了为PhoenixEcto设置OpenTelemetry工具,我们需要在启动Supervisor之前调用它们的工具包的设置方法。

application.ex文件中完成如下设置:

@impl true
  def start(_type, _args) do
    OpentelemetryEcto.setup([:featureflagservice, :repo])
    OpentelemetryPhoenix.setup()

    children = [
      # 启动Ecto仓库
      Featureflagservice.Repo,
      # 启动PubSub系统
      {Phoenix.PubSub, name: Featureflagservice.PubSub},
      # 启动Endpoint (http/https)
      FeatureflagserviceWeb.Endpoint
      # 通过调用以下方法启动一个Worker:Featureflagservice.Worker.start_link(arg)
      # {Featureflagservice.Worker, arg}
    ]

    # 其他策略和受支持选项请参阅https://hexdocs.pm/elixir/Supervisor.html
    opts = [strategy: :one_for_one, name: Featureflagservice.Supervisor]
    Supervisor.start_link(children, opts)
  end

要为grpcbox添加追踪功能,我们需要添加适当的拦截器interceptor

runtime.exs文件中配置如下:

config :grpcbox,
  servers: [
    %{
      :grpc_opts => %{
        :service_protos => [:ffs_demo_pb],
        :unary_interceptor => {:otel_grpcbox_interceptor, :unary},
        :services => %{:"oteldemo.FeatureFlagService" => :ffs_service}
      },
      :listen_opts => %{:port => grpc_port}
    }
  ]

为自动仪表化的跨度增加属性

通过在跨度对象上使用?set_attribute,可以为跨度添加属性。在get_flag函数中,给跨度添加了两个属性。

-include_lib("grpcbox/include/grpcbox.hrl").

-include_lib("opentelemetry_api/include/otel_tracer.hrl").

-spec get_flag(ctx:t(), ffs_demo_pb:get_flag_request()) ->
    {ok, ffs_demo_pb:get_flag_response(), ctx:t()} | grpcbox_stream:grpc_error_response().
get_flag(Ctx, #{name := Name}) ->
    case 'Elixir.Featureflagservice.FeatureFlags':get_feature_flag_by_name(Name) of
        nil ->
            {grpc_error, {?GRPC_STATUS_NOT_FOUND, <<"the requested feature flag does not exist">>}};
        #{'__struct__' := 'Elixir.Featureflagservice.FeatureFlags.FeatureFlag',
          description := Description,
          enabled := Enabled,
          inserted_at := CreatedAt,
          updated_at := UpdatedAt
         } ->
            ?set_attribute('app.featureflag.name', Name),
            ?set_attribute('app.featureflag.enabled', Enabled),
            {ok, Epoch} = 'Elixir.NaiveDateTime':from_erl({{1970, 1, 1}, {0, 0, 0}}),
            CreatedAtSeconds = 'Elixir.NaiveDateTime':diff(CreatedAt, Epoch),
            UpdatedAtSeconds = 'Elixir.NaiveDateTime':diff(UpdatedAt, Epoch),
            Flag = #{name => Name,
                     description => Description,
                     enabled => Enabled,
                     created_at => #{seconds => CreatedAtSeconds, nanos => 0},
                     updated_at => #{seconds => UpdatedAtSeconds, nanos => 0}},
            {ok, #{flag => Flag}, Ctx}
    end.

指标

待定

日志

待定

最后修改 December 13, 2023: improve glossary translation (46f8201b)