From 526f1eacaa7fe4414b050a01e586f7ffcd4a5d6d Mon Sep 17 00:00:00 2001 From: Josh Lee Date: Mon, 19 Sep 2022 12:38:09 -0400 Subject: [PATCH] Custom attributes and spans for FeatureFlag service (#371) --- CHANGELOG.md | 2 ++ docs/manual_span_attributes.md | 6 ++++-- docs/trace_service_features.md | 2 +- .../lib/featureflagservice/feature_flags.ex | 17 ++++++++++++++--- src/featureflagservice/src/ffs_service.erl | 4 ++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dd4e85713..8c3f524b55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,3 +93,5 @@ significant modifications will be credited to OpenTelemetry Authors. ([#345](https://github.com/open-telemetry/opentelemetry-demo/pull/345)) * Improve initial run time, without a build ([#362](https://github.com/open-telemetry/opentelemetry-demo/pull/362)) +* Add custom span and custom span attributes for Feature Flag Service +([#371](https://github.com/open-telemetry/opentelemetry-demo/pull/371)) diff --git a/docs/manual_span_attributes.md b/docs/manual_span_attributes.md index ef4114012a..f94e832ee6 100644 --- a/docs/manual_span_attributes.md +++ b/docs/manual_span_attributes.md @@ -51,8 +51,10 @@ This document contains the list of manual Span Attributes used throughout the de ## FeatureFlagService | Name | Type | Description | -|-----------|------|-------------| -| None yet | | | +|-------------------------------|---------|--------------------------| +| `app.featureflag.name` | string | Name of the feature flag | +| `app.featureflag.description` | string | Admin description | +| `app.featureflag.enabled` | boolean | The feature flag status | | ## Frontend diff --git a/docs/trace_service_features.md b/docs/trace_service_features.md index 5f2dab38ca..3bf5f17132 100644 --- a/docs/trace_service_features.md +++ b/docs/trace_service_features.md @@ -13,7 +13,7 @@ Emoji Legend | Checkout | Go | :100: | :100: | :100: | :no_bell: | :no_bell: | :no_bell: | | Currency | C++ | :no_bell: | :100: | :100: | :100: | :no_bell: | :no_bell: | | Email | Ruby | :100: | :100: | :100: | :no_bell: | :no_bell: | :no_bell: | -| Feature Flag | Erlang / Elixir | :100: | :construction: | :construction: | :no_bell: | :no_bell: | :no_bell: | +| Feature Flag | Erlang / Elixir | :100: | :100: | :100: | :no_bell: | :no_bell: | :no_bell: | | Frontend | JavaScript | :100: | :100: | :100: | :no_bell: | :100: | :100: | | Payment | JavaScript | :100: | :100: | :100: | :no_bell: | :no_bell: | :100: | | Product Catalog | Go | :100: | :no_bell: | :100: | :no_bell: | :no_bell: | :no_bell: | diff --git a/src/featureflagservice/lib/featureflagservice/feature_flags.ex b/src/featureflagservice/lib/featureflagservice/feature_flags.ex index 4f0e86932f..c9d38dbbfa 100644 --- a/src/featureflagservice/lib/featureflagservice/feature_flags.ex +++ b/src/featureflagservice/lib/featureflagservice/feature_flags.ex @@ -4,6 +4,9 @@ defmodule Featureflagservice.FeatureFlags do """ import Ecto.Query, warn: false + + require OpenTelemetry.Tracer + alias Featureflagservice.Repo alias Featureflagservice.FeatureFlags.FeatureFlag @@ -64,9 +67,17 @@ defmodule Featureflagservice.FeatureFlags do """ def create_feature_flag(attrs \\ %{}) do - %FeatureFlag{} - |> FeatureFlag.changeset(attrs) - |> Repo.insert() + {function_name, arity} = __ENV__.function + OpenTelemetry.Tracer.with_span "featureflagservice.featureflags.#{function_name}/#{arity}" do + OpenTelemetry.Tracer.set_attributes(%{ + "app.featureflag.name" => attrs["name"], + "app.featureflag.description" => attrs["description"], + "app.featureflag.enabled" => attrs["enabled"] + }) + %FeatureFlag{} + |> FeatureFlag.changeset(attrs) + |> Repo.insert() + end end @doc """ diff --git a/src/featureflagservice/src/ffs_service.erl b/src/featureflagservice/src/ffs_service.erl index a2e5680f40..fa47bf1b8c 100644 --- a/src/featureflagservice/src/ffs_service.erl +++ b/src/featureflagservice/src/ffs_service.erl @@ -10,6 +10,8 @@ -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}) -> @@ -22,6 +24,8 @@ get_flag(Ctx, #{name := Name}) -> 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),