diff --git a/pkg/service/roomservice.go b/pkg/service/roomservice.go index 43e2b39b..bc79ee92 100644 --- a/pkg/service/roomservice.go +++ b/pkg/service/roomservice.go @@ -90,7 +90,7 @@ func (s *RoomService) ListRooms(ctx context.Context, req *livekit.ListRoomsReque } rooms, err := s.roomStore.ListRooms(ctx, names) if err != nil { - // TODO: translate error codes to twirp + // TODO: translate error codes to Twirp return } diff --git a/pkg/service/server.go b/pkg/service/server.go index 1057176b..60a94b9e 100644 --- a/pkg/service/server.go +++ b/pkg/service/server.go @@ -83,9 +83,10 @@ func NewLivekitServer(conf *config.Config, middlewares = append(middlewares, NewAPIKeyAuthMiddleware(keyProvider)) } - roomServer := livekit.NewRoomServiceServer(roomService) - egressServer := livekit.NewEgressServer(egressService) - ingressServer := livekit.NewIngressServer(ingressService) + twirpLoggingHook := TwirpLogger(logger.GetDefaultLogger()) + roomServer := livekit.NewRoomServiceServer(roomService, twirpLoggingHook) + egressServer := livekit.NewEgressServer(egressService, twirpLoggingHook) + ingressServer := livekit.NewIngressServer(ingressService, twirpLoggingHook) mux := http.NewServeMux() if conf.Development { diff --git a/pkg/service/twirp.go b/pkg/service/twirp.go new file mode 100644 index 00000000..b62487e1 --- /dev/null +++ b/pkg/service/twirp.go @@ -0,0 +1,128 @@ +/* + * Copyright 2022 LiveKit, 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 service + +import ( + "context" + "sync" + "time" + + "github.com/twitchtv/twirp" + + "github.com/livekit/protocol/logger" +) + +var ( + logKey = struct{}{} +) + +// logging handling inspired by https://github.com/bakins/twirpzap +// License: Apache-2.0 +func TwirpLogger(logger logger.Logger) *twirp.ServerHooks { + loggerPool := &sync.Pool{ + New: func() interface{} { + return &requestLogger{ + logger: logger, + fieldsOrig: make([]interface{}, 0, 20), + } + }, + } + return &twirp.ServerHooks{ + RequestReceived: func(ctx context.Context) (context.Context, error) { + return requestReceived(ctx, loggerPool) + }, + RequestRouted: responseRouted, + Error: errorReceived, + ResponseSent: func(ctx context.Context) { + responseSent(ctx, loggerPool) + }, + } +} + +type requestLogger struct { + logger logger.Logger + service string + method string + error twirp.Error + fieldsOrig []interface{} + fields []interface{} + startedAt time.Time +} + +func requestReceived(ctx context.Context, requestLoggerPool *sync.Pool) (context.Context, error) { + r := requestLoggerPool.Get().(*requestLogger) + r.startedAt = time.Now() + r.fields = r.fieldsOrig + r.error = nil + + if svc, ok := twirp.ServiceName(ctx); ok { + r.service = svc + r.fields = append(r.fields, "service", svc) + } + + ctx = context.WithValue(ctx, logKey, r) + return ctx, nil +} + +func responseRouted(ctx context.Context) (context.Context, error) { + if meth, ok := twirp.MethodName(ctx); ok { + l, ok := ctx.Value(logKey).(*requestLogger) + if !ok || l == nil { + return ctx, nil + } + l.method = meth + l.fields = append(l.fields, "method", meth) + } + + return ctx, nil +} + +func responseSent(ctx context.Context, requestLoggerPool *sync.Pool) { + r, ok := ctx.Value(logKey).(*requestLogger) + if !ok || r == nil { + return + } + + r.fields = append(r.fields, "duration", time.Since(r.startedAt)) + + if status, ok := twirp.StatusCode(ctx); ok { + r.fields = append(r.fields, "status", status) + } + if r.error != nil { + r.fields = append(r.fields, "error", r.error.Msg()) + r.fields = append(r.fields, "code", r.error.Code()) + } + + serviceMethod := "API " + r.service + "." + r.method + r.logger.Infow(serviceMethod, r.fields...) + + r.fields = r.fieldsOrig + r.error = nil + + requestLoggerPool.Put(r) +} + +func errorReceived(ctx context.Context, e twirp.Error) context.Context { + r, ok := ctx.Value(logKey).(*requestLogger) + if !ok || r == nil { + return ctx + } + + r.error = e + + return ctx +} diff --git a/pkg/sfu/buffer/factory.go b/pkg/sfu/buffer/factory.go index aae1e0d7..1f1c5a00 100644 --- a/pkg/sfu/buffer/factory.go +++ b/pkg/sfu/buffer/factory.go @@ -4,8 +4,9 @@ import ( "io" "sync" - "github.com/livekit/mediatransportutil/pkg/bucket" "github.com/pion/transport/packetio" + + "github.com/livekit/mediatransportutil/pkg/bucket" ) type Factory struct {