Skip to content

Commit

Permalink
Add default freetds driver for Docker Agent (#6636)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang authored May 27, 2020
1 parent ca468aa commit a62fe8a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[ODBC Drivers]
FreeTDS=Installed

[FreeTDS]
Driver=/opt/datadog-agent/embedded/lib/libtdsodbc.so
4 changes: 4 additions & 0 deletions sqlserver/datadog_checks/sqlserver/sqlserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from datadog_checks.base import AgentCheck
from datadog_checks.base.config import is_affirmative

from .utils import set_default_driver_conf

try:
import adodbapi
except ImportError:
Expand All @@ -30,6 +32,8 @@
if adodbapi is None and pyodbc is None:
raise ImportError('adodbapi or pyodbc must be installed to use this check.')

set_default_driver_conf()

EVENT_TYPE = SOURCE_TYPE_NAME = 'sql server'
ALL_INSTANCES = 'ALL'
VALID_METRIC_TYPES = ('gauge', 'rate', 'histogram')
Expand Down
16 changes: 16 additions & 0 deletions sqlserver/datadog_checks/sqlserver/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os

from datadog_checks.base.utils.platform import Platform

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
DRIVER_CONFIG_DIR = os.path.join(CURRENT_DIR, 'data', 'driver_config')


def set_default_driver_conf():
if Platform.is_containerized():
# Use default `./driver_config/odbcinst.ini` when Agent is running in docker.
# `freetds` is shipped with the Docker Agent.
os.environ.setdefault('ODBCSYSINI', DRIVER_CONFIG_DIR)
9 changes: 0 additions & 9 deletions sqlserver/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

import os

from datadog_checks.dev import get_docker_hostname, get_here
from datadog_checks.dev.utils import ON_MACOS, ON_WINDOWS
from datadog_checks.sqlserver import SQLServer
Expand Down Expand Up @@ -92,10 +90,3 @@ def get_local_driver():
}

FULL_E2E_CONFIG = {"init_config": INIT_CONFIG, "instances": [INSTANCE_E2E]}

E2E_METADATA = {
'start_commands': ['apt-get update', 'apt-get install -y tdsodbc unixodbc-dev'],
'docker_volumes': [
'{}:/opt/datadog-agent/embedded/etc/odbcinst.ini'.format(os.path.join(HERE, 'odbc', 'odbcinst.ini'))
],
}
3 changes: 1 addition & 2 deletions sqlserver/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from .common import (
DOCKER_SERVER,
E2E_METADATA,
FULL_E2E_CONFIG,
HERE,
INIT_CONFIG,
Expand Down Expand Up @@ -67,4 +66,4 @@ def sqlserver():
conditions=[WaitFor(sqlserver, wait=3, attempts=10)],
mount_logs=True,
):
yield FULL_E2E_CONFIG, E2E_METADATA
yield FULL_E2E_CONFIG
25 changes: 25 additions & 0 deletions sqlserver/tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os

import pytest

from datadog_checks.dev import EnvVars
from datadog_checks.sqlserver import SQLServer
from datadog_checks.sqlserver.sqlserver import SQLConnectionError
from datadog_checks.sqlserver.utils import set_default_driver_conf

# mark the whole module
pytestmark = pytest.mark.unit
Expand All @@ -20,3 +24,24 @@ def test_get_cursor(instance_sql2017):
check = SQLServer(CHECK_NAME, {}, [])
with pytest.raises(SQLConnectionError):
check.get_cursor(instance_sql2017, 'foo')


def test_set_default_driver_conf():
# Docker Agent with ODBCSYSINI env var
# The only case where we set ODBCSYSINI to the the default odbcinst.ini folder
with EnvVars({'DOCKER_DD_AGENT': 'true'}, ignore=['ODBCSYSINI']):
set_default_driver_conf()
assert os.environ['ODBCSYSINI'].endswith(os.path.join('data', 'driver_config'))

# `set_default_driver_conf` have no effect on the cases below
with EnvVars({'ODBCSYSINI': 'ABC', 'DOCKER_DD_AGENT': 'true'}):
set_default_driver_conf()
assert os.environ['ODBCSYSINI'] == 'ABC'

with EnvVars({}, ignore=['ODBCSYSINI']):
set_default_driver_conf()
assert 'ODBCSYSINI' not in os.environ

with EnvVars({'ODBCSYSINI': 'ABC'}):
set_default_driver_conf()
assert os.environ['ODBCSYSINI'] == 'ABC'

0 comments on commit a62fe8a

Please sign in to comment.