4040 DataStoreStatusProvider , DataStoreUpdateSink ,
4141 FeatureStore , FlagTracker )
4242from ldclient .migrations import OpTracker , Stage
43+ from ldclient .plugin import (ApplicationMetadata , EnvironmentMetadata ,
44+ SdkMetadata )
45+ from ldclient .version import VERSION
4346from ldclient .versioned_data_kind import FEATURES , SEGMENTS , VersionedDataKind
4447
4548from .impl import AnyNum
@@ -223,8 +226,11 @@ def postfork(self, start_wait: float = 5):
223226 self .__start_up (start_wait )
224227
225228 def __start_up (self , start_wait : float ):
229+ environment_metadata = self .__get_environment_metadata ()
230+ plugin_hooks = self .__get_plugin_hooks (environment_metadata )
231+
226232 self .__hooks_lock = ReadWriteLock ()
227- self .__hooks = self ._config .hooks # type: List[Hook]
233+ self .__hooks = self ._config .hooks + plugin_hooks # type: List[Hook]
228234
229235 data_store_listeners = Listeners ()
230236 store_sink = DataStoreUpdateSinkImpl (data_store_listeners )
@@ -256,6 +262,8 @@ def __start_up(self, start_wait: float):
256262
257263 diagnostic_accumulator = self ._set_event_processor (self ._config )
258264
265+ self .__register_plugins (environment_metadata )
266+
259267 update_processor_ready = threading .Event ()
260268 self ._update_processor = self ._make_update_processor (self ._config , self ._store , update_processor_ready , diagnostic_accumulator )
261269 self ._update_processor .start ()
@@ -273,6 +281,43 @@ def __start_up(self, start_wait: float):
273281 else :
274282 log .warning ("Initialization timeout exceeded for LaunchDarkly Client or an error occurred. " "Feature Flags may not yet be available." )
275283
284+ def __get_environment_metadata (self ) -> EnvironmentMetadata :
285+ sdk_metadata = SdkMetadata (
286+ name = "python-server-sdk" ,
287+ version = VERSION ,
288+ wrapper_name = self ._config .wrapper_name ,
289+ wrapper_version = self ._config .wrapper_version
290+ )
291+
292+ application_metadata = None
293+ if self ._config .application :
294+ application_metadata = ApplicationMetadata (
295+ id = self ._config .application .get ('id' ),
296+ version = self ._config .application .get ('version' ),
297+ )
298+
299+ return EnvironmentMetadata (
300+ sdk = sdk_metadata ,
301+ application = application_metadata ,
302+ sdk_key = self ._config .sdk_key
303+ )
304+
305+ def __get_plugin_hooks (self , environment_metadata : EnvironmentMetadata ) -> List [Hook ]:
306+ hooks = []
307+ for plugin in self ._config .plugins :
308+ try :
309+ hooks .extend (plugin .get_hooks (environment_metadata ))
310+ except Exception as e :
311+ log .error ("Error getting hooks from plugin %s: %s" , plugin .metadata .name , e )
312+ return hooks
313+
314+ def __register_plugins (self , environment_metadata : EnvironmentMetadata ):
315+ for plugin in self ._config .plugins :
316+ try :
317+ plugin .register (self , environment_metadata )
318+ except Exception as e :
319+ log .error ("Error registering plugin %s: %s" , plugin .metadata .name , e )
320+
276321 def _set_event_processor (self , config ):
277322 if config .offline or not config .send_events :
278323 self ._event_processor = NullEventProcessor ()
0 commit comments