def __init__(self, table_name: str = None, schema_name: str = None): """ Creates a new instance of the persistence component. :param table_name: (optional) a table name. :param schema_name: (optional) a schema name. """ self.__config: ConfigParams = None self.__references: IReferences = None self.__opened: bool = None self.__local_connection: bool = None self.__schema_statements = [] # The dependency resolver. self._dependency_resolver: DependencyResolver = DependencyResolver(self._default_config) # The logger. self._logger: CompositeLogger = CompositeLogger() # The MySQL connection component. self._connection: MySqlConnection = None # The MySQL connection pool object. self._client: Any = None # The MySQL database name. self._database_name: str = None # The MySQL table object. self._table_name: str = None # Max number of objects in data pages self._max_page_size: int = 100 self._table_name = table_name self._schema_name = schema_name
def __init__(self): """ Creates a new instance of the client. """ # The HTTP client. self._client: Any = None # The remote service uri which is calculated on open. self._uri: str = None # The invocation timeout in milliseconds. self._timeout = 1000 # The connection resolver. self._connection_resolver: HttpConnectionResolver = HttpConnectionResolver( ) # The logger. self._logger: CompositeLogger = CompositeLogger() # The performance counters. self._counters: CompositeCounters = CompositeCounters() # The tracer. self._tracer: CompositeTracer = CompositeTracer() # The configuration options. self._options: ConfigParams = ConfigParams() # The base route. self._base_route: str = None # The number of retries. self._retries = 1 # The default headers to be added to every request. self._headers: dict = {} # The connection timeout in milliseconds. self._connect_timeout = 1000 self._correlation_id_location: str = "query"
def open(self, correlation_id): """ Opens the component. :param correlation_id: (optional) transaction id to trace execution through call chain. """ if self._references is not None: raise InvalidStateException(correlation_id, "ALREADY_OPENED", "Container was already opened") try: self._logger.trace(correlation_id, "Starting container.") # Create references with configured components self._references = ContainerReferences() self._init_references(self._references) self._references.put_from_config(self._config) self.set_references(self._references) # Get custom description if available info_descriptor = Descriptor("*", "context-info", "*", "*", "*") self._info = self._references.get_one_optional(info_descriptor) # Reference and open components self._references.open(correlation_id) # Get reference to logger self._logger = CompositeLogger(self._references) self._logger.info(correlation_id, "Container " + self._info.name + " started.") except Exception as ex: self._logger.error(correlation_id, ex, "Failed to start container") traceback.print_exc() raise ex
def __init__(self): # The dependency resolver. self._dependency_resolver: DependencyResolver = DependencyResolver( self._default_config) # The logger. self._logger: CompositeLogger = CompositeLogger() # The performance counters. self._counters: CompositeCounters = CompositeCounters() self._debug = False # The base route. self._base_route: str = None # The HTTP endpoint that exposes this service. self._endpoint: HttpEndpoint = None # The tracer. self._tracer: CompositeTracer = CompositeTracer() self._config: ConfigParams = None self._swagger_service: ISwaggerService = None self._swagger_enabled = False self._swagger_route = 'swagger' self.__local_endpoint: bool = None self.__references: IReferences = None self.__opened: bool = None
def __init__(self): """ Creates a new instance of the client. """ self._logger = CompositeLogger() self._counters = CompositeCounters() self._dependency_resolver = DependencyResolver() self._dependency_resolver.put('controller', 'none')
def __init__(self): self._default_config = ConfigParams.from_tuples( "base_route", None, "dependencies.endpoint", "*:endpoint:http:*:1.0") # self._registered = False self._dependency_resolver = DependencyResolver() self._logger = CompositeLogger() self._counters = CompositeCounters()
def setup_method(self, method): refs = References.from_tuples( Descriptor('pip-services-commons', 'logger', 'console', 'default', '1.0'), ConsoleLogger(), Descriptor('pip-services-commons', 'logger', 'null', 'default', '1.0'), NullLogger()) self.log = CompositeLogger(refs) self.fixture = LoggerFixture(self.log)
def setup_method(self, method): refs = References.from_tuples( Descriptor("pip-services", "logger", "null", "default", "1.0"), NullLogger(), Descriptor("pip-services", "logger", "console", "default", "1.0"), ConsoleLogger()) self.log = CompositeLogger(refs) self.fixture = LoggerFixture(self.log)
def __init__(self, name=None): """ Creates a new instance of the message queue. :param name: (optional) a queue name """ self._lock = threading.Lock() self._logger = CompositeLogger() self._counters = CompositeCounters() self._connection_resolver = ConnectionResolver() self._credential_resolver = CredentialResolver() self._name = name
def __init__(self, loader = None, saver = None): """ Creates a new instance of the persistence. :param loader: (optional) a loader to load items from external datasource. :param saver: (optional) a saver to save items to external datasource. """ self._lock = threading.Lock() self._logger = CompositeLogger() self._items = [] self._loader = loader self._saver = saver
def __init__(self): """ Creates a new instance of the performance counters. """ super(PrometheusCounters, self).__init__() self.__logger = CompositeLogger() self.__connection_resolver = HttpConnectionResolver() self.__opened = False self.__source: str = None self.__instance: str = None self.__push_enabled: bool = None self.__client: Any = None self.__request_route: str = None
def __init__(self): """ Creates a new instance of the client. """ self._connection_resolver = HttpConnectionResolver() self._default_config = ConfigParams.from_tuples( "connection.protocol", "http", "connection.host", "0.0.0.0", "connection.port", 3000, "options.timeout", 10000, "options.request_max_size", 1024 * 1024, "options.connect_timeout", 10000, "options.retries", 3, "options.debug", True) self._logger = CompositeLogger() self._counters = CompositeCounters() self._options = ConfigParams() self._headers = {}
def __init__(self, loader: ILoader = None, saver: ISaver = None): """ Creates a new instance of the persistence. :param loader: (optional) a loader to load items from external datasource. :param saver: (optional) a saver to save items to external datasource. """ self._lock: threading.Lock = threading.Lock() self._logger: CompositeLogger = CompositeLogger() self._items: List[Any] = [] self._loader: ILoader = loader self._saver: ISaver = saver self._opened: bool = False self._max_page_size = 100
def __init__(self): """ Creates HttpEndpoint """ self._default_config = ConfigParams.from_tuples( "connection.protocol", "http", "connection.host", "0.0.0.0", "connection.port", 3000, "credential.ssl_key_file", None, "credential.ssl_crt_file", None, "credential.ssl_ca_file", None, "options.maintenance_enabled", False, "options.request_max_size", 1024 * 1024, "options.file_max_size", 200 * 1024 * 1024, "connection.connect_timeout", 60000, "connection.debug", True) self._connection_resolver = HttpConnectionResolver() self._logger = CompositeLogger() self._counters = CompositeCounters() self._registrations = []
def __init__(self): self.__default_config = ConfigParams.from_tuples( "options.connect_timeout", 0, "options.idle_timeout", 10000, "options.max_pool_size", 3) # The logger. self._logger: CompositeLogger = CompositeLogger() # The connection resolver. self._connection_resolver: MySqlConnectionResolver = MySqlConnectionResolver( ) # The configuration options. self._options: ConfigParams = ConfigParams() # The MySQL connection pool object. self._connection: Any = None # The MySQL database name. self._database_name: str = None
def __init__(self): """ Creates a new instance of the client. """ # The controller reference. self._controller: Any = None # The open flag. self._opened: bool = True # The logger. self._logger: CompositeLogger = CompositeLogger() # The tracer. self._tracer: CompositeTracer = CompositeTracer() # The performance counters self._counters: CompositeCounters = CompositeCounters() # The dependency resolver to get controller reference. self._dependency_resolver: DependencyResolver = DependencyResolver() self._dependency_resolver.put('controller', 'none')
def __init__(self): """ Creates HttpEndpoint """ self.__service = None self.__server = None self.__maintenance_enabled: bool = False self.__file_max_size = 200 * 1024 * 1024 self.__protocol_upgrade_enabled: bool = False self.__uri: str = None self.__connection_resolver: HttpConnectionResolver = HttpConnectionResolver( ) self.__logger: CompositeLogger = CompositeLogger() self.__counters: CompositeCounters = CompositeCounters() self.__registrations: List[IRegisterable] = [] self.__allowed_headers: List[str] = ["correlation_id"] self.__allowed_origins: List[str] = []
def __init__(self, name: str = None, capabilities: MessagingCapabilities = None): """ Creates a new instance of the message queue. :param name: (optional) a queue name :param capabilities: (optional) a capabilities of this message queue """ self._lock: threading.Lock = threading.Lock() self._event = threading.Event() self._capabilities: MessagingCapabilities = None self._logger: CompositeLogger = CompositeLogger() self._counters: CompositeCounters = CompositeCounters() self._connection_resolver: ConnectionResolver = ConnectionResolver() self._credential_resolver: CredentialResolver = CredentialResolver() self._name: str = name self._capabilities = capabilities or \ MessagingCapabilities(False, False, False, False, False, False, False, False, False)
class FacadeRoutes(ABC, IConfigurable, IReferenceable): _logger = CompositeLogger() _counters = CompositeCounters() _dependencyResolver = DependencyResolver() _service: IFacadeService def __init__(self): self._dependencyResolver.put( "service", Descriptor("pip-services-facade", "service", "*", "*", "*")) def configure(self, config): self._dependencyResolver.configure(config) def set_references(self, references): self._logger.set_references(references) self._counters.set_references(references) self._dependencyResolver.set_references(references) self._service = self._dependencyResolver.get_one_required('service') self._register() def instrument(self, correlation_id, method, route): self._logger.debug(correlation_id, "Calling {} {}", method, route) self._counters.increment_one(route + "." + method + ".calls") def get_correlation_id(self, req): return req.query.get("correlation_id") def register_route(self, method, route, action): def action_curl(req=None, res=None): correlation_id = self.get_correlation_id(req) self.instrument(correlation_id, method, route) action(req, res) self._service.register_route(method, route, action_curl) @abstractmethod def _register(self): pass
class FacadeService(IConfigurable, IReferenceable, IFacadeService): _root_path = '' _partition = SessionMiddleware(bottle.Bottle(catchall=True, autojson=True)).app _dependency_resolver = DependencyResolver() _logger = CompositeLogger() def configure(self, config): self._root_path = config.get_as_string_with_default('root_path', self._root_path) if len(self._root_path) > 0 and not (self._root_path.startswith('/')): self._root_path = '/' + self._root_path def set_references(self, references): self._dependency_resolver.set_references(references) self._logger.set_references(references) def get_root_path(self): return self._root_path def register_middleware(self, action): self._partition.add_hook('before_request', action) def register_middleware_for_path(self, path, action): self._partition.add_hook('before_request', lambda: action() if not ( path is not None and path != '' and bottle.request.url.startswith(path)) else None) def register_route(self, method, route, action): if method == 'del': method = 'delete' self._logger.debug(None, 'Registering route {} {}', method, self.get_root_path() + route) self._partition.route(route, method, action) def register_route_with_auth(self, method, route, action, authorize): if method == 'del': method = 'delete' self._logger.debug(None, 'Registering route {} {}', method, self.get_root_path() + route) self._partition.route(route, method, action, authorize=authorize)
def __init__(self, collection: str = None): """ Creates a new instance of the persistence component. :param collection: (optional) a collection name. """ self._lock: threading.Lock = threading.Lock() self._connection_resolver: MongoDbConnectionResolver = MongoDbConnectionResolver( ) self._options: ConfigParams = ConfigParams() # The logger. self._logger: CompositeLogger = CompositeLogger() # The dependency resolver. self._dependency_resolver = DependencyResolver(self.__default_config) # The MongoDB database name. self._database_name: str = None # The MongoDb database object. self._db: Any = None # The MongoDb collection object. self._collection: Collection = None # The MongoDB connection object. self._client: Any = None # The MongoDB connection component. self._connection: MongoDbConnection = None self._max_page_size = 100 # The MongoDB colleciton object. self._collection_name: str = collection self.__config: ConfigParams = None self.__references: IReferences = None self.__opened = False self.__local_connection = False self.__indexes: List[MongoDbIndex] = []
class LogTracer(IConfigurable, IReferenceable, ITracer): """ Tracer that dumps recorded traces to logger. ### Configuration parameters ### - options: - log_level: log level to record traces (default: debug) ### References ### - `\*:logger:\*:\*:1.0` :class:`ILogger <pip_services3_components.log.ILogger.ILogger>` components to dump the captured counters - `\*:context-info:\*:\*:1.0` (optional) :class:`ContextInfo <pip_services3_components.info.ContextInfo.ContextInfo>` to detect the context id and specify counters source See :class:`ITracer <pip_services3_components.trace.ITracer.ITracer>`, :class:`CachedCounters <pip_services3_components.count.CachedCounters.CachedCounters>`, :class:`CompositeLogger <pip_services3_components.log.CompositeLogger.CompositeLogger>` Example: .. code-block:: python tracer = LogTracer() tracer.set_references(References.from_tuples( Descriptor("pip-services", "logger", "console", "default", "1.0"), ConsoleLogger() )) timing = tracer.begin_trace("123", "mycomponent", "mymethod") try: ... timing.endTrace() except Exception as err: timing.end_failure(err) """ __LOGGER = CompositeLogger() def __init__(self): """ Creates a new instance of the tracer. """ self.__log_level = LogLevel.Debug def configure(self, config: ConfigParams): """ Configures component by passing configuration parameters. :param config: configuration parameters to be set. """ self.__log_level = LogLevelConverter.to_log_level(config.get_as_object('options.log_level'), self.__log_level) def set_references(self, references: IReferences): """ Sets references to dependent components. :param references: references to locate the component dependencies. """ self.__LOGGER.set_references(references) def __log_trace(self, correlation_id: Optional[str], component: str, operation: str, error: Optional[Exception], duration: float): builder = '' if error is not None: builder += 'Failed to execute ' else: builder += 'Executed ' builder += component builder += "." builder += operation if duration > 0: builder += " in " + str(duration) + " msec" if error is not None: self.__LOGGER.error(correlation_id, error, builder) else: self.__LOGGER.log(self.__log_level, correlation_id, None, builder) def trace(self, correlation_id: Optional[str], component: str, operation: str, duration: float): """ Records an operation trace with its name and duration :param correlation_id: (optional) transaction id to trace execution through call chain. :param component: a name of called component :param operation: a name of the executed operation. :param duration: execution duration in milliseconds. """ self.__log_trace(correlation_id, component, operation, None, duration) def begin_trace(self, correlation_id: Optional[str], component: str, operation: str) -> TraceTiming: """ Begings recording an operation trace :param correlation_id: (optional) transaction id to trace execution through call chain. :param component: a name of called component :param operation: a name of the executed operation. :return: a trace timing object. """ return TraceTiming(correlation_id, component, operation, self)
class FacadeOperations(ABC, IConfigurable, IReferenceable): _logger = CompositeLogger() _counters = CompositeCounters() _dependencyResolver = DependencyResolver() def configure(self, config): self._dependencyResolver.configure(config) def set_references(self, references): self._logger.set_references(references) self._counters.set_references(references) self._dependencyResolver.set_references(references) def _get_correlation_id(self, req): return req.query.get("correlation_id") def _get_filter_params(self, req): key_value_req = {} for key, value in req.query.items: if key not in ['skip', 'take', 'total']: key_value_req[key] = value filter = FilterParams.from_value(key_value_req) return filter def _get_paging_params(self, req): key_value_req = {} for key, value in req.query.items: if key in ['skip', 'take', 'total']: key_value_req[key] = value filter = FilterParams.from_value(key_value_req) return filter def _send_result(self, res): return HttpResponseSender.send_result(res) def _send_empty_result(self, res): return HttpResponseSender.send_empty_result(res) def _send_created_result(self, res): return HttpResponseSender.send_created_result(res) def _send_deleted_result(self, res): return HttpResponseSender.send_deleted_result(res) def _send_error(self, err): HttpResponseSender.send_error(err) def _send_bad_request(self, req, err, message): correlation_id = self._get_correlation_id(req) error = BadRequestException(correlation_id, 'BAD_REQUEST', message) self._send_error(error) def _send_unauthorized(self, req, message): correlation_id = self._get_correlation_id(req) error = UnauthorizedException(correlation_id, 'UNAUTHORIZED', message) self._send_error(error) def _send_not_found(self, req, message): correlation_id = self._get_correlation_id(req) error = NotFoundException(correlation_id, 'NOT_FOUND', message) self._send_error(error) def _send_conflict(self, req, message): correlation_id = self._get_correlation_id(req) error = ConflictException(correlation_id, 'CONFLICT', message) self._send_error(error) def _send_session_expired(self, req, message): correlation_id = self._get_correlation_id(req) error = UnknownException(correlation_id, 'SESSION_EXPIRED', message) error.status = 440 self._send_error(error) def _send_internal_error(self, req, message): correlation_id = self._get_correlation_id(req) error = UnknownException(correlation_id, 'INTERNAL', message) self._send_error(error) def _send_server_unavailable(self, req, message): correlation_id = self._get_correlation_id(req) error = ConflictException(correlation_id, 'SERVER_UNAVAILABLE', message) error.status = 503 self._send_error(error)
def __init__(self): self.__message = "Hello World!" self.__logger = CompositeLogger() self.__timer = FixedRateTimer(self, 1000, 1000) self.__counter = 0