Skip to content

Commit

Permalink
Merge branch 'main' into issue-2755
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Dec 6, 2024
2 parents b790403 + f12c9a2 commit cdb2da8
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/close-stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
days-before-pr-stale: 45
days-before-issue-close: 10
days-before-pr-close: 10
exempt-issue-labels: p1,p2,p3,bug,dependencies
exempt-issue-labels: p1,p2,p3,bug,dependencies,roadmap
exempt-assignees: thomaspoignant
2 changes: 1 addition & 1 deletion cmd/relayproxy/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *Server) initRoutes() {
cAllFlags := controller.NewAllFlags(s.services.GOFeatureFlagService, s.services.Metrics)
cFlagEval := controller.NewFlagEval(s.services.GOFeatureFlagService, s.services.Metrics)
cFlagEvalOFREP := ofrep.NewOFREPEvaluate(s.services.GOFeatureFlagService, s.services.Metrics)
cEvalDataCollector := controller.NewCollectEvalData(s.services.GOFeatureFlagService, s.services.Metrics)
cEvalDataCollector := controller.NewCollectEvalData(s.services.GOFeatureFlagService, s.services.Metrics, s.zapLog)
cRetrieverRefresh := controller.NewForceFlagsRefresh(s.services.GOFeatureFlagService, s.services.Metrics)
cFlagChangeAPI := controller.NewAPIFlagChange(s.services.GOFeatureFlagService, s.services.Metrics)

Expand Down
18 changes: 16 additions & 2 deletions cmd/relayproxy/controller/collect_eval_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"fmt"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
ffclient "github.com/thomaspoignant/go-feature-flag"
Expand All @@ -11,18 +12,21 @@ import (
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/model"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
)

type collectEvalData struct {
goFF *ffclient.GoFeatureFlag
metrics metric.Metrics
logger *zap.Logger
}

// NewCollectEvalData initialize the controller for the /data/collector endpoint
func NewCollectEvalData(goFF *ffclient.GoFeatureFlag, metrics metric.Metrics) Controller {
func NewCollectEvalData(goFF *ffclient.GoFeatureFlag, metrics metric.Metrics, logger *zap.Logger) Controller {
return &collectEvalData{
goFF: goFF,
metrics: metrics,
logger: logger,
}
}

Expand Down Expand Up @@ -51,7 +55,6 @@ func (h *collectEvalData) Handler(c echo.Context) error {
if reqBody == nil || reqBody.Events == nil {
return echo.NewHTTPError(http.StatusBadRequest, "collectEvalData: invalid input data")
}

tracer := otel.GetTracerProvider().Tracer(config.OtelTracerName)
_, span := tracer.Start(c.Request().Context(), "collectEventData")
defer span.End()
Expand All @@ -60,6 +63,17 @@ func (h *collectEvalData) Handler(c echo.Context) error {
if event.Source == "" {
event.Source = "PROVIDER_CACHE"
}
// force the creation date to be a unix timestamp
if event.CreationDate > 9999999999 {
h.logger.Warn(
"creationDate received is in milliseconds, we convert it to seconds",
zap.Int64("creationDate", event.CreationDate))
// if we receive a timestamp in milliseconds, we convert it to seconds
// but since it is totally possible to have a timestamp in seconds that is bigger than 9999999999
// we will accept timestamp up to 9999999999 (2286-11-20 18:46:39 +0100 CET)
event.CreationDate, _ = strconv.ParseInt(
strconv.FormatInt(event.CreationDate, 10)[:10], 10, 64)
}
h.goFF.CollectEventData(event)
}

Expand Down
19 changes: 17 additions & 2 deletions cmd/relayproxy/controller/collect_eval_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/controller"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
"github.com/thomaspoignant/go-feature-flag/retriever/fileretriever"
"go.uber.org/zap"
)

func Test_collect_eval_data_Handler(t *testing.T) {
Expand Down Expand Up @@ -86,6 +88,17 @@ func Test_collect_eval_data_Handler(t *testing.T) {
errorCode: http.StatusBadRequest,
},
},
{
name: "be sure that the creation date is a unix timestamp",
args: args{
"../testdata/controller/collect_eval_data/valid_request_with_timestamp_ms.json",
},
want: want{
httpCode: http.StatusOK,
bodyFile: "../testdata/controller/collect_eval_data/valid_response.json",
collectedDataFile: "../testdata/controller/collect_eval_data/valid_collected_data_with_timestamp_ms.json",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -107,7 +120,9 @@ func Test_collect_eval_data_Handler(t *testing.T) {
Exporter: &fileexporter.Exporter{Filename: exporterFile.Name()},
},
})
ctrl := controller.NewCollectEvalData(goFF, metric.Metrics{})
logger, err := zap.NewDevelopment()
require.NoError(t, err)
ctrl := controller.NewCollectEvalData(goFF, metric.Metrics{}, logger)

e := echo.New()
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -154,7 +169,7 @@ func Test_collect_eval_data_Handler(t *testing.T) {
assert.NoError(t, err, "Impossible the expected wantBody file %s", tt.want.bodyFile)
assert.Equal(t, tt.want.httpCode, rec.Code, "Invalid HTTP Code")
assert.JSONEq(t, string(wantBody), replacedStr, "Invalid response wantBody")
assert.Equal(t, string(wantCollectData), string(exportedData), "Invalid exported data")
assert.JSONEq(t, string(wantCollectData), string(exportedData), "Invalid exported data")
})
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{"kind":"feature","contextKind":"user","userKey":"94a25909-20d8-40cc-8500-fee99b569345","creationDate":1680246000011,"key":"my-feature-flag","variation":"admin-variation","value":"string","default":false,"version":"v1.0.0","source":"EDGE"}
{
"kind": "feature",
"contextKind": "user",
"userKey": "94a25909-20d8-40cc-8500-fee99b569345",
"creationDate": 1680246000,
"key": "my-feature-flag",
"variation": "admin-variation",
"value": "string",
"default": false,
"version": "v1.0.0",
"source": "EDGE"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"events": [
{
"contextKind": "user",
"creationDate": 1680246000011,
"creationDate": 1680246000,
"default": false,
"key": "my-feature-flag",
"kind": "feature",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{"kind":"feature","contextKind":"user","userKey":"94a25909-20d8-40cc-8500-fee99b569345","creationDate":1680246000011,"key":"my-feature-flag","variation":"admin-variation","value":"string","default":false,"version":"v1.0.0","source":"PROVIDER_CACHE"}
{
"kind": "feature",
"contextKind": "user",
"userKey": "94a25909-20d8-40cc-8500-fee99b569345",
"creationDate": 1680246000,
"key": "my-feature-flag",
"variation": "admin-variation",
"value": "string",
"default": false,
"version": "v1.0.0",
"source": "PROVIDER_CACHE"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"kind": "feature",
"contextKind": "user",
"userKey": "94a25909-20d8-40cc-8500-fee99b569345",
"creationDate": 1733230547,
"key": "my-feature-flag",
"variation": "admin-variation",
"value": "string",
"default": false,
"version": "v1.0.0",
"source": "PROVIDER_CACHE"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"events": [
{
"contextKind": "user",
"creationDate": 1680246000011,
"creationDate": 1680246000,
"default": false,
"key": "my-feature-flag",
"kind": "feature",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"events": [
{
"contextKind": "user",
"creationDate": 1733230547728,
"default": false,
"key": "my-feature-flag",
"kind": "feature",
"userKey": "94a25909-20d8-40cc-8500-fee99b569345",
"value": "string",
"variation": "admin-variation",
"version": "v1.0.0"
}
]
}
16 changes: 8 additions & 8 deletions examples/openfeature_web/webapp/package-lock.json

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

2 changes: 1 addition & 1 deletion examples/openfeature_web/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.7.0",
"browser-sync": "^3.0.3",
"eslint": "^9.15.0",
"eslint": "^9.16.0",
"eslint-webpack-plugin": "^4.2.0",
"htmlnano": "^2.1.1",
"imagemin-cli": "^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.32.5
github.com/aws/aws-sdk-go-v2/config v1.28.5
github.com/aws/aws-sdk-go-v2/credentials v1.17.46
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.40
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.41
github.com/aws/aws-sdk-go-v2/service/kinesis v1.32.6
github.com/aws/aws-sdk-go-v2/service/s3 v1.69.0
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3/go.mod h1:uk1vhHHERfSVCUnq
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20 h1:sDSXIrlsFSFJtWKLQS4PUWRvrT580rrnuLydJrCQ/yA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20/go.mod h1:WZ/c+w0ofps+/OUqMwWgnfrgzZH1DZO1RIkktICsqnY=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.3/go.mod h1:0dHuD2HZZSiwfJSy1FO5bX1hQ1TxVV1QXXjpn3XUE44=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.40 h1:CbalQNEYQljzAJ+3beY8FQBShdLNLpJzHL4h/5LSFMc=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.40/go.mod h1:1iYVr/urNWuZ7WZ1829FSE7RRTaXvzFdwrEQV8Z40cE=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.41 h1:hqcxMc2g/MwwnRMod9n6Bd+t+9Nf7d5qRg7RaXKPd6o=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.41/go.mod h1:d1eH0VrttvPmrCraU68LOyNdu26zFxQFjrVSb5vdhog=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9/go.mod h1:AnVH5pvai0pAF4lXRq0bmhbes1u9R8wTE+g+183bZNM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.24 h1:4usbeaes3yJnCFC7kfeyhkdkPtoRYPa/hTmCqMpKpLI=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.24/go.mod h1:5CI1JemjVwde8m2WG3cz23qHKPOxbpkq0HaoreEgLIY=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DataCollectorHook<T>(private val collectorManager: DataCollectorManager) :
) {
val event = Event(
contextKind = "user",
creationDate = Date().time,
creationDate = Date().time / 1000L,
key = ctx.flagKey,
kind = "feature",
userKey = ctx.ctx?.getTargetingKey(),
Expand All @@ -29,7 +29,7 @@ class DataCollectorHook<T>(private val collectorManager: DataCollectorManager) :
override fun error(ctx: HookContext<T>, error: Exception, hints: Map<String, Any>) {
val event = Event(
contextKind = "user",
creationDate = Date().time,
creationDate = Date().time / 1000L,
key = ctx.flagKey,
kind = "feature",
userKey = ctx.ctx?.getTargetingKey(),
Expand All @@ -40,4 +40,4 @@ class DataCollectorHook<T>(private val collectorManager: DataCollectorManager) :
)
collectorManager.addEvent(event)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GoFeatureFlagApiTest {
private var defaultEventList: List<Event> = listOf(
Event(
contextKind = "contextKind",
creationDate = 1721650841108,
creationDate = 1721650841,
key = "flag-1",
kind = "feature",
userKey = "981f2662-1fb4-4732-ac6d-8399d9205aa9",
Expand Down Expand Up @@ -140,4 +140,4 @@ class GoFeatureFlagApiTest {
val got = recordedRequest.body.readUtf8()
JSONAssert.assertEquals(want, got, false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"events": [
{
"contextKind": "contextKind",
"creationDate": 1721650841108,
"creationDate": 1721650841,
"key": "flag-1",
"kind": "feature",
"userKey": "981f2662-1fb4-4732-ac6d-8399d9205aa9",
Expand All @@ -11,7 +11,7 @@
"variation": "enabled"
}
],
"metadata": {
"meta": {
"provider": "android",
"openfeature": "true"
}
Expand Down
2 changes: 1 addition & 1 deletion website/static/sdk-versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"maven":{"sdk":"1.12.2","providerKt":"0.1.0","providerJava":"0.3.0","android":"0.3.0"},"npm":{"core":"1.5.0","serverSDK":"1.16.2","providerServer":"0.7.3","providerWeb":"0.2.1"},"pypi":{"sdk":"0.7.2","provider":"0.2.1"},"nuget":{"sdk":"2.1.0","provider":"0.2.0"},"go":{"provider":"v0.2.1","sdk":"v1.13.1"}}
{"maven":{"sdk":"1.12.2","providerKt":"0.1.0","providerJava":"0.4.0","android":"0.3.0"},"npm":{"core":"1.5.0","serverSDK":"1.16.2","webSDK":"1.3.2","providerWeb":"0.2.1"},"pypi":{"sdk":"0.7.4","provider":"0.3.0"},"nuget":{"sdk":"2.1.0","provider":"0.2.0"},"go":{"provider":"v0.2.1","sdk":"v1.13.1"}}

0 comments on commit cdb2da8

Please sign in to comment.