11import logging
2- from datetime import datetime , timedelta
2+ from datetime import datetime
3+ from threading import Lock
34
4- from datadog_api_client import Configuration , ThreadedApiClient , ApiClient
5+ from datadog_api_client import Configuration , ApiClient
56from datadog_api_client .v2 .api .logs_api import LogsApi
67from datadog_api_client .v2 .model .logs_list_request import LogsListRequest
78from datadog_api_client .v2 .model .logs_list_request_page import LogsListRequestPage
@@ -29,7 +30,42 @@ class LogDD(BaseModel):
2930 pod_name : str = Field (description = "Pod name where the logs has been emitted" )
3031
3132
32- class Datadog :
33+ class SingletonMeta (type ):
34+ """
35+ This is a thread-safe implementation of Singleton.
36+ """
37+
38+ _instances = {}
39+
40+ _lock : Lock = Lock ()
41+ """
42+ We now have a lock object that will be used to synchronize threads during
43+ first access to the Singleton.
44+ """
45+
46+ def __call__ (cls , * args , ** kwargs ):
47+ """
48+ Possible changes to the value of the `__init__` argument do not affect
49+ the returned instance.
50+ """
51+ # Now, imagine that the program has just been launched. Since there's no
52+ # Singleton instance yet, multiple threads can simultaneously pass the
53+ # previous conditional and reach this point almost at the same time. The
54+ # first of them will acquire lock and will proceed further, while the
55+ # rest will wait here.
56+ with cls ._lock :
57+ # The first thread to acquire the lock, reaches this conditional,
58+ # goes inside and creates the Singleton instance. Once it leaves the
59+ # lock block, a thread that might have been waiting for the lock
60+ # release may then enter this section. But since the Singleton field
61+ # is already initialized, the thread won't create a new object.
62+ if cls not in cls ._instances :
63+ instance = super ().__call__ (* args , ** kwargs )
64+ cls ._instances [cls ] = instance
65+ return cls ._instances [cls ]
66+
67+
68+ class Datadog (metaclass = SingletonMeta ):
3369 LIMIT_PER_QUERY_LOGS = 1000
3470 MAX_RETURN_LOGS = 100000
3571
@@ -46,6 +82,7 @@ def __init__(self):
4682 app_key = vault_api .get_secret_kv_store (DATADOG_SECRET_KEYS , "dd_app_key" )
4783
4884 self .configuration = Configuration ()
85+ self .configuration .server_variables ["site" ] = "datadoghq.com"
4986 self .configuration .api_key ["apiKeyAuth" ] = api_key
5087 self .configuration .api_key ["appKeyAuth" ] = app_key
5188 self .configuration .enable_retry = True
0 commit comments