Skip to content

Commit

Permalink
added timestamp in the prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
saumya4751 committed Dec 17, 2024
1 parent 5f90852 commit f929773
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions caption_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@
REQUEST_COUNTER = Counter(
'capshnz_http_requests_total',
'Total HTTP requests by method, endpoint, status code, and client IP',
['method', 'endpoint', 'status_code', 'client_ip', 'user_agent', 'request_size', 'response_size'],
['timestamp', 'method', 'endpoint', 'status_code', 'client_ip', 'user_agent', 'request_size', 'response_size'],
registry=registry
)

REQUEST_COUNTER_JUST_ENDPOINT = Counter(
'capshnz_http_request_by_ip_endpoint_total',
'Total HTTP requests by IP and Endpoint',
['client_ip', 'endpoint'],
['client_ip', 'endpoint', 'timestamp'],
registry=registry
)

Expand Down Expand Up @@ -2577,6 +2577,11 @@ def get(self):
disconnect(conn)
return response

def get_pst_timestamp():
pst = timezone('America/Los_Angeles')
current_time = datetime.now(pst)
return current_time.strftime('%Y-%m-%d %H:%M:%S')

@app.before_request
def before_request():
g.start_time = time.time()
Expand Down Expand Up @@ -2608,10 +2613,12 @@ def after_request(response):
payload = request.get_json(silent=True) # Log JSON payloads if available
query_params = request.args.to_dict()

current_timestamp = get_pst_timestamp()

# Log details
logger.info(
f"IP: {client_ip}, Endpoint: {endpoint}, Method: {method}, "
f"Status Code: {status_code}, Latency: {latency:.3f}s, "
f"Status Code: {status_code}, Timestamp: {current_timestamp}, Latency: {latency:.3f}s, "
f"Request Size: {request_size} bytes, Response Size: {response_size} bytes, "
f"User-Agent: {user_agent}, Query: {query_params}, Payload: {payload}"
)
Expand All @@ -2625,10 +2632,12 @@ def after_request(response):

REQUEST_COUNTER_JUST_ENDPOINT.labels(
client_ip=client_ip,
endpoint=normalized_endpoint
endpoint=normalized_endpoint,
timestamp=current_timestamp
).inc()

REQUEST_COUNTER.labels(
timestamp=current_timestamp,
method=method,
endpoint=endpoint,
status_code=status_code,
Expand Down

0 comments on commit f929773

Please sign in to comment.