[
https://issues.apache.org/jira/browse/THRIFT-5164?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17134411#comment-17134411
]
Yuxuan Wang commented on THRIFT-5164:
-------------------------------------
This is an example of using middleware to do json response logging. I'm sure
there are ways to improve/simplify it, for example just use TSimpleJSONProtocol
for the dummyOut to avoid serializing it again for logging:
{code:go}
func LoggingMiddleware(respGenerator map[string]func() thrift.TStruct,
protoFactory thrift.TProtocolFactory) thrift.ProcessorMiddleware {
deserializerPool := thrift.NewTDeserializerPool(func()
*thrift.TDeserializer {
transport := thrift.NewTMemoryBufferLen(1024)
protocol := protoFactory.GetProtocol(transport)
return &thrift.TDeserializer{
Transport: transport,
Protocol: protocol,
}
})
jsonSerializerPool := thrift.NewTSerializerPool(func()
*thrift.TSerializer {
transport := thrift.NewTMemoryBufferLen(1024)
protocol := thrift.NewTSimpleJSONProtocol(transport)
return &thrift.TSerializer{
Transport: transport,
Protocol: protocol,
}
})
return func(name string, next thrift.TProcessorFunction)
thrift.TProcessorFunction {
return thrift.WrappedTProcessorFunction{
Wrapped: func(ctx context.Context, seqID int32, in, out
thrift.TProtocol) (bool, thrift.TException) {
dummyOut := out
if respGen, ok := respGenerator[name]; ok {
memBuf := thrift.NewTMemoryBuffer()
dummyOut =
protoFactory.GetProtocol(memBuf)
defer func() {
resp := respGen()
if err :=
deserializerPool.Read(resp, memBuf.Bytes()); err != nil {
// TODO: handle error
}
jsonString, err :=
jsonSerializerPool.WriteString(context.Background(), resp)
if err != nil {
// TODO: handle error
} else {
// TODO: log jsonString
_ = jsonString
}
if err := resp.Write(out); err
!= nil {
// TODO: handle error
}
}()
} else {
println("Intercepting of " + name + "
not supported")
}
return next.Process(ctx, seqID, in, dummyOut)
},
}
}
}
{code}
> Go middleware support
> ---------------------
>
> Key: THRIFT-5164
> URL: https://issues.apache.org/jira/browse/THRIFT-5164
> Project: Thrift
> Issue Type: Improvement
> Components: Go - Library
> Reporter: Yuxuan Wang
> Assignee: Duru Can Celasun
> Priority: Major
> Labels: Breaking-Change
> Fix For: 0.14.0
>
> Time Spent: 2h
> Remaining Estimate: 0h
>
> I saw that this idea was discussed before in THRIFT-4553, but want to reopen
> the discussion to see if there's a change of mind.
> We (Reddit) recently implemented TProcessor level middleware support in our
> Baseplate.go library, and we think that our implementation is generic enough
> that it might make sense to contribute that into the thrift library. The
> related implementation are all in this file:
> https://github.com/reddit/baseplate.go/blob/4225e42dc8dde56b222ac7ea3a4ff63aa726f6a6/thriftbp/middlewares.go
> https://github.com/reddit/baseplate.go/blob/4225e42dc8dde56b222ac7ea3a4ff63aa726f6a6/tracing/middlewares.go#L40-L64
> is an example of how a tracing middleware is implemented.
> If we think that's a good idea, then we can contribute that into thrift repo
> (with renames, of course). One implementation detail I'm not sure is that
> whether we want to expand the TProcessor interface to also include
> ProcessorMap and AddToProcessorMap, or do we want to keep the two iinterfaces
> separate.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)