Comparison

plugins/mod_s2s.lua @ 11526:15a3db955ad3

s2s et al.: Add counters for connection state transitions
author Jonas Schäfer <jonas@wielicki.name>
date Wed, 21 Apr 2021 17:11:58 +0200
parent 11525:5f99fcc43938
child 11560:3bbb1af92514
comparison
equal deleted inserted replaced
11525:5f99fcc43938 11526:15a3db955ad3
48 ); 48 );
49 local measure_connections_outbound = module:metric( 49 local measure_connections_outbound = module:metric(
50 "gauge", "connections_outbound", "", 50 "gauge", "connections_outbound", "",
51 "Established outgoing s2s connections", 51 "Established outgoing s2s connections",
52 {"host", "type", "ip_family"} 52 {"host", "type", "ip_family"}
53 );
54
55 local m_accepted_tcp_connections = module:metric(
56 "counter", "accepted_tcp", "",
57 "Accepted incoming connections on the TCP layer"
58 );
59 local m_authn_connections = module:metric(
60 "counter", "authenticated", "",
61 "Authenticated incoming connections",
62 {"host", "direction", "mechanism"}
63 );
64 local m_initiated_connections = module:metric(
65 "counter", "initiated", "",
66 "Initiated outbound connections",
67 {"host"}
68 );
69 local m_closed_connections = module:metric(
70 "counter", "closed", "",
71 "Closed connections",
72 {"host", "direction", "error"}
53 ); 73 );
54 74
55 local sessions = module:shared("sessions"); 75 local sessions = module:shared("sessions");
56 76
57 local runner_callbacks = {}; 77 local runner_callbacks = {};
188 -- Store in buffer 208 -- Store in buffer
189 host_session.bounce_sendq = bounce_sendq; 209 host_session.bounce_sendq = bounce_sendq;
190 host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} }; 210 host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
191 log("debug", "stanza [%s] queued until connection complete", stanza.name); 211 log("debug", "stanza [%s] queued until connection complete", stanza.name);
192 connect(service.new(to_host, "xmpp-server", "tcp", s2s_service_options), listener, nil, { session = host_session }); 212 connect(service.new(to_host, "xmpp-server", "tcp", s2s_service_options), listener, nil, { session = host_session });
213 m_initiated_connections:with_labels(from_host):add(1)
193 return true; 214 return true;
194 end 215 end
195 216
196 local function keepalive(event) 217 local function keepalive(event)
197 local session = event.session; 218 local session = event.session;
308 if not session.hosts[host] then session.hosts[host] = {}; end 329 if not session.hosts[host] then session.hosts[host] = {}; end
309 session.hosts[host].authed = true; 330 session.hosts[host].authed = true;
310 end 331 end
311 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host); 332 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
312 333
334 local local_host = session.direction == "incoming" and session.to_host or session.from_host
335 m_authn_connections:with_labels(local_host, session.direction, event.mechanism or "other"):add(1)
336
313 if (session.type == "s2sout" and session.external_auth ~= "succeeded") or session.type == "s2sin" then 337 if (session.type == "s2sout" and session.external_auth ~= "succeeded") or session.type == "s2sin" then
314 -- Stream either used dialback for authentication or is an incoming stream. 338 -- Stream either used dialback for authentication or is an incoming stream.
315 mark_connected(session); 339 mark_connected(session);
316 end 340 end
317 341
526 session:open_stream(session.to_host, session.from_host); 550 session:open_stream(session.to_host, session.from_host);
527 else 551 else
528 session:open_stream(session.from_host, session.to_host); 552 session:open_stream(session.from_host, session.to_host);
529 end 553 end
530 end 554 end
555
556 local this_host = session.direction == "incoming" and session.to_host or session.from_host
557
531 if reason then -- nil == no err, initiated by us, false == initiated by remote 558 if reason then -- nil == no err, initiated by us, false == initiated by remote
532 local stream_error; 559 local stream_error;
560 local condition, text, extra
533 if type(reason) == "string" then -- assume stream error 561 if type(reason) == "string" then -- assume stream error
534 stream_error = st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); 562 condition = reason
535 elseif type(reason) == "table" and not st.is_stanza(reason) then 563 elseif type(reason) == "table" and not st.is_stanza(reason) then
536 stream_error = st.stanza("stream:error"):tag(reason.condition or "undefined-condition", stream_xmlns_attr):up(); 564 condition = reason.condition or "undefined-condition"
537 if reason.text then 565 text = reason.text
538 stream_error:tag("text", stream_xmlns_attr):text(reason.text):up(); 566 extra = reason.extra
567 end
568 if condition then
569 stream_error = st.stanza("stream:error"):tag(condition, stream_xmlns_attr):up();
570 if text then
571 stream_error:tag("text", stream_xmlns_attr):text(text):up();
539 end 572 end
540 if reason.extra then 573 if extra then
541 stream_error:add_child(reason.extra); 574 stream_error:add_child(extra);
542 end 575 end
576 end
577 if this_host and condition then
578 m_closed_connections:with_labels(this_host, session.direction, condition):add(1)
543 end 579 end
544 if st.is_stanza(stream_error) then 580 if st.is_stanza(stream_error) then
545 -- to and from are never unknown on outgoing connections 581 -- to and from are never unknown on outgoing connections
546 log("debug", "Disconnecting %s->%s[%s], <stream:error> is: %s", 582 log("debug", "Disconnecting %s->%s[%s], <stream:error> is: %s",
547 session.from_host or "(unknown host)" or session.ip, session.to_host or "(unknown host)", session.type, reason); 583 session.from_host or "(unknown host)" or session.ip, session.to_host or "(unknown host)", session.type, reason);
548 session.sends2s(stream_error); 584 session.sends2s(stream_error);
549 end 585 end
586 else
587 m_closed_connections:with_labels(this_host, session.direction, reason == false and ":remote-choice" or ":local-choice"):add(1)
550 end 588 end
551 589
552 session.sends2s("</stream:stream>"); 590 session.sends2s("</stream:stream>");
553 function session.sends2s() return false; end 591 function session.sends2s() return false; end
554 592
688 if not session then -- New incoming connection 726 if not session then -- New incoming connection
689 session = s2s_new_incoming(conn); 727 session = s2s_new_incoming(conn);
690 sessions[conn] = session; 728 sessions[conn] = session;
691 session.log("debug", "Incoming s2s connection"); 729 session.log("debug", "Incoming s2s connection");
692 initialize_session(session); 730 initialize_session(session);
731 m_accepted_tcp_connections:with_labels():add(1)
693 else -- Outgoing session connected 732 else -- Outgoing session connected
694 session:open_stream(session.from_host, session.to_host); 733 session:open_stream(session.from_host, session.to_host);
695 end 734 end
696 session.ip = conn:ip(); 735 session.ip = conn:ip();
697 end 736 end