-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update processor SDK, add middleware (#1742)
* update processor SDK - apply processor middleware to builtin processors - use config.Config for configuration map - take into account middleware parameters in tests * organize imports * processor changes - update processor SDK - set SchemaService for builtin processors - add support for "time" in field.convert processor * go generate * rename config.Config parameter names * use proper logger for builtin processors * update generator and processor-sdk * update processor-sdk
- Loading branch information
1 parent
a8d76dc
commit d6e83ce
Showing
70 changed files
with
614 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright © 2024 Meroxa, Inc. | ||
// | ||
// 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 ctxutil | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/conduitio/conduit/pkg/foundation/log" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
// processorIDCtxKey is used as the key when saving the processor ID in a context. | ||
type processorIDCtxKey struct{} | ||
|
||
// ContextWithProcessorID wraps ctx and returns a context that contains processor ID. | ||
func ContextWithProcessorID(ctx context.Context, processorID string) context.Context { | ||
return context.WithValue(ctx, processorIDCtxKey{}, processorID) | ||
} | ||
|
||
// ProcessorIDFromContext fetches the record processor ID from the context. If the | ||
// context does not contain a processor ID it returns nil. | ||
func ProcessorIDFromContext(ctx context.Context) string { | ||
processorID := ctx.Value(processorIDCtxKey{}) | ||
if processorID != nil { | ||
return processorID.(string) | ||
} | ||
return "" | ||
} | ||
|
||
// ProcessorIDLogCtxHook fetches the record processor ID from the context and if it | ||
// exists it adds the processorID to the log output. | ||
type ProcessorIDLogCtxHook struct{} | ||
|
||
// Run executes the log hook. | ||
func (h ProcessorIDLogCtxHook) Run(e *zerolog.Event, _ zerolog.Level, _ string) { | ||
p := ProcessorIDFromContext(e.GetCtx()) | ||
if p != "" { | ||
e.Str(log.ProcessorIDField, p) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright © 2022 Meroxa, Inc. | ||
// | ||
// 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 ctxutil | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/conduitio/conduit/pkg/foundation/log" | ||
"github.com/google/uuid" | ||
"github.com/matryer/is" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
func TestContextWithProcessorID_Success(t *testing.T) { | ||
is := is.New(t) | ||
|
||
ctx := context.Background() | ||
processorID := uuid.NewString() | ||
|
||
ctx = ContextWithProcessorID(ctx, processorID) | ||
got := ProcessorIDFromContext(ctx) | ||
|
||
is.Equal(processorID, got) | ||
} | ||
|
||
func TestContextWithProcessorID_Twice(t *testing.T) { | ||
is := is.New(t) | ||
|
||
ctx := context.Background() | ||
processorID := uuid.NewString() | ||
|
||
ctx = ContextWithProcessorID(ctx, "existing processor ID") | ||
ctx = ContextWithProcessorID(ctx, processorID) | ||
got := ProcessorIDFromContext(ctx) | ||
|
||
is.Equal(processorID, got) | ||
} | ||
|
||
func TestProcessorIDFromContext_Empty(t *testing.T) { | ||
is := is.New(t) | ||
|
||
ctx := context.Background() | ||
got := ProcessorIDFromContext(ctx) | ||
|
||
is.Equal("", got) | ||
} | ||
|
||
func TestProcessorIDLogCtxHook_Success(t *testing.T) { | ||
is := is.New(t) | ||
|
||
ctx := context.Background() | ||
processorID := uuid.NewString() | ||
|
||
ctx = ContextWithProcessorID(ctx, processorID) | ||
|
||
var logOutput bytes.Buffer | ||
logger := zerolog.New(&logOutput) | ||
e := logger.Info().Ctx(ctx) | ||
ProcessorIDLogCtxHook{}.Run(e, zerolog.InfoLevel, "") | ||
e.Send() | ||
|
||
is.Equal(fmt.Sprintf(`{"level":"info","%s":"%s"}`, log.ProcessorIDField, processorID)+"\n", logOutput.String()) | ||
} | ||
|
||
func TestProcessorIDLogCtxHook_EmptyCtx(t *testing.T) { | ||
is := is.New(t) | ||
|
||
ctx := context.Background() | ||
|
||
var logOutput bytes.Buffer | ||
logger := zerolog.New(&logOutput) | ||
e := logger.Info().Ctx(ctx) | ||
ProcessorIDLogCtxHook{}.Run(e, zerolog.InfoLevel, "") | ||
e.Send() | ||
|
||
is.Equal(`{"level":"info"}`+"\n", logOutput.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.