From 7abf591f5550dc5644a12c275f967c2ccb5d2d78 Mon Sep 17 00:00:00 2001 From: Komu Wairagu Date: Thu, 13 Dec 2018 13:12:54 +0300 Subject: [PATCH] add client id (#45) What: - add client_id to naz.Client Why: - this will help in monitoring and observability since all logs from one naz.Client class instance will be tagged with the corresponding `client_id`. You can use the `client_id` to tail/grep/splunk all logs from that naz.Client instance --- README.md | 18 +++++++++--------- cli/cli.py | 10 +++++++++- docs/config.md | 4 +++- examples/example_config.json | 2 +- naz/__version__.py | 2 +- naz/client.py | 10 +++++++++- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3e6daf03..4587a7aa 100644 --- a/README.md +++ b/README.md @@ -128,14 +128,14 @@ run: ```shell Naz: the SMPP client. -{'event': 'connect', 'stage': 'start'} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'connect', 'stage': 'end'} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'tranceiver_bind', 'stage': 'start'} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'send_data', 'stage': 'start', 'smpp_command': 'bind_transceiver', 'correlation_id': None} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'SimpleHook.request', 'stage': 'start', 'correlation_id': None} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'send_data', 'stage': 'end', 'smpp_command': 'bind_transceiver', 'correlation_id': None} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'tranceiver_bind', 'stage': 'end'} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} -{'event': 'send_forever', 'stage': 'start'} {'smsc_host': '127.0.0.1', 'system_id': 'smppclient1'} +{'event': 'naz.Client.connect', 'stage': 'start', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.Client.connect', 'stage': 'end', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.Client.tranceiver_bind', 'stage': 'start', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.Client.send_data', 'stage': 'start', 'smpp_event': 'bind_transceiver', 'correlation_id': None, 'msg': 'hello', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.SimpleHook.request', 'stage': 'start', 'smpp_event': 'bind_transceiver', 'correlation_id': None, 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.Client.send_data', 'stage': 'end', 'smpp_event': 'bind_transceiver', 'correlation_id': None, 'msg': 'hello', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.Client.tranceiver_bind', 'stage': 'end', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} +{'event': 'naz.Client.send_forever', 'stage': 'start', 'environment': 'production', 'release': 'canary', 'smsc_host': '127.0.0.1', 'system_id': 'smppclient1', 'client_id': '2VU55VT86KHWXTW7X'} ``` **NB:** @@ -197,7 +197,7 @@ cli = naz.Client( ) ``` and then these will show up in all log events. -by default, `naz` annotates all log events with `smsc_host` and `system_id` +by default, `naz` annotates all log events with `smsc_host`, `system_id` and `client_id` ##### 2.2 hooks a hook is a class with two methods `request` and `response`, ie it implements `naz`'s BaseHook interface as [defined here](https://github.com/komuw/naz/blob/master/naz/hooks.py). diff --git a/cli/cli.py b/cli/cli.py index 76872c7c..2bc76f64 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -1,6 +1,8 @@ import os import sys import json +import random +import string import asyncio import logging import inspect @@ -103,6 +105,8 @@ def make_parser(): def main(): """ """ + client_id = "".join(random.choices(string.ascii_uppercase + string.digits, k=17)) + logger = logging.getLogger("naz.cli") handler = logging.StreamHandler() formatter = logging.Formatter("%(message)s") @@ -131,7 +135,11 @@ def main(): if not log_metadata: log_metadata = {} log_metadata.update( - {"smsc_host": kwargs.get("smsc_host"), "system_id": kwargs.get("system_id")} + { + "smsc_host": kwargs.get("smsc_host"), + "system_id": kwargs.get("system_id"), + "client_id": client_id, + } ) extra_log_data = {"log_metadata": log_metadata} logger = naz.client.NazLoggingAdapter(logger, extra_log_data) diff --git a/docs/config.md b/docs/config.md index 5a369953..36afab7d 100644 --- a/docs/config.md +++ b/docs/config.md @@ -46,7 +46,9 @@ You are also encouraged to consult any documentation of the SMSC partner that yo --- | --- | --- encoding | encoding used to encode messages been sent to SMSC | gsm0338 sequence_generator | python class used to generate sequence_numbers| naz.sequence.SimpleSequenceGenerator -outboundqueue | python class implementing some queueing mechanism. messages to be sent to SMSC are queued using the said mechanism before been sent | N/A +outboundqueue | python class implementing some queueing mechanism. messages to be sent to SMSC are queued using the said +client_id | a unique string identifying a naz client class instance | "".join(random.choices(string.ascii_uppercase + string.digits, k=17)) +mechanism before been sent | N/A loglevel | the level at which to log | DEBUG log_metadata | metadata that will be included in all log statements | {"smsc_host": smsc_host, "system_id": system_id} codec_class | python class to be used to encode/decode messages | naz.nazcodec.NazCodec diff --git a/examples/example_config.json b/examples/example_config.json index c749c2b5..b67a0560 100644 --- a/examples/example_config.json +++ b/examples/example_config.json @@ -8,7 +8,7 @@ "sequence_generator": "examples.example_klasses.ExampleSeqGen", "loglevel": "INFO", "log_metadata": { - "environment": "production", + "environment": "staging", "release": "canary" }, "codec_errors_level": "ignore", diff --git a/naz/__version__.py b/naz/__version__.py index 5b9c6542..f5947d32 100644 --- a/naz/__version__.py +++ b/naz/__version__.py @@ -2,7 +2,7 @@ "__title__": "naz", "__description__": "Naz is an SMPP client.", "__url__": "https://github.com/komuw/naz", - "__version__": "0.0.7", + "__version__": "0.0.8", "__author__": "komuW", "__author_email__": "komuw05@gmail.com", "__license__": "MIT", diff --git a/naz/client.py b/naz/client.py index da05156c..8793e8bc 100644 --- a/naz/client.py +++ b/naz/client.py @@ -1,4 +1,6 @@ import struct +import random +import string import asyncio import logging import collections @@ -28,6 +30,7 @@ def __init__( system_id, password, outboundqueue, + client_id=None, system_type="", addr_ton=0, addr_npi=0, @@ -87,6 +90,9 @@ def __init__( self.system_id = system_id self.password = password self.outboundqueue = outboundqueue + self.client_id = client_id + if not self.client_id: + self.client_id = "".join(random.choices(string.ascii_uppercase + string.digits, k=17)) self.system_type = system_type self.interface_version = interface_version self.addr_ton = addr_ton @@ -103,7 +109,9 @@ def __init__( self.log_metadata = log_metadata if not self.log_metadata: self.log_metadata = {} - self.log_metadata.update({"smsc_host": self.smsc_host, "system_id": system_id}) + self.log_metadata.update( + {"smsc_host": self.smsc_host, "system_id": system_id, "client_id": self.client_id} + ) self.codec_errors_level = codec_errors_level self.codec_class = codec_class