@@ -138,54 +138,79 @@ func handleLocalWebRTCSignal(c *gin.Context) {
138138 return
139139 }
140140
141+ // get the source from the request
142+ source := c .ClientIP ()
143+
141144 // Now use conn for websocket operations
142145 defer wsCon .Close (websocket .StatusNormalClosure , "" )
143- err = handleWebRTCSignalWsMessages (wsCon , false )
146+ err = handleWebRTCSignalWsMessages (wsCon , false , source )
144147 if err != nil {
145148 c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
146149 return
147150 }
148151}
149152
150- func handleWebRTCSignalWsMessages (wsCon * websocket.Conn , isCloudConnection bool ) error {
153+ func handleWebRTCSignalWsMessages (wsCon * websocket.Conn , isCloudConnection bool , source string ) error {
151154 runCtx , cancelRun := context .WithCancel (context .Background ())
152155 defer cancelRun ()
153156
154157 // Add connection tracking to detect reconnections
155158 connectionID := uuid .New ().String ()
156159 cloudLogger .Infof ("new websocket connection established with ID: %s" , connectionID )
157160
161+ // connection type
162+ var sourceType string
163+ if isCloudConnection {
164+ sourceType = "cloud"
165+ } else {
166+ sourceType = "local"
167+ }
168+
169+ // probably we can use a better logging framework here
170+ logInfof := func (format string , args ... interface {}) {
171+ args = append (args , source , sourceType )
172+ websocketLogger .Infof (format + ", source: %s, sourceType: %s" , args ... )
173+ }
174+ logWarnf := func (format string , args ... interface {}) {
175+ args = append (args , source , sourceType )
176+ websocketLogger .Warnf (format + ", source: %s, sourceType: %s" , args ... )
177+ }
178+ logTracef := func (format string , args ... interface {}) {
179+ args = append (args , source , sourceType )
180+ websocketLogger .Tracef (format + ", source: %s, sourceType: %s" , args ... )
181+ }
182+
158183 go func () {
159184 for {
160185 time .Sleep (WebsocketPingInterval )
161186
162187 // set the timer for the ping duration
163188 timer := prometheus .NewTimer (prometheus .ObserverFunc (func (v float64 ) {
164- metricConnectionLastPingDuration .Set (v )
165- metricConnectionPingDuration .Observe (v )
189+ metricConnectionLastPingDuration .WithLabelValues ( sourceType , source ). Set (v )
190+ metricConnectionPingDuration .WithLabelValues ( sourceType , source ). Observe (v )
166191 }))
167192
168- cloudLogger . Infof ("pinging websocket" )
193+ logInfof ("pinging websocket" )
169194 err := wsCon .Ping (runCtx )
170195
171196 if err != nil {
172- cloudLogger . Warnf ("websocket ping error: %v" , err )
197+ logWarnf ("websocket ping error: %v" , err )
173198 cancelRun ()
174199 return
175200 }
176201
177202 // dont use `defer` here because we want to observe the duration of the ping
178203 timer .ObserveDuration ()
179204
180- metricConnectionTotalPingCount .Inc ()
181- metricConnectionLastPingTimestamp .SetToCurrentTime ()
205+ metricConnectionTotalPingCount .WithLabelValues ( sourceType , source ). Inc ()
206+ metricConnectionLastPingTimestamp .WithLabelValues ( sourceType , source ). SetToCurrentTime ()
182207 }
183208 }()
184209
185210 for {
186211 typ , msg , err := wsCon .Read (runCtx )
187212 if err != nil {
188- websocketLogger . Warnf ("websocket read error: %v" , err )
213+ logWarnf ("websocket read error: %v" , err )
189214 return err
190215 }
191216 if typ != websocket .MessageText {
@@ -200,54 +225,54 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool)
200225
201226 err = json .Unmarshal (msg , & message )
202227 if err != nil {
203- websocketLogger . Warnf ("unable to parse ws message: %v" , string ( msg ) )
228+ logWarnf ("unable to parse ws message: %v" , err )
204229 continue
205230 }
206231
207232 if message .Type == "offer" {
208- websocketLogger . Infof ("new session request received" )
233+ logInfof ("new session request received" )
209234 var req WebRTCSessionRequest
210235 err = json .Unmarshal (message .Data , & req )
211236 if err != nil {
212- websocketLogger . Warnf ("unable to parse session request data: %v" , string ( message . Data ) )
237+ logWarnf ("unable to parse session request data: %v" , err )
213238 continue
214239 }
215240
216- websocketLogger . Infof ("new session request: %v" , req .OidcGoogle )
217- websocketLogger . Tracef ("session request info: %v" , req )
241+ logInfof ("new session request: %v" , req .OidcGoogle )
242+ logTracef ("session request info: %v" , req )
218243
219- metricConnectionSessionRequestCount .Inc ()
220- metricConnectionLastSessionRequestTimestamp .SetToCurrentTime ()
221- err = handleSessionRequest (runCtx , wsCon , req , isCloudConnection )
244+ metricConnectionSessionRequestCount .WithLabelValues ( sourceType , source ). Inc ()
245+ metricConnectionLastSessionRequestTimestamp .WithLabelValues ( sourceType , source ). SetToCurrentTime ()
246+ err = handleSessionRequest (runCtx , wsCon , req , isCloudConnection , source )
222247 if err != nil {
223- websocketLogger . Infof ("error starting new session: %v" , err )
248+ logWarnf ("error starting new session: %v" , err )
224249 continue
225250 }
226251 } else if message .Type == "new-ice-candidate" {
227- websocketLogger . Infof ("The client sent us a new ICE candidate: %v" , string (message .Data ))
252+ logInfof ("The client sent us a new ICE candidate: %v" , string (message .Data ))
228253 var candidate webrtc.ICECandidateInit
229254
230255 // Attempt to unmarshal as a ICECandidateInit
231256 if err := json .Unmarshal (message .Data , & candidate ); err != nil {
232- websocketLogger . Warnf ("unable to parse incoming ICE candidate data: %v" , string (message .Data ))
257+ logWarnf ("unable to parse incoming ICE candidate data: %v" , string (message .Data ))
233258 continue
234259 }
235260
236261 if candidate .Candidate == "" {
237- websocketLogger . Warnf ("empty incoming ICE candidate, skipping" )
262+ logWarnf ("empty incoming ICE candidate, skipping" )
238263 continue
239264 }
240265
241- websocketLogger . Infof ("unmarshalled incoming ICE candidate: %v" , candidate )
266+ logInfof ("unmarshalled incoming ICE candidate: %v" , candidate )
242267
243268 if currentSession == nil {
244- websocketLogger . Infof ("no current session, skipping incoming ICE candidate" )
269+ logInfof ("no current session, skipping incoming ICE candidate" )
245270 continue
246271 }
247272
248- websocketLogger . Infof ("adding incoming ICE candidate to current session: %v" , candidate )
273+ logInfof ("adding incoming ICE candidate to current session: %v" , candidate )
249274 if err = currentSession .peerConnection .AddICECandidate (candidate ); err != nil {
250- websocketLogger . Warnf ("failed to add incoming ICE candidate to our peer connection: %v" , err )
275+ logWarnf ("failed to add incoming ICE candidate to our peer connection: %v" , err )
251276 }
252277 }
253278 }
0 commit comments