Skip to content

Commit

Permalink
Use dedicated instance logger for connection messages (#9887)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored and mx-psi committed Aug 16, 2021
1 parent c7ca7c8 commit e18f9b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
32 changes: 16 additions & 16 deletions ibm_mq/datadog_checks/ibm_mq/connection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

import logging
from typing import TYPE_CHECKING

from datadog_checks.ibm_mq.config import IBMMQConfig

Expand All @@ -11,11 +10,12 @@
except ImportError:
pymqi = None

log = logging.getLogger(__file__)
if TYPE_CHECKING:
from datadog_checks.base.log import CheckLoggingAdapter


def get_queue_manager_connection(config):
# type: (IBMMQConfig) -> pymqi.QueueManager
def get_queue_manager_connection(config, logger):
# type: (IBMMQConfig, CheckLoggingAdapter) -> pymqi.QueueManager
"""
Get the queue manager connection
"""
Expand All @@ -25,38 +25,38 @@ def get_queue_manager_connection(config):
# This does not fix the memory leak but mitigate its likelihood.
# Details: https://github.com/dsuch/pymqi/issues/208
try:
get_normal_connection(config)
get_normal_connection(config, logger)
except pymqi.MQMIError as e:
log.debug("Tried normal connection before SSL connection. It failed with: %s", e)
logger.debug("Tried normal connection before SSL connection. It failed with: %s", e)
if e.reason == pymqi.CMQC.MQRC_UNKNOWN_CHANNEL_NAME:
raise
return get_ssl_connection(config)
return get_ssl_connection(config, logger)
else:
return get_normal_connection(config)
return get_normal_connection(config, logger)


def get_normal_connection(config):
# type: (IBMMQConfig) -> pymqi.QueueManager
def get_normal_connection(config, logger):
# type: (IBMMQConfig, CheckLoggingAdapter) -> pymqi.QueueManager
"""
Get the connection either with a username and password or without
"""
channel_definition = _get_channel_definition(config)
queue_manager = pymqi.QueueManager(None)

if config.username and config.password:
log.debug("connecting with username and password")
logger.debug("connecting with username and password")

kwargs = {'user': config.username, 'password': config.password, 'cd': channel_definition}

queue_manager.connect_with_options(config.queue_manager_name, **kwargs)
else:
log.debug("connecting without a username and password")
logger.debug("connecting without a username and password")
queue_manager.connect_with_options(config.queue_manager_name, channel_definition)
return queue_manager


def get_ssl_connection(config):
# type: (IBMMQConfig) -> pymqi.QueueManager
def get_ssl_connection(config, logger):
# type: (IBMMQConfig, CheckLoggingAdapter) -> pymqi.QueueManager
"""
Get the connection with SSL
"""
Expand All @@ -78,7 +78,7 @@ def get_ssl_connection(config):
}
)

log.debug(
logger.debug(
"Create SSL connection with ConnectionName=%s, ChannelName=%s, Version=%s, SSLCipherSpec=%s, "
"KeyRepository=%s, CertificateLabel=%s, user=%s",
cd.ConnectionName,
Expand Down
2 changes: 1 addition & 1 deletion ibm_mq/datadog_checks/ibm_mq/ibm_mq.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, *args, **kwargs):

def check(self, _):
try:
queue_manager = connection.get_queue_manager_connection(self._config)
queue_manager = connection.get_queue_manager_connection(self._config, self.log)
self.service_check(self.SERVICE_CHECK, AgentCheck.OK, self._config.tags)
except Exception as e:
self.warning("cannot connect to queue manager: %s", e)
Expand Down
19 changes: 11 additions & 8 deletions ibm_mq/tests/test_ibm_mq_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ def test_ssl_connection_creation(instance):


def test_ssl_check_normal_connection_before_ssl_connection(instance_ssl_dummy):
import logging

import pymqi

logger = logging.getLogger(__file__)
config = IBMMQConfig(instance_ssl_dummy)

error = pymqi.MQMIError(pymqi.CMQC.MQCC_FAILED, pymqi.CMQC.MQRC_UNKNOWN_CHANNEL_NAME)
Expand All @@ -213,9 +216,9 @@ def test_ssl_check_normal_connection_before_ssl_connection(instance_ssl_dummy):
) as get_normal_connection, mock.patch('datadog_checks.ibm_mq.connection.get_ssl_connection') as get_ssl_connection:

with pytest.raises(pymqi.MQMIError):
get_queue_manager_connection(config)
get_queue_manager_connection(config, logger)

get_normal_connection.assert_called_with(config)
get_normal_connection.assert_called_with(config, logger)
assert not get_ssl_connection.called

# normal connection failed with with error other those listed in get_queue_manager_connection
Expand All @@ -227,17 +230,17 @@ def test_ssl_check_normal_connection_before_ssl_connection(instance_ssl_dummy):
'datadog_checks.ibm_mq.connection.get_ssl_connection'
) as get_ssl_connection:

get_queue_manager_connection(config)
get_queue_manager_connection(config, logger)

get_normal_connection.assert_called_with(config)
get_ssl_connection.assert_called_with(config)
get_normal_connection.assert_called_with(config, logger)
get_ssl_connection.assert_called_with(config, logger)

# no issue with normal connection
with mock.patch('datadog_checks.ibm_mq.connection.get_normal_connection') as get_normal_connection, mock.patch(
'datadog_checks.ibm_mq.connection.get_ssl_connection'
) as get_ssl_connection:

get_queue_manager_connection(config)
get_queue_manager_connection(config, logger)

get_normal_connection.assert_called_with(config)
get_ssl_connection.assert_called_with(config)
get_normal_connection.assert_called_with(config, logger)
get_ssl_connection.assert_called_with(config, logger)

0 comments on commit e18f9b2

Please sign in to comment.