@@ -1888,9 +1888,79 @@ def test_statement_samples_config_invalid_number(integration_check, pg_instance,
18881888 integration_check (pg_instance )
18891889
18901890
1891+ class ObjectNotInPrerequisiteState (psycopg .errors .ObjectNotInPrerequisiteState ):
1892+ """
1893+ A fake ObjectNotInPrerequisiteState that allows setting pg_error on construction since ObjectNotInPrerequisiteState
1894+ has it as read-only and not settable at construction-time
1895+ """
1896+
1897+ def __init__ (self , pg_error ):
1898+ self .pg_error = pg_error
1899+
1900+ def __getattribute__ (self , attr ):
1901+ if attr == 'pgerror' :
1902+ return self .pg_error
1903+ else :
1904+ return super (ObjectNotInPrerequisiteState , self ).__getattribute__ (attr )
1905+
1906+ def __str__ (self ):
1907+ return self .pg_error
1908+
1909+
1910+ class UndefinedTable (psycopg .errors .UndefinedTable ):
1911+ """
1912+ A fake UndefinedTable that allows setting pg_error on construction since UndefinedTable
1913+ has it as read-only and not settable at construction-time
1914+ """
1915+
1916+ def __init__ (self , pg_error ):
1917+ self .pg_error = pg_error
1918+
1919+ def __getattribute__ (self , attr ):
1920+ if attr == 'pgerror' :
1921+ return self .pg_error
1922+ else :
1923+ return super (UndefinedTable , self ).__getattribute__ (attr )
1924+
1925+ def __str__ (self ):
1926+ return self .pg_error
1927+
1928+
18911929@pytest .mark .parametrize (
18921930 "error,metric_columns,expected_error_tag,expected_warnings" ,
18931931 [
1932+ (
1933+ ObjectNotInPrerequisiteState ('pg_stat_statements must be loaded via shared_preload_libraries' ),
1934+ [],
1935+ 'error:database-ObjectNotInPrerequisiteState-pg_stat_statements_not_loaded' ,
1936+ [
1937+ 'Unable to collect statement metrics because pg_stat_statements extension is '
1938+ "not loaded in database 'datadog_test'. See https://docs.datadoghq.com/database_monitoring/"
1939+ 'setup_postgres/troubleshooting#pg-stat-statements-not-loaded'
1940+ ' for more details\n code=pg-stat-statements-not-loaded dbname=datadog_test host=stubbed.hostname' ,
1941+ ],
1942+ ),
1943+ (
1944+ UndefinedTable ('ERROR: relation "pg_stat_statements" does not exist' ),
1945+ [],
1946+ 'error:database-UndefinedTable-pg_stat_statements_not_created' ,
1947+ [
1948+ 'Unable to collect statement metrics because pg_stat_statements is not '
1949+ "created in database 'datadog_test'. See https://docs.datadoghq.com/database_monitoring/"
1950+ 'setup_postgres/troubleshooting#pg-stat-statements-not-created'
1951+ ' for more details\n code=pg-stat-statements-not-created dbname=datadog_test host=stubbed.hostname' ,
1952+ ],
1953+ ),
1954+ (
1955+ ObjectNotInPrerequisiteState ('cannot insert into view' ),
1956+ [],
1957+ 'error:database-ObjectNotInPrerequisiteState' ,
1958+ [
1959+ "Unable to collect statement metrics because of an error running queries in database 'datadog_test'. "
1960+ "See https://docs.datadoghq.com/database_monitoring/troubleshooting for help: cannot insert into view\n "
1961+ "dbname=datadog_test host=stubbed.hostname"
1962+ ],
1963+ ),
18941964 (
18951965 psycopg .errors .DatabaseError ('connection reset' ),
18961966 [],
@@ -1938,52 +2008,6 @@ def test_statement_metrics_database_errors(
19382008 assert check .warnings == expected_warnings
19392009
19402010
1941- @pytest .mark .parametrize (
1942- "error,metric_columns,expected_error_tag,expected_warnings" ,
1943- [
1944- (
1945- 'not-created' ,
1946- [],
1947- 'error:database-UndefinedTable-pg_stat_statements_not_created' ,
1948- [
1949- 'Unable to collect statement metrics because pg_stat_statements is not '
1950- "created in database 'datadog_test'. See https://docs.datadoghq.com/database_monitoring/"
1951- 'setup_postgres/troubleshooting#pg-stat-statements-not-created'
1952- ' for more details\n code=pg-stat-statements-not-created dbname=datadog_test host=stubbed.hostname' ,
1953- ],
1954- ),
1955- ],
1956- )
1957- def test_statement_metrics_database_extension_errors (
1958- aggregator , integration_check , dbm_instance , error , metric_columns , expected_error_tag , expected_warnings
1959- ):
1960- # don't need samples for this test
1961- dbm_instance ['query_samples' ]['enabled' ] = False
1962- dbm_instance ['query_activity' ]['enabled' ] = False
1963- check = integration_check (dbm_instance )
1964-
1965- # Break databased on purpose to simulate the extension not being loaded or created
1966- if error == 'not-created' :
1967- superconn = _get_superconn (dbm_instance )
1968- with superconn .cursor () as cur :
1969- cur .execute ("DROP EXTENSION pg_stat_statements CASCADE;" )
1970-
1971- run_one_check (check )
1972-
1973- # Restore extension for next test
1974- cur .execute ("CREATE EXTENSION IF NOT EXISTS pg_stat_statements SCHEMA public;" )
1975-
1976- expected_tags = _get_expected_tags (
1977- check , dbm_instance , with_host = False , with_db = True , agent_hostname = 'stubbed.hostname'
1978- ) + [expected_error_tag ]
1979-
1980- aggregator .assert_metric (
1981- 'dd.postgres.statement_metrics.error' , value = 1.0 , count = 1 , tags = expected_tags , hostname = 'stubbed.hostname'
1982- )
1983-
1984- assert check .warnings == expected_warnings
1985-
1986-
19872011@pytest .mark .parametrize (
19882012 "pg_stat_statements_max_threshold,expected_warnings" ,
19892013 [
0 commit comments