Skip to content

Commit

Permalink
add client id (#45)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
komuw authored Dec 13, 2018
1 parent b001a3c commit 7abf591
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down Expand Up @@ -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).
Expand Down
10 changes: 9 additions & 1 deletion cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys
import json
import random
import string
import asyncio
import logging
import inspect
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"sequence_generator": "examples.example_klasses.ExampleSeqGen",
"loglevel": "INFO",
"log_metadata": {
"environment": "production",
"environment": "staging",
"release": "canary"
},
"codec_errors_level": "ignore",
Expand Down
2 changes: 1 addition & 1 deletion naz/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion naz/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import struct
import random
import string
import asyncio
import logging
import collections
Expand Down Expand Up @@ -28,6 +30,7 @@ def __init__(
system_id,
password,
outboundqueue,
client_id=None,
system_type="",
addr_ton=0,
addr_npi=0,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7abf591

Please sign in to comment.