Skip to content

Commit 7c40b30

Browse files
authored
Add X-Request-ID header for tracing (#191)
1 parent b7fd2af commit 7c40b30

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

alertaclient/api.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import os
5+
import uuid
56
from datetime import datetime
67
from http.client import HTTPConnection
78
from urllib.parse import urlencode
@@ -483,6 +484,13 @@ def __init__(self, endpoint, key=None, token=None, username=None, password=None,
483484

484485
self.debug = debug
485486

487+
@staticmethod
488+
def headers():
489+
return {
490+
'X-Request-ID': str(uuid.uuid4()),
491+
'Content-Type': 'application/json'
492+
}
493+
486494
def get(self, path, query=None, **kwargs):
487495
query = query or []
488496
if 'page' in kwargs:
@@ -492,27 +500,25 @@ def get(self, path, query=None, **kwargs):
492500

493501
url = self.endpoint + path + '?' + urlencode(query, doseq=True)
494502
try:
495-
response = self.session.get(url, auth=self.auth, timeout=self.timeout)
503+
response = self.session.get(url, headers=self.headers(), auth=self.auth, timeout=self.timeout)
496504
except requests.exceptions.RequestException:
497505
raise
498506
return self._handle_error(response)
499507

500508
def post(self, path, data=None):
501509
url = self.endpoint + path
502-
headers = {'Content-Type': 'application/json'}
503510
try:
504511
response = self.session.post(url, data=json.dumps(data, cls=CustomJsonEncoder),
505-
headers=headers, auth=self.auth, timeout=self.timeout)
512+
headers=self.headers(), auth=self.auth, timeout=self.timeout)
506513
except requests.exceptions.RequestException:
507514
raise
508515
return self._handle_error(response)
509516

510517
def put(self, path, data=None):
511518
url = self.endpoint + path
512-
headers = {'Content-Type': 'application/json'}
513519
try:
514520
response = self.session.put(url, data=json.dumps(data, cls=CustomJsonEncoder),
515-
headers=headers, auth=self.auth, timeout=self.timeout)
521+
headers=self.headers(), auth=self.auth, timeout=self.timeout)
516522
except requests.exceptions.RequestException:
517523
raise
518524
return self._handle_error(response)
@@ -521,7 +527,7 @@ def delete(self, path):
521527
url = self.endpoint + path
522528

523529
try:
524-
response = self.session.delete(url, auth=self.auth, timeout=self.timeout)
530+
response = self.session.delete(url, headers=self.headers(), auth=self.auth, timeout=self.timeout)
525531
except requests.exceptions.RequestException:
526532
raise
527533
return self._handle_error(response)

0 commit comments

Comments
 (0)