Skip to content

Commit

Permalink
fix(exporter): Ensure that timestamp are in second and not millisecon…
Browse files Browse the repository at this point in the history
…ds (#2751)

* fix(exporter): Ensure that timestamp are in second and not milliseconds

Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>

* remove trailing line

Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>

* Adding a warning log

Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>

---------

Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
  • Loading branch information
thomaspoignant authored Dec 4, 2024
1 parent c4059d1 commit bafe9dc
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 9 deletions.
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"
}
]
}

0 comments on commit bafe9dc

Please sign in to comment.