Skip to content

Commit aeea3c0

Browse files
committed
use innerEvent to encapsulate user data only when the tracing is enabled
this also supports allowing the user to decide whether to use the raw data to send Signed-off-by: laminar <fangtian@kubesphere.io>
1 parent 798fc07 commit aeea3c0

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

context/context.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ const (
5757
TestModeOn = "on"
5858
innerEventTypePrefix = "io.openfunction.function"
5959
tracingProviderSkywalking = "skywalking"
60+
RawData = Option("RawData") // This option controls the Send() function to send raw data
6061
)
6162

6263
type Runtime string
6364
type ResourceType string
65+
type Option string
6466

6567
type NativeContext interface {
6668
// GetNativeContext returns the Go native context object.
@@ -188,6 +190,9 @@ type Context interface {
188190

189191
// GetInnerEvent returns the InnerEvent.
190192
GetInnerEvent() InnerEvent
193+
194+
// ContextOptions returns the context's options.
195+
ContextOptions() ContextOption
191196
}
192197

193198
type Out interface {
@@ -229,6 +234,11 @@ type TracingConfig interface {
229234
GetBaggage() map[string]string
230235
}
231236

237+
type ContextOption interface {
238+
SetRawData(condition bool) ContextOption
239+
IsRawDataEnabled() bool
240+
}
241+
232242
type FunctionContext struct {
233243
mu sync.Mutex
234244
Name string `json:"name"`
@@ -252,6 +262,7 @@ type FunctionContext struct {
252262
podNamespace string
253263
daprClient dapr.Client
254264
mode string
265+
options map[Option]string
255266
}
256267

257268
type EventRequest struct {
@@ -363,7 +374,7 @@ func (ctx *FunctionContext) Send(outputName string, data []byte) ([]byte, error)
363374

364375
payload = data
365376

366-
if traceable(output.ComponentType) {
377+
if IsTracingProviderSkyWalking(ctx) && traceable(output.ComponentType) && !ctx.IsRawDataEnabled() {
367378
ie := NewInnerEvent(ctx)
368379
ie.MergeMetadata(ctx.GetInnerEvent())
369380
ie.SetUserData(data)
@@ -607,6 +618,37 @@ func (ctx *FunctionContext) GetOut() Out {
607618
return ctx.Out
608619
}
609620

621+
func (ctx *FunctionContext) setContextOption(key Option, value string) {
622+
ctx.mu.Lock()
623+
defer ctx.mu.Unlock()
624+
ctx.options[key] = value
625+
}
626+
627+
func (ctx *FunctionContext) getContextOption(key Option) string {
628+
if value, ok := ctx.options[key]; ok {
629+
return value
630+
} else {
631+
return ""
632+
}
633+
}
634+
635+
func (ctx *FunctionContext) ContextOptions() ContextOption {
636+
return ctx
637+
}
638+
639+
func (ctx *FunctionContext) SetRawData(enable bool) ContextOption {
640+
ctx.setContextOption(RawData, strconv.FormatBool(enable))
641+
return ctx
642+
}
643+
644+
func (ctx *FunctionContext) IsRawDataEnabled() bool {
645+
if enable, err := strconv.ParseBool(ctx.getContextOption(RawData)); err != nil {
646+
return false
647+
} else {
648+
return enable
649+
}
650+
}
651+
610652
func (o *FunctionOut) GetOut() *FunctionOut {
611653
return o
612654
}
@@ -810,13 +852,22 @@ func parseContext() (*FunctionContext, error) {
810852
clientGRPCPort = port
811853
}
812854

855+
// Initialize the context options
856+
newContextOptions(ctx)
857+
813858
return ctx, nil
814859
}
815860

816861
func NewFunctionOut() *FunctionOut {
817862
return &FunctionOut{}
818863
}
819864

865+
func newContextOptions(ctx *FunctionContext) {
866+
ctx.options = map[Option]string{
867+
RawData: "false",
868+
}
869+
}
870+
820871
// Convert queue binding event into cloud event format to add tracing metadata in the cloud event context.
821872
func traceable(t string) bool {
822873

@@ -886,3 +937,12 @@ func ConvertUserDataToBytes(data interface{}) []byte {
886937
return d
887938
}
888939
}
940+
941+
func IsTracingProviderSkyWalking(ctx RuntimeContext) bool {
942+
if ctx.HasPluginsTracingCfg() && ctx.GetPluginsTracingCfg().IsEnabled() &&
943+
ctx.GetPluginsTracingCfg().ProviderName() == TracingProviderSkywalking {
944+
return true
945+
}
946+
947+
return false
948+
}

0 commit comments

Comments
 (0)