add datadog monitoring

This commit is contained in:
Alberto Xamin 2022-03-24 19:22:09 +01:00
parent 8ca795f680
commit 1e8c399c3b
2 changed files with 24 additions and 1 deletions

View File

@ -8,3 +8,4 @@ six==1.16.0
pytest==7.1.1
requests==2.27.1
discord-webhook==0.15.0
datadog==0.44.0

View File

@ -1,5 +1,6 @@
import os
import json
import time
import random
from typing import List
import eventlet
@ -10,6 +11,17 @@ from bang.players import Player, PendingAction
import requests
from discord_webhook import DiscordWebhook
from datadog import initialize, api
send_metrics = False
if "DATADOG_API_KEY" in os.environ and "DATADOG_APP_KEY" in os.environ:
initialize()
send_metrics = True
api.Event.create(title="Backend start", text="", tags=["server:backend", f"host:{os.environ['HOST']}"], alert_type="info")])
else:
print("Datadog not configured")
import sys
sys.setrecursionlimit(10**6) # this should prevents bots from stopping
@ -43,6 +55,8 @@ blacklist: List[str] = []
def advertise_lobbies():
sio.emit('lobbies', room='lobby', data=[{'name': g.name, 'players': len(g.players), 'locked': g.password != ''} for g in games if not g.started and len(g.players) < 10 and not g.is_hidden])
sio.emit('spectate_lobbies', room='lobby', data=[{'name': g.name, 'players': len(g.players), 'locked': g.password != ''} for g in games if g.started])
if send_metrics:
api.Metric.send(metric='lobbies', points=[(int(time.time()), len(games))], tags=["server:backend", "type:lobbies", f"host:{os.environ['HOST']}"])
@sio.event
def connect(sid, environ):
@ -51,6 +65,8 @@ def connect(sid, environ):
print('connect ', sid)
sio.enter_room(sid, 'lobby')
sio.emit('players', room='lobby', data=online_players)
if send_metrics:
api.Metric.send(metric='online_players', points=[(int(time.time()), online_players)], tags=["server:backend", f"host:{os.environ['HOST']}"])
@sio.event
def get_online_players(sid):
@ -73,6 +89,8 @@ def report(sid, text):
sio.emit('chat_message', room=sid, data={'color': f'green','text':f'Report OK'})
else:
print("WARNING: DISCORD_WEBHOOK not found")
if send_metrics:
api.Event.create(title="BUG REPORT", text=data, tags=["server:backend", f"host:{os.environ['HOST']}"])
print(f'New bug report, replay at https://www.toptal.com/developers/hastebin/{key}')
@sio.event
@ -156,6 +174,8 @@ def disconnect(sid):
games.pop(games.index(sio.get_session(sid).game))
print('disconnect ', sid)
advertise_lobbies()
if send_metrics:
api.Metric.send(metric='online_players', points=[(int(time.time()), online_players)], tags=["server:backend", f"host:{os.environ['HOST']}"])
@sio.event
def create_room(sid, room_name):
@ -263,6 +283,8 @@ def start_game(sid):
ses: Player = sio.get_session(sid)
ses.game.start_game()
advertise_lobbies()
if send_metrics:
api.Metric.send(metric='start_game', points=[(int(time.time()), 1)], tags=["server:backend", f"host:{os.environ['HOST']}"])
@sio.event
def set_character(sid, name):