Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ffs] - Allow setting initial feature flag values #1319

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .licenserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"# Copyright The OpenTelemetry Authors",
"# SPDX-License-Identifier: Apache-2.0"
],
"**/*.sql": [
"-- Copyright The OpenTelemetry Authors",
"-- SPDX-License-Identifier: Apache-2.0"
],
"**/{Dockerfile,Makefile}": [
"# Copyright The OpenTelemetry Authors",
"# SPDX-License-Identifier: Apache-2.0"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ release.
([#1313](https://github.com/open-telemetry/opentelemetry-demo/pull/1313))
* [accountingservice] Update dependencies and semconv
* ([#1316](https://github.com/open-telemetry/opentelemetry-demo/pull/1316))
* [featureflagservice] Allow setting initial feature flag values
([#1319](https://github.com/open-telemetry/opentelemetry-demo/pull/1319))

## 1.7.0

Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ services:
- POSTGRES_USER=ffs
- POSTGRES_DB=ffs
- POSTGRES_PASSWORD=ffs
volumes:
- ./src/ffs_postgres/10-ffs_schema.sql:/docker-entrypoint-initdb.d/10-ffs_schema.sql
- ./src/ffs_postgres/20-ffs_data.sql:/docker-entrypoint-initdb.d/20-ffs_data.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -d ffs -U ffs"]
interval: 10s
Expand Down
4 changes: 0 additions & 4 deletions pb/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package oteldemo;

option go_package = "genproto/oteldemo";
Expand Down Expand Up @@ -277,8 +275,6 @@ message Flag {
string name = 1;
string description = 2;
bool enabled = 3;
google.protobuf.Timestamp created_at = 4;
google.protobuf.Timestamp updated_at = 5;
}

message GetFlagRequest {
Expand Down
761 changes: 366 additions & 395 deletions src/accountingservice/genproto/oteldemo/demo.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/accountingservice/genproto/oteldemo/demo_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

761 changes: 366 additions & 395 deletions src/checkoutservice/genproto/oteldemo/demo.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/checkoutservice/genproto/oteldemo/demo_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ defmodule Featureflagservice.FeatureFlags do

## Examples

iex> get_feature_flag!(123)
iex> get_feature_flag!(foo)
%FeatureFlag{}

iex> get_feature_flag!(456)
iex> get_feature_flag!(bar)
** (Ecto.NoResultsError)

"""
def get_feature_flag!(id), do: Repo.get!(FeatureFlag, id)
def get_feature_flag!(name), do: Repo.get!(FeatureFlag, name)
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved

@doc """
Gets a single feature_flag by name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule Featureflagservice.FeatureFlags.FeatureFlag do
use Ecto.Schema
import Ecto.Changeset

schema "featureflags" do
@primary_key {:name, :string, autogenerate: false}
@derive {Phoenix.Param, key: :name}

schema "featureflags" do
field :description, :string
field :enabled, :float, default: 0.0
field :name, :string

timestamps()
end

@doc false
Expand Down
4 changes: 0 additions & 4 deletions src/featureflagservice/priv/repo/migrations/.formatter.exs

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions src/featureflagservice/priv/repo/seeds.exs

This file was deleted.

12 changes: 2 additions & 10 deletions src/featureflagservice/src/ffs_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ get_flag(Ctx, #{name := Name}) ->
{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
enabled := Enabled
} ->
RandomNumber = rand:uniform(100), % Generate a random number between 0 and 100
Probability = trunc(Enabled * 100), % Convert the Enabled value to a percentage
Expand All @@ -46,15 +44,9 @@ get_flag(Ctx, #{name := Name}) ->
?set_attribute('app.featureflag.raw_value', Enabled),
?set_attribute('app.featureflag.enabled', FlagEnabledValue),

{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 => FlagEnabledValue,
created_at => #{seconds => CreatedAtSeconds, nanos => 0},
updated_at => #{seconds => UpdatedAtSeconds, nanos => 0}},
enabled => FlagEnabledValue},

{ok, #{flag => Flag}, Ctx}
end.
Expand Down
13 changes: 13 additions & 0 deletions src/ffs_postgres/10-ffs_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Copyright The OpenTelemetry Authors
-- SPDX-License-Identifier: Apache-2.0

CREATE TABLE public.featureflags (
name character varying(255),
description character varying(255),
enabled double precision DEFAULT 0.0 NOT NULL
);

ALTER TABLE ONLY public.featureflags ADD CONSTRAINT featureflags_pkey PRIMARY KEY (name);

CREATE UNIQUE INDEX featureflags_name_index ON public.featureflags USING btree (name);

15 changes: 15 additions & 0 deletions src/ffs_postgres/20-ffs_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Copyright The OpenTelemetry Authors
-- SPDX-License-Identifier: Apache-2.0

-- Feature Flags created and initialized on startup
-- 'enabled' is a decimal value between 0 and 1 (inclusive)
-- 0.0 is always disabled
-- 1.0 is always enabled
-- All values between set a percentage chance on each request
-- example: 0.55 is enabled 55% of the time
INSERT INTO public.featureflags (name, description, enabled)
VALUES
('productCatalogFailure', 'Fail product catalog service on a specific product', 0),
('recommendationCache', 'Cache recommendations', 0),
('adServiceFailure', 'Fail ad service requests', 0),
('cartServiceFailure', 'Fail cart service requests', 0);
Loading
Loading