Use a Twirp server hook to send API call details to telemetry. (#3401)
* Use a Twirp server hook to send API call details to telemetry. * mage generate and clean up * Add project_id * deps * - Redact requests - Do not store responses - Extract top level fields room_name, room_id, participant_identity, participant_id, track_id as appropriate - Store status as int * deps
This commit is contained in:
+107
-9
@@ -69,7 +69,10 @@ func NewRoomService(
|
||||
}
|
||||
|
||||
func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomRequest) (*livekit.Room, error) {
|
||||
AppendLogFields(ctx, "room", req.Name, "request", logger.Proto(redactCreateRoomRequest(req)))
|
||||
redactedReq := redactCreateRoomRequest(req)
|
||||
RecordRequest(ctx, redactedReq)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Name, "request", logger.Proto(redactedReq))
|
||||
if err := EnsureCreatePermission(ctx); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
} else if req.Egress != nil && s.egressLauncher == nil {
|
||||
@@ -85,10 +88,14 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.router.CreateRoom(ctx, req)
|
||||
room, err := s.router.CreateRoom(ctx, req)
|
||||
RecordResponse(ctx, room)
|
||||
return room, err
|
||||
}
|
||||
|
||||
func (s *RoomService) ListRooms(ctx context.Context, req *livekit.ListRoomsRequest) (*livekit.ListRoomsResponse, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Names)
|
||||
err := EnsureListPermission(ctx)
|
||||
if err != nil {
|
||||
@@ -108,10 +115,13 @@ func (s *RoomService) ListRooms(ctx context.Context, req *livekit.ListRoomsReque
|
||||
res := &livekit.ListRoomsResponse{
|
||||
Rooms: rooms,
|
||||
}
|
||||
RecordResponse(ctx, res)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) DeleteRoom(ctx context.Context, req *livekit.DeleteRoomRequest) (*livekit.DeleteRoomResponse, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room)
|
||||
if err := EnsureCreatePermission(ctx); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
@@ -134,10 +144,14 @@ func (s *RoomService) DeleteRoom(ctx context.Context, req *livekit.DeleteRoomReq
|
||||
}
|
||||
|
||||
err = s.roomStore.DeleteRoom(ctx, livekit.RoomName(req.Room))
|
||||
return &livekit.DeleteRoomResponse{}, err
|
||||
res := &livekit.DeleteRoomResponse{}
|
||||
RecordResponse(ctx, res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *RoomService) ListParticipants(ctx context.Context, req *livekit.ListParticipantsRequest) (*livekit.ListParticipantsResponse, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room)
|
||||
if err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room)); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
@@ -151,10 +165,13 @@ func (s *RoomService) ListParticipants(ctx context.Context, req *livekit.ListPar
|
||||
res := &livekit.ListParticipantsResponse{
|
||||
Participants: participants,
|
||||
}
|
||||
RecordResponse(ctx, res)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) GetParticipant(ctx context.Context, req *livekit.RoomParticipantIdentity) (*livekit.ParticipantInfo, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room, "participant", req.Identity)
|
||||
if err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room)); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
@@ -165,10 +182,13 @@ func (s *RoomService) GetParticipant(ctx context.Context, req *livekit.RoomParti
|
||||
return nil, err
|
||||
}
|
||||
|
||||
RecordResponse(ctx, participant)
|
||||
return participant, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) RemoveParticipant(ctx context.Context, req *livekit.RoomParticipantIdentity) (*livekit.RemoveParticipantResponse, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room, "participant", req.Identity)
|
||||
|
||||
if err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room)); err != nil {
|
||||
@@ -179,19 +199,27 @@ func (s *RoomService) RemoveParticipant(ctx context.Context, req *livekit.RoomPa
|
||||
return nil, twirp.NotFoundError("participant not found")
|
||||
}
|
||||
|
||||
return s.participantClient.RemoveParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
res, err := s.participantClient.RemoveParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
RecordResponse(ctx, res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteRoomTrackRequest) (*livekit.MuteRoomTrackResponse, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room, "participant", req.Identity, "trackID", req.TrackSid, "muted", req.Muted)
|
||||
if err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room)); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
}
|
||||
|
||||
return s.participantClient.MutePublishedTrack(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
res, err := s.participantClient.MutePublishedTrack(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
RecordResponse(ctx, res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.UpdateParticipantRequest) (*livekit.ParticipantInfo, error) {
|
||||
RecordRequest(ctx, redactUpdateParticipantRequest(req))
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room, "participant", req.Identity)
|
||||
|
||||
if !s.limitConf.CheckParticipantNameLength(req.Name) {
|
||||
@@ -210,10 +238,14 @@ func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.Update
|
||||
return nil, twirpAuthError(err)
|
||||
}
|
||||
|
||||
return s.participantClient.UpdateParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
res, err := s.participantClient.UpdateParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
RecordResponse(ctx, res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *RoomService) UpdateSubscriptions(ctx context.Context, req *livekit.UpdateSubscriptionsRequest) (*livekit.UpdateSubscriptionsResponse, error) {
|
||||
RecordRequest(ctx, req)
|
||||
|
||||
trackSIDs := append(make([]string, 0), req.TrackSids...)
|
||||
for _, pt := range req.ParticipantTracks {
|
||||
trackSIDs = append(trackSIDs, pt.TrackSids...)
|
||||
@@ -224,10 +256,14 @@ func (s *RoomService) UpdateSubscriptions(ctx context.Context, req *livekit.Upda
|
||||
return nil, twirpAuthError(err)
|
||||
}
|
||||
|
||||
return s.participantClient.UpdateSubscriptions(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
res, err := s.participantClient.UpdateSubscriptions(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
|
||||
RecordResponse(ctx, res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *RoomService) SendData(ctx context.Context, req *livekit.SendDataRequest) (*livekit.SendDataResponse, error) {
|
||||
RecordRequest(ctx, redactSendDataRequest(req))
|
||||
|
||||
roomName := livekit.RoomName(req.Room)
|
||||
AppendLogFields(ctx, "room", roomName, "size", len(req.Data))
|
||||
if err := EnsureAdminPermission(ctx, roomName); err != nil {
|
||||
@@ -239,10 +275,14 @@ func (s *RoomService) SendData(ctx context.Context, req *livekit.SendDataRequest
|
||||
return nil, twirp.NewError(twirp.InvalidArgument, fmt.Sprintf("nonce should be 16-bytes or not present, got: %d bytes", len(req.Nonce)))
|
||||
}
|
||||
|
||||
return s.roomClient.SendData(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
|
||||
res, err := s.roomClient.SendData(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
|
||||
RecordResponse(ctx, res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.UpdateRoomMetadataRequest) (*livekit.Room, error) {
|
||||
RecordRequest(ctx, redactUpdateRoomMetadataRequest(req))
|
||||
|
||||
AppendLogFields(ctx, "room", req.Room, "size", len(req.Metadata))
|
||||
maxMetadataSize := int(s.limitConf.MaxMetadataSize)
|
||||
if maxMetadataSize > 0 && len(req.Metadata) > maxMetadataSize {
|
||||
@@ -263,11 +303,12 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
||||
return nil, err
|
||||
}
|
||||
|
||||
RecordResponse(ctx, room)
|
||||
return room, nil
|
||||
}
|
||||
|
||||
func redactCreateRoomRequest(req *livekit.CreateRoomRequest) *livekit.CreateRoomRequest {
|
||||
if req.Egress == nil {
|
||||
if req.Egress == nil && req.Metadata == "" {
|
||||
// nothing to redact
|
||||
return req
|
||||
}
|
||||
@@ -284,5 +325,62 @@ func redactCreateRoomRequest(req *livekit.CreateRoomRequest) *livekit.CreateRoom
|
||||
egress.RedactUpload(clone.Egress.Tracks)
|
||||
}
|
||||
|
||||
// replace with size of metadata to provide visibility on request size
|
||||
if clone.Metadata != "" {
|
||||
clone.Metadata = fmt.Sprintf("__size: %d", len(clone.Metadata))
|
||||
}
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
func redactUpdateParticipantRequest(req *livekit.UpdateParticipantRequest) *livekit.UpdateParticipantRequest {
|
||||
if req.Metadata == "" && len(req.Attributes) == 0 {
|
||||
return req
|
||||
}
|
||||
|
||||
clone := utils.CloneProto(req)
|
||||
|
||||
// replace with size of metadata/attributes to provide visibility on request size
|
||||
if clone.Metadata != "" {
|
||||
clone.Metadata = fmt.Sprintf("__size: %d", len(clone.Metadata))
|
||||
}
|
||||
|
||||
if len(clone.Attributes) != 0 {
|
||||
total := 0
|
||||
for k, v := range clone.Attributes {
|
||||
total += len(k) + len(v)
|
||||
}
|
||||
|
||||
clone.Attributes = map[string]string{
|
||||
"__size": fmt.Sprintf("%d", total),
|
||||
}
|
||||
}
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
func redactSendDataRequest(req *livekit.SendDataRequest) *livekit.SendDataRequest {
|
||||
if len(req.Data) == 0 {
|
||||
return req
|
||||
}
|
||||
|
||||
clone := utils.CloneProto(req)
|
||||
|
||||
// replace with size of data to provide visibility on request size
|
||||
clone.Data = []byte(fmt.Sprintf("__size: %d", len(clone.Data)))
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
func redactUpdateRoomMetadataRequest(req *livekit.UpdateRoomMetadataRequest) *livekit.UpdateRoomMetadataRequest {
|
||||
if req.Metadata == "" {
|
||||
return req
|
||||
}
|
||||
|
||||
clone := utils.CloneProto(req)
|
||||
|
||||
// replace with size of metadata to provide visibility on request size
|
||||
clone.Metadata = fmt.Sprintf("__size: %d", len(clone.Metadata))
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
+229
-27
@@ -23,43 +23,48 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/twitchtv/twirp"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/telemetry"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
type twirpLoggerContext struct{}
|
||||
type statusReporterKey struct{}
|
||||
|
||||
type twirpRequestFields struct {
|
||||
service string
|
||||
method string
|
||||
error twirp.Error
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
type twirpLoggerKey struct{}
|
||||
|
||||
// logging handling inspired by https://github.com/bakins/twirpzap
|
||||
// License: Apache-2.0
|
||||
func TwirpLogger() *twirp.ServerHooks {
|
||||
loggerPool := &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &requestLogger{
|
||||
return &twirpLogger{
|
||||
fieldsOrig: make([]interface{}, 0, 30),
|
||||
}
|
||||
},
|
||||
}
|
||||
return &twirp.ServerHooks{
|
||||
RequestReceived: func(ctx context.Context) (context.Context, error) {
|
||||
return requestReceived(ctx, loggerPool)
|
||||
return loggerRequestReceived(ctx, loggerPool)
|
||||
},
|
||||
RequestRouted: responseRouted,
|
||||
Error: errorReceived,
|
||||
RequestRouted: loggerRequestRouted,
|
||||
Error: loggerErrorReceived,
|
||||
ResponseSent: func(ctx context.Context) {
|
||||
responseSent(ctx, loggerPool)
|
||||
loggerResponseSent(ctx, loggerPool)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type requestLogger struct {
|
||||
type twirpLogger struct {
|
||||
twirpRequestFields
|
||||
|
||||
fieldsOrig []interface{}
|
||||
@@ -68,7 +73,7 @@ type requestLogger struct {
|
||||
}
|
||||
|
||||
func AppendLogFields(ctx context.Context, fields ...interface{}) {
|
||||
r, ok := ctx.Value(twirpLoggerContext{}).(*requestLogger)
|
||||
r, ok := ctx.Value(twirpLoggerKey{}).(*twirpLogger)
|
||||
if !ok || r == nil {
|
||||
return
|
||||
}
|
||||
@@ -76,8 +81,8 @@ func AppendLogFields(ctx context.Context, fields ...interface{}) {
|
||||
r.fields = append(r.fields, fields...)
|
||||
}
|
||||
|
||||
func requestReceived(ctx context.Context, requestLoggerPool *sync.Pool) (context.Context, error) {
|
||||
r := requestLoggerPool.Get().(*requestLogger)
|
||||
func loggerRequestReceived(ctx context.Context, twirpLoggerPool *sync.Pool) (context.Context, error) {
|
||||
r := twirpLoggerPool.Get().(*twirpLogger)
|
||||
r.startedAt = time.Now()
|
||||
r.fields = r.fieldsOrig
|
||||
r.error = nil
|
||||
@@ -87,13 +92,12 @@ func requestReceived(ctx context.Context, requestLoggerPool *sync.Pool) (context
|
||||
r.fields = append(r.fields, "service", svc)
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, twirpLoggerContext{}, r)
|
||||
return ctx, nil
|
||||
return context.WithValue(ctx, twirpLoggerKey{}, r), nil
|
||||
}
|
||||
|
||||
func responseRouted(ctx context.Context) (context.Context, error) {
|
||||
func loggerRequestRouted(ctx context.Context) (context.Context, error) {
|
||||
if meth, ok := twirp.MethodName(ctx); ok {
|
||||
l, ok := ctx.Value(twirpLoggerContext{}).(*requestLogger)
|
||||
l, ok := ctx.Value(twirpLoggerKey{}).(*twirpLogger)
|
||||
if !ok || l == nil {
|
||||
return ctx, nil
|
||||
}
|
||||
@@ -104,8 +108,8 @@ func responseRouted(ctx context.Context) (context.Context, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func responseSent(ctx context.Context, requestLoggerPool *sync.Pool) {
|
||||
r, ok := ctx.Value(twirpLoggerContext{}).(*requestLogger)
|
||||
func loggerResponseSent(ctx context.Context, twirpLoggerPool *sync.Pool) {
|
||||
r, ok := ctx.Value(twirpLoggerKey{}).(*twirpLogger)
|
||||
if !ok || r == nil {
|
||||
return
|
||||
}
|
||||
@@ -126,24 +130,27 @@ func responseSent(ctx context.Context, requestLoggerPool *sync.Pool) {
|
||||
r.fields = r.fieldsOrig
|
||||
r.error = nil
|
||||
|
||||
requestLoggerPool.Put(r)
|
||||
twirpLoggerPool.Put(r)
|
||||
}
|
||||
|
||||
func errorReceived(ctx context.Context, e twirp.Error) context.Context {
|
||||
r, ok := ctx.Value(twirpLoggerContext{}).(*requestLogger)
|
||||
func loggerErrorReceived(ctx context.Context, e twirp.Error) context.Context {
|
||||
r, ok := ctx.Value(twirpLoggerKey{}).(*twirpLogger)
|
||||
if !ok || r == nil {
|
||||
return ctx
|
||||
}
|
||||
|
||||
r.error = e
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
type statusReporterKey struct{}
|
||||
|
||||
func TwirpRequestStatusReporter() *twirp.ServerHooks {
|
||||
return &twirp.ServerHooks{
|
||||
RequestReceived: statusReporterRequestReceived,
|
||||
RequestRouted: statusReporterResponseRouted,
|
||||
RequestRouted: statusReporterRequestRouted,
|
||||
Error: statusReporterErrorReceived,
|
||||
ResponseSent: statusReporterResponseSent,
|
||||
}
|
||||
@@ -156,11 +163,10 @@ func statusReporterRequestReceived(ctx context.Context) (context.Context, error)
|
||||
r.service = svc
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, statusReporterKey{}, r)
|
||||
return ctx, nil
|
||||
return context.WithValue(ctx, statusReporterKey{}, r), nil
|
||||
}
|
||||
|
||||
func statusReporterResponseRouted(ctx context.Context) (context.Context, error) {
|
||||
func statusReporterRequestRouted(ctx context.Context) (context.Context, error) {
|
||||
if meth, ok := twirp.MethodName(ctx); ok {
|
||||
l, ok := ctx.Value(statusReporterKey{}).(*twirpRequestFields)
|
||||
if !ok || l == nil {
|
||||
@@ -207,6 +213,202 @@ func statusReporterErrorReceived(ctx context.Context, e twirp.Error) context.Con
|
||||
}
|
||||
|
||||
r.error = e
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
type twirpTelemetryKey struct{}
|
||||
|
||||
func TwirpTelemetry(
|
||||
nodeID livekit.NodeID,
|
||||
getProjectID func(ctx context.Context) string,
|
||||
telemetry telemetry.TelemetryService,
|
||||
) *twirp.ServerHooks {
|
||||
return &twirp.ServerHooks{
|
||||
RequestReceived: telemetryRequestReceived,
|
||||
Error: telemetryErrorReceived,
|
||||
ResponseSent: func(ctx context.Context) {
|
||||
telemetryResponseSent(ctx, nodeID, getProjectID, telemetry)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func RecordRequest(ctx context.Context, request proto.Message) {
|
||||
if request == nil {
|
||||
return
|
||||
}
|
||||
|
||||
a, ok := ctx.Value(twirpTelemetryKey{}).(*livekit.APICallInfo)
|
||||
if !ok || a == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// capture request and extract common fields to top level as appropriate
|
||||
switch msg := request.(type) {
|
||||
case *livekit.CreateRoomRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_CreateRoomRequest{
|
||||
CreateRoomRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Name
|
||||
|
||||
case *livekit.ListRoomsRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_ListRoomsRequest{
|
||||
ListRoomsRequest: msg,
|
||||
},
|
||||
}
|
||||
|
||||
case *livekit.DeleteRoomRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_DeleteRoomRequest{
|
||||
DeleteRoomRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
|
||||
case *livekit.ListParticipantsRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_ListParticipantsRequest{
|
||||
ListParticipantsRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
|
||||
case *livekit.RoomParticipantIdentity:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_RoomParticipantIdentity{
|
||||
RoomParticipantIdentity: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
a.ParticipantIdentity = msg.Identity
|
||||
|
||||
case *livekit.MuteRoomTrackRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_MuteRoomTrackRequest{
|
||||
MuteRoomTrackRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
a.ParticipantIdentity = msg.Identity
|
||||
a.TrackId = msg.TrackSid
|
||||
|
||||
case *livekit.UpdateParticipantRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_UpdateParticipantRequest{
|
||||
UpdateParticipantRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
a.ParticipantIdentity = msg.Identity
|
||||
|
||||
case *livekit.UpdateSubscriptionsRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_UpdateSubscriptionsRequest{
|
||||
UpdateSubscriptionsRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
a.ParticipantIdentity = msg.Identity
|
||||
|
||||
case *livekit.SendDataRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_SendDataRequest{
|
||||
SendDataRequest: msg,
|
||||
},
|
||||
}
|
||||
a.RoomName = msg.Room
|
||||
|
||||
case *livekit.UpdateRoomMetadataRequest:
|
||||
a.Request = &livekit.APICallRequest{
|
||||
Message: &livekit.APICallRequest_UpdateRoomMetadataRequest{
|
||||
UpdateRoomMetadataRequest: msg,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RecordResponse(ctx context.Context, response proto.Message) {
|
||||
if response == nil {
|
||||
return
|
||||
}
|
||||
|
||||
a, ok := ctx.Value(twirpTelemetryKey{}).(*livekit.APICallInfo)
|
||||
if !ok || a == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// extract common fields to top level as appropriate
|
||||
switch msg := response.(type) {
|
||||
case *livekit.Room:
|
||||
a.RoomId = msg.Sid
|
||||
|
||||
case *livekit.ParticipantInfo:
|
||||
a.ParticipantId = msg.Sid
|
||||
}
|
||||
}
|
||||
|
||||
func telemetryRequestReceived(ctx context.Context) (context.Context, error) {
|
||||
a := &livekit.APICallInfo{}
|
||||
a.StartedAt = timestamppb.Now()
|
||||
|
||||
if svc, ok := twirp.ServiceName(ctx); ok {
|
||||
a.Service = svc
|
||||
}
|
||||
|
||||
return context.WithValue(ctx, twirpTelemetryKey{}, a), nil
|
||||
}
|
||||
|
||||
func telemetryRequestRouted(ctx context.Context) (context.Context, error) {
|
||||
if meth, ok := twirp.MethodName(ctx); ok {
|
||||
a, ok := ctx.Value(twirpTelemetryKey{}).(*livekit.APICallInfo)
|
||||
if !ok || a == nil {
|
||||
return ctx, nil
|
||||
}
|
||||
a.Method = meth
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func telemetryResponseSent(
|
||||
ctx context.Context,
|
||||
nodeID livekit.NodeID,
|
||||
getProjectID func(ctx context.Context) string,
|
||||
telemetry telemetry.TelemetryService,
|
||||
) {
|
||||
a, ok := ctx.Value(twirpTelemetryKey{}).(*livekit.APICallInfo)
|
||||
if !ok || a == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if getProjectID != nil {
|
||||
a.ProjectId = getProjectID(ctx)
|
||||
}
|
||||
a.NodeId = string(nodeID)
|
||||
if statusCode, ok := twirp.StatusCode(ctx); ok {
|
||||
if status, err := strconv.Atoi(statusCode); err == nil {
|
||||
a.Status = int32(status)
|
||||
}
|
||||
}
|
||||
a.DurationNs = time.Since(a.StartedAt.AsTime()).Nanoseconds()
|
||||
if telemetry != nil {
|
||||
telemetry.APICall(ctx, a)
|
||||
}
|
||||
}
|
||||
|
||||
func telemetryErrorReceived(ctx context.Context, e twirp.Error) context.Context {
|
||||
a, ok := ctx.Value(twirpTelemetryKey{}).(*livekit.APICallInfo)
|
||||
if !ok || a == nil {
|
||||
return ctx
|
||||
}
|
||||
|
||||
a.TwirpErrorCode = string(e.Code())
|
||||
a.TwirpErrorMessage = e.Msg()
|
||||
return ctx
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
+13
-2
@@ -499,12 +499,23 @@ func (t *telemetryService) IngressEnded(ctx context.Context, info *livekit.Ingre
|
||||
})
|
||||
}
|
||||
|
||||
func (t *telemetryService) Report(ctx context.Context, report *livekit.ReportInfo) {
|
||||
func (t *telemetryService) Report(ctx context.Context, reportInfo *livekit.ReportInfo) {
|
||||
t.enqueue(func() {
|
||||
ev := &livekit.AnalyticsEvent{
|
||||
Type: livekit.AnalyticsEventType_REPORT,
|
||||
Timestamp: timestamppb.Now(),
|
||||
Report: report,
|
||||
Report: reportInfo,
|
||||
}
|
||||
t.SendEvent(ctx, ev)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *telemetryService) APICall(ctx context.Context, apiCallInfo *livekit.APICallInfo) {
|
||||
t.enqueue(func() {
|
||||
ev := &livekit.AnalyticsEvent{
|
||||
Type: livekit.AnalyticsEventType_API_CALL,
|
||||
Timestamp: timestamppb.Now(),
|
||||
ApiCall: apiCallInfo,
|
||||
}
|
||||
t.SendEvent(ctx, ev)
|
||||
})
|
||||
|
||||
@@ -10,6 +10,12 @@ import (
|
||||
)
|
||||
|
||||
type FakeTelemetryService struct {
|
||||
APICallStub func(context.Context, *livekit.APICallInfo)
|
||||
aPICallMutex sync.RWMutex
|
||||
aPICallArgsForCall []struct {
|
||||
arg1 context.Context
|
||||
arg2 *livekit.APICallInfo
|
||||
}
|
||||
EgressEndedStub func(context.Context, *livekit.EgressInfo)
|
||||
egressEndedMutex sync.RWMutex
|
||||
egressEndedArgsForCall []struct {
|
||||
@@ -263,6 +269,39 @@ type FakeTelemetryService struct {
|
||||
invocationsMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) APICall(arg1 context.Context, arg2 *livekit.APICallInfo) {
|
||||
fake.aPICallMutex.Lock()
|
||||
fake.aPICallArgsForCall = append(fake.aPICallArgsForCall, struct {
|
||||
arg1 context.Context
|
||||
arg2 *livekit.APICallInfo
|
||||
}{arg1, arg2})
|
||||
stub := fake.APICallStub
|
||||
fake.recordInvocation("APICall", []interface{}{arg1, arg2})
|
||||
fake.aPICallMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.APICallStub(arg1, arg2)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) APICallCallCount() int {
|
||||
fake.aPICallMutex.RLock()
|
||||
defer fake.aPICallMutex.RUnlock()
|
||||
return len(fake.aPICallArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) APICallCalls(stub func(context.Context, *livekit.APICallInfo)) {
|
||||
fake.aPICallMutex.Lock()
|
||||
defer fake.aPICallMutex.Unlock()
|
||||
fake.APICallStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) APICallArgsForCall(i int) (context.Context, *livekit.APICallInfo) {
|
||||
fake.aPICallMutex.RLock()
|
||||
defer fake.aPICallMutex.RUnlock()
|
||||
argsForCall := fake.aPICallArgsForCall[i]
|
||||
return argsForCall.arg1, argsForCall.arg2
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) EgressEnded(arg1 context.Context, arg2 *livekit.EgressInfo) {
|
||||
fake.egressEndedMutex.Lock()
|
||||
fake.egressEndedArgsForCall = append(fake.egressEndedArgsForCall, struct {
|
||||
@@ -1458,6 +1497,8 @@ func (fake *FakeTelemetryService) TrackUnsubscribedArgsForCall(i int) (context.C
|
||||
func (fake *FakeTelemetryService) Invocations() map[string][][]interface{} {
|
||||
fake.invocationsMutex.RLock()
|
||||
defer fake.invocationsMutex.RUnlock()
|
||||
fake.aPICallMutex.RLock()
|
||||
defer fake.aPICallMutex.RUnlock()
|
||||
fake.egressEndedMutex.RLock()
|
||||
defer fake.egressEndedMutex.RUnlock()
|
||||
fake.egressStartedMutex.RLock()
|
||||
|
||||
@@ -76,7 +76,8 @@ type TelemetryService interface {
|
||||
IngressUpdated(ctx context.Context, info *livekit.IngressInfo)
|
||||
IngressEnded(ctx context.Context, info *livekit.IngressInfo)
|
||||
LocalRoomState(ctx context.Context, info *livekit.AnalyticsNodeRooms)
|
||||
Report(ctx context.Context, report *livekit.ReportInfo)
|
||||
Report(ctx context.Context, reportInfo *livekit.ReportInfo)
|
||||
APICall(ctx context.Context, apiCallInfo *livekit.APICallInfo)
|
||||
|
||||
// helpers
|
||||
AnalyticsService
|
||||
|
||||
Reference in New Issue
Block a user