Skip to content

Commit

Permalink
[pkg/telemetryquerylanguage] Update contexts to use StandardGetSetter (
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth authored Aug 15, 2022
1 parent a5b841e commit 504e59f
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 362 deletions.
166 changes: 76 additions & 90 deletions pkg/telemetryquerylanguage/contexts/tqllogs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@ func (ctx LogTransformContext) GetResource() pcommon.Resource {
return ctx.Resource
}

// pathGetSetter is a getSetter which has been resolved using a path expression provided by a user.
type pathGetSetter struct {
getter tql.ExprFunc
setter func(ctx tql.TransformContext, val interface{})
}

func (path pathGetSetter) Get(ctx tql.TransformContext) interface{} {
return path.getter(ctx)
}

func (path pathGetSetter) Set(ctx tql.TransformContext, val interface{}) {
path.setter(ctx, val)
}

var symbolTable = map[tql.EnumSymbol]tql.Enum{
"SEVERITY_NUMBER_UNSPECIFIED": tql.Enum(plog.SeverityNumberUNDEFINED),
"SEVERITY_NUMBER_TRACE": tql.Enum(plog.SeverityNumberTRACE),
Expand Down Expand Up @@ -165,12 +151,12 @@ func newPathGetSetter(path []tql.Field) (tql.GetSetter, error) {
return nil, fmt.Errorf("invalid path expression %v", path)
}

func accessResource() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessResource() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetResource()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if newRes, ok := val.(pcommon.Resource); ok {
ctx.GetResource().Attributes().Clear()
newRes.CopyTo(ctx.GetResource())
Expand All @@ -179,12 +165,12 @@ func accessResource() pathGetSetter {
}
}

func accessResourceAttributes() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessResourceAttributes() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetResource().Attributes()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if attrs, ok := val.(pcommon.Map); ok {
ctx.GetResource().Attributes().Clear()
attrs.CopyTo(ctx.GetResource().Attributes())
Expand All @@ -193,125 +179,125 @@ func accessResourceAttributes() pathGetSetter {
}
}

func accessResourceAttributesKey(mapKey *string) pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessResourceAttributesKey(mapKey *string) tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return getAttr(ctx.GetResource().Attributes(), *mapKey)
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
setAttr(ctx.GetResource().Attributes(), *mapKey, val)
},
}
}

func accessInstrumentationScope() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessInstrumentationScope() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetInstrumentationScope()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if newIl, ok := val.(pcommon.InstrumentationScope); ok {
newIl.CopyTo(ctx.GetInstrumentationScope())
}
},
}
}

func accessInstrumentationScopeName() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessInstrumentationScopeName() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetInstrumentationScope().Name()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if str, ok := val.(string); ok {
ctx.GetInstrumentationScope().SetName(str)
}
},
}
}

func accessInstrumentationScopeVersion() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessInstrumentationScopeVersion() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetInstrumentationScope().Version()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if str, ok := val.(string); ok {
ctx.GetInstrumentationScope().SetVersion(str)
}
},
}
}

func accessTimeUnixNano() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessTimeUnixNano() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).Timestamp().AsTime().UnixNano()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if i, ok := val.(int64); ok {
ctx.GetItem().(plog.LogRecord).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, i)))
}
},
}
}

func accessObservedTimeUnixNano() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessObservedTimeUnixNano() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).ObservedTimestamp().AsTime().UnixNano()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if i, ok := val.(int64); ok {
ctx.GetItem().(plog.LogRecord).SetObservedTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, i)))
}
},
}
}

func accessSeverityNumber() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessSeverityNumber() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return int64(ctx.GetItem().(plog.LogRecord).SeverityNumber())
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if i, ok := val.(int64); ok {
ctx.GetItem().(plog.LogRecord).SetSeverityNumber(plog.SeverityNumber(i))
}
},
}
}

func accessSeverityText() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessSeverityText() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).SeverityText()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if s, ok := val.(string); ok {
ctx.GetItem().(plog.LogRecord).SetSeverityText(s)
}
},
}
}

func accessBody() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessBody() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return getValue(ctx.GetItem().(plog.LogRecord).Body())
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
setValue(ctx.GetItem().(plog.LogRecord).Body(), val)
},
}
}

func accessAttributes() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessAttributes() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).Attributes()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if attrs, ok := val.(pcommon.Map); ok {
ctx.GetItem().(plog.LogRecord).Attributes().Clear()
attrs.CopyTo(ctx.GetItem().(plog.LogRecord).Attributes())
Expand All @@ -320,62 +306,62 @@ func accessAttributes() pathGetSetter {
}
}

func accessAttributesKey(mapKey *string) pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessAttributesKey(mapKey *string) tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return getAttr(ctx.GetItem().(plog.LogRecord).Attributes(), *mapKey)
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
setAttr(ctx.GetItem().(plog.LogRecord).Attributes(), *mapKey, val)
},
}
}

func accessDroppedAttributesCount() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessDroppedAttributesCount() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return int64(ctx.GetItem().(plog.LogRecord).DroppedAttributesCount())
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if i, ok := val.(int64); ok {
ctx.GetItem().(plog.LogRecord).SetDroppedAttributesCount(uint32(i))
}
},
}
}

func accessFlags() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessFlags() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return int64(ctx.GetItem().(plog.LogRecord).Flags())
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if i, ok := val.(int64); ok {
ctx.GetItem().(plog.LogRecord).SetFlags(uint32(i))
}
},
}
}

func accessTraceID() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessTraceID() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).TraceID()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if newTraceID, ok := val.(pcommon.TraceID); ok {
ctx.GetItem().(plog.LogRecord).SetTraceID(newTraceID)
}
},
}
}

func accessStringTraceID() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessStringTraceID() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).TraceID().HexString()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if str, ok := val.(string); ok {
if traceID, err := parseTraceID(str); err == nil {
ctx.GetItem().(plog.LogRecord).SetTraceID(traceID)
Expand All @@ -385,25 +371,25 @@ func accessStringTraceID() pathGetSetter {
}
}

func accessSpanID() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessSpanID() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).SpanID()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if newSpanID, ok := val.(pcommon.SpanID); ok {
ctx.GetItem().(plog.LogRecord).SetSpanID(newSpanID)
}
},
}
}

func accessStringSpanID() pathGetSetter {
return pathGetSetter{
getter: func(ctx tql.TransformContext) interface{} {
func accessStringSpanID() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetItem().(plog.LogRecord).SpanID().HexString()
},
setter: func(ctx tql.TransformContext, val interface{}) {
Setter: func(ctx tql.TransformContext, val interface{}) {
if str, ok := val.(string); ok {
if spanID, err := parseSpanID(str); err == nil {
ctx.GetItem().(plog.LogRecord).SetSpanID(spanID)
Expand Down
Loading

0 comments on commit 504e59f

Please sign in to comment.