-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathffs_service.erl
72 lines (59 loc) · 2.99 KB
/
ffs_service.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
% Copyright The OpenTelemetry Authors
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
-module(ffs_service).
-behaviour(ffs_service_bhvr).
-export([get_flag/2,
create_flag/2,
update_flag/2,
list_flags/2,
delete_flag/2]).
-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
} ->
RandomNumber = rand:uniform(100), % Generate a random number between 0 and 100
Probability = trunc(Enabled * 100), % Convert the Enabled value to a percentage
FlagEnabledValue = RandomNumber =< Probability, % Determine if the random number falls within the probability range
?set_attribute('app.featureflag.name', Name),
?set_attribute('app.featureflag.raw_value', Enabled),
?set_attribute('app.featureflag.enabled', FlagEnabledValue),
Flag = #{name => Name,
description => Description,
enabled => FlagEnabledValue},
{ok, #{flag => Flag}, Ctx}
end.
-spec create_flag(ctx:t(), ffs_demo_pb:create_flag_request()) ->
{ok, ffs_demo_pb:create_flag_response(), ctx:t()} | grpcbox_stream:grpc_error_response().
create_flag(_Ctx, _) ->
{grpc_error, {?GRPC_STATUS_UNIMPLEMENTED, <<"use the web interface to create flags.">>}}.
-spec update_flag(ctx:t(), ffs_demo_pb:update_flag_request()) ->
{ok, ffs_demo_pb:update_flag_response(), ctx:t()} | grpcbox_stream:grpc_error_response().
update_flag(_Ctx, _) ->
{grpc_error, {?GRPC_STATUS_UNIMPLEMENTED, <<"use the web interface to update flags.">>}}.
-spec list_flags(ctx:t(), ffs_demo_pb:list_flags_request()) ->
{ok, ffs_demo_pb:list_flags_response(), ctx:t()} | grpcbox_stream:grpc_error_response().
list_flags(_Ctx, _) ->
{grpc_error, {?GRPC_STATUS_UNIMPLEMENTED, <<"use the web interface to view all flags.">>}}.
-spec delete_flag(ctx:t(), ffs_demo_pb:delete_flag_request()) ->
{ok, ffs_demo_pb:delete_flag_response(), ctx:t()} | grpcbox_stream:grpc_error_response().
delete_flag(_Ctx, _) ->
{grpc_error, {?GRPC_STATUS_UNIMPLEMENTED, <<"use the web interface to delete flags.">>}}.