Skip to content

Commit

Permalink
Merge pull request #10 from infinite-options/ss_1209
Browse files Browse the repository at this point in the history
added timestamp in the prometheus
  • Loading branch information
saumya4751 authored Dec 17, 2024
2 parents f32a0d6 + f929773 commit f1f0ad4
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 f1f0ad4

Please sign in to comment.