Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2178 from matrix-org/erikj/message_metrics
Browse files Browse the repository at this point in the history
Add more granular event send metrics
  • Loading branch information
erikjohnston authored May 2, 2017
2 parents 82475a1 + 3e5a62e commit e4c15fc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class EventContext(object):
"prev_group",
"delta_ids",
"prev_state_events",
"app_service",
]

def __init__(self):
Expand All @@ -68,3 +69,5 @@ def __init__(self):
self.delta_ids = None

self.prev_state_events = None

self.app_service = None
10 changes: 8 additions & 2 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ def get_messages(self, requester, room_id=None, pagin_config=None,
defer.returnValue(chunk)

@defer.inlineCallbacks
def create_event(self, event_dict, token_id=None, txn_id=None, prev_event_ids=None):
def create_event(self, requester, event_dict, token_id=None, txn_id=None,
prev_event_ids=None):
"""
Given a dict from a client, create a new event.
Expand All @@ -185,6 +186,7 @@ def create_event(self, event_dict, token_id=None, txn_id=None, prev_event_ids=No
Adds display names to Join membership events.
Args:
requester
event_dict (dict): An entire event
token_id (str)
txn_id (str)
Expand Down Expand Up @@ -226,6 +228,7 @@ def create_event(self, event_dict, token_id=None, txn_id=None, prev_event_ids=No

event, context = yield self._create_new_client_event(
builder=builder,
requester=requester,
prev_event_ids=prev_event_ids,
)

Expand Down Expand Up @@ -319,6 +322,7 @@ def create_and_send_nonmember_event(
See self.create_event and self.send_nonmember_event.
"""
event, context = yield self.create_event(
requester,
event_dict,
token_id=requester.access_token_id,
txn_id=txn_id
Expand Down Expand Up @@ -416,7 +420,7 @@ def get_state_events(self, user_id, room_id, is_guest=False):

@measure_func("_create_new_client_event")
@defer.inlineCallbacks
def _create_new_client_event(self, builder, prev_event_ids=None):
def _create_new_client_event(self, builder, requester=None, prev_event_ids=None):
if prev_event_ids:
prev_events = yield self.store.add_event_hashes(prev_event_ids)
prev_max_depth = yield self.store.get_max_depth_of_events(prev_event_ids)
Expand Down Expand Up @@ -456,6 +460,8 @@ def _create_new_client_event(self, builder, prev_event_ids=None):
state_handler = self.state_handler

context = yield state_handler.compute_event_context(builder)
if requester:
context.app_service = requester.app_service

if builder.is_state():
builder.prev_state = yield self.store.add_event_hashes(
Expand Down
1 change: 1 addition & 0 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _local_membership_update(
content["kind"] = "guest"

event, context = yield msg_handler.create_event(
requester,
{
"type": EventTypes.Member,
"content": content,
Expand Down
1 change: 1 addition & 0 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def on_PUT(self, request, room_id, event_type, state_key, txn_id=None):
else:
msg_handler = self.handlers.message_handler
event, context = yield msg_handler.create_event(
requester,
event_dict,
token_id=requester.access_token_id,
txn_id=txn_id,
Expand Down
16 changes: 16 additions & 0 deletions synapse/storage/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from synapse.api.errors import SynapseError
from synapse.state import resolve_events
from synapse.util.caches.descriptors import cached
from synapse.types import get_domain_from_id

from canonicaljson import encode_canonical_json
from collections import deque, namedtuple, OrderedDict
Expand All @@ -49,6 +50,9 @@

metrics = synapse.metrics.get_metrics_for(__name__)
persist_event_counter = metrics.register_counter("persisted_events")
event_counter = metrics.register_counter(
"persisted_events_sep", labels=["type", "origin_type", "origin_entity"]
)


def encode_json(json_object):
Expand Down Expand Up @@ -370,6 +374,18 @@ def _persist_events(self, events_and_contexts, backfilled=False,
new_forward_extremeties=new_forward_extremeties,
)
persist_event_counter.inc_by(len(chunk))
for event, context in chunk:
if context.app_service:
origin_type = "local"
origin_entity = context.app_service.id
elif self.hs.is_mine_id(event.sender):
origin_type = "local"
origin_entity = "*client*"
else:
origin_type = "remote"
origin_entity = get_domain_from_id(event.sender)

event_counter.inc(event.type, origin_type, origin_entity)

@defer.inlineCallbacks
def _calculate_new_extremeties(self, room_id, event_contexts, latest_event_ids):
Expand Down
4 changes: 2 additions & 2 deletions tests/storage/event_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def __init__(self, hs):
self.event_builder_factory = hs.get_event_builder_factory()

@defer.inlineCallbacks
def create_room(self, room):
def create_room(self, room, user):
builder = self.event_builder_factory.new({
"type": EventTypes.Create,
"sender": "",
"sender": user.to_string(),
"room_id": room.to_string(),
"content": {},
})
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_count_daily_messages(self):
# Create something to report
room = RoomID.from_string("!abc123:test")
user = UserID.from_string("@raccoonlover:test")
yield self.event_injector.create_room(room)
yield self.event_injector.create_room(room, user)

self.base_event = yield self._get_last_stream_token()

Expand Down

0 comments on commit e4c15fc

Please sign in to comment.