diff --git a/fs/instance_impl.go b/fs/instance_impl.go index 46708e75..44080a58 100644 --- a/fs/instance_impl.go +++ b/fs/instance_impl.go @@ -23,7 +23,6 @@ import ( "github.com/functionstream/function-stream/fs/api" "github.com/functionstream/function-stream/fs/contube" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "log/slog" ) @@ -37,6 +36,8 @@ type FunctionInstanceImpl struct { index int32 } +type CtxKey string + type DefaultInstanceFactory struct{} func NewDefaultInstanceFactory() api.FunctionInstanceFactory { @@ -45,10 +46,8 @@ func NewDefaultInstanceFactory() api.FunctionInstanceFactory { func (f *DefaultInstanceFactory) NewFunctionInstance(definition *model.Function, sourceFactory contube.SourceTubeFactory, sinkFactory contube.SinkTubeFactory, index int32) api.FunctionInstance { ctx, cancelFunc := context.WithCancel(context.Background()) - ctx.Value(logrus.Fields{ - "function-name": definition.Name, - "function-index": index, - }) + ctx = context.WithValue(ctx, CtxKey("function-name"), definition.Name) + ctx = context.WithValue(ctx, CtxKey("function-index"), index) return &FunctionInstanceImpl{ ctx: ctx, cancelFunc: cancelFunc, diff --git a/fs/instance_impl_test.go b/fs/instance_impl_test.go new file mode 100644 index 00000000..34d409bd --- /dev/null +++ b/fs/instance_impl_test.go @@ -0,0 +1,44 @@ +/* + * Copyright 2024 Function Stream Org. + * + * 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. + */ + +package fs + +import ( + "github.com/functionstream/function-stream/common/model" + "testing" +) + +func TestFunctionInstanceContextSetting(t *testing.T) { + defaultInstanceFactory := DefaultInstanceFactory{} + definition := &model.Function{ + Name: "test-function", + } + index := int32(1) + instance := defaultInstanceFactory.NewFunctionInstance(definition, nil, nil, index) + + if instance == nil { + t.Error("FunctionInstance should not be nil") + } + + if ctxValue, ok := instance.Context().Value(CtxKey("function-name")).(string); !ok || ctxValue != definition.Name { + t.Errorf("Expected 'function-name' in ctx to be '%s'", definition.Name) + } + + if ctxValue, ok := instance.Context().Value(CtxKey("function-index")).(int32); !ok || ctxValue != index { + t.Errorf("Expected 'function-index' in ctx to be '%d'", index) + } + +} diff --git a/go.mod b/go.mod index fbaf9996..542c8cdc 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/bmizerany/perks v0.0.0-20230307044200-03f9df79da1e github.com/gorilla/mux v1.8.1 github.com/pkg/errors v0.9.1 - github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 github.com/tetratelabs/wazero v1.6.0 golang.org/x/net v0.21.0 @@ -53,6 +52,7 @@ require ( github.com/prometheus/common v0.46.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect go.uber.org/atomic v1.11.0 // indirect