Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from os.path import abspath, dirname, join

from tests.fixtures import (elasticapm_client, not_so_random,
from tests.fixtures import (elasticapm_client, instrument, not_so_random,
sending_elasticapm_client, validating_httpserver)
from tests.utils.compat import middleware_setting

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytest_localserver.http import ContentServer
from werkzeug.wrappers import Request, Response

import elasticapm
from elasticapm.base import Client

ERRORS_SCHEMA = 'https://raw.githubusercontent.com/elastic/apm-server/master/docs/spec/errors/payload.json'
Expand Down Expand Up @@ -117,3 +118,10 @@ def not_so_random():
random.seed(42)
yield
random.setstate(old_state)


@pytest.fixture()
def instrument():
elasticapm.instrument()
yield
elasticapm.uninstrument()
4 changes: 2 additions & 2 deletions tests/instrumentation/botocore_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@mock.patch("botocore.endpoint.Endpoint.make_request")
def test_botocore_instrumentation(mock_make_request, elasticapm_client):
def test_botocore_instrumentation(mock_make_request, elasticapm_client, instrument):
mock_response = mock.Mock()
mock_response.status_code = 200
mock_make_request.return_value = (mock_response, {})
Expand All @@ -26,7 +26,7 @@ def test_botocore_instrumentation(mock_make_request, elasticapm_client):



def test_nonstandard_endpoint_url(elasticapm_client):
def test_nonstandard_endpoint_url(instrument, elasticapm_client):
instrument = BotocoreInstrumentation()
elasticapm_client.begin_transaction('test')
module, method = BotocoreInstrumentation.instrument_list[0]
Expand Down
4 changes: 2 additions & 2 deletions tests/instrumentation/django_tests/template_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


@mock.patch("elasticapm.traces.TransactionsStore.should_collect")
def test_template_rendering(should_collect, django_elasticapm_client, client):
def test_template_rendering(should_collect, instrument, django_elasticapm_client, client):
should_collect.return_value = False
with override_settings(**middleware_setting(django.VERSION,
['elasticapm.contrib.django.middleware.TracingMiddleware'])):
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_template_rendering(should_collect, django_elasticapm_client, client):
@pytest.mark.skipif(django.VERSION < (1, 8),
reason='Jinja2 support introduced with Django 1.8')
@mock.patch("elasticapm.traces.TransactionsStore.should_collect")
def test_template_rendering_django18_jinja2(should_collect, django_elasticapm_client, client):
def test_template_rendering_django18_jinja2(should_collect, instrument, django_elasticapm_client, client):
should_collect.return_value = False
with override_settings(
TEMPLATES=TEMPLATES,
Expand Down
4 changes: 2 additions & 2 deletions tests/instrumentation/jinja2_tests/jinja2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def jinja_env():


@mock.patch("elasticapm.traces.TransactionsStore.should_collect")
def test_from_file(should_collect, jinja_env, elasticapm_client):
def test_from_file(should_collect, instrument, jinja_env, elasticapm_client):
should_collect.return_value = False
elasticapm_client.begin_transaction("transaction.test")
template = jinja_env.get_template('mytemplate.html')
Expand All @@ -32,7 +32,7 @@ def test_from_file(should_collect, jinja_env, elasticapm_client):
assert spans[0]['type'] == 'template.jinja2'


def test_from_string(elasticapm_client):
def test_from_string(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
template = Template("<html></html")
template.render()
Expand Down
13 changes: 3 additions & 10 deletions tests/instrumentation/psycopg2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ def test_multi_statement_sql():
def test_psycopg2_register_type(postgres_connection, elasticapm_client):
import psycopg2.extras

control.instrument()

try:
elasticapm_client.begin_transaction("web.django")
new_type = psycopg2.extras.register_uuid(None, postgres_connection)
Expand All @@ -237,8 +235,6 @@ def test_psycopg2_register_json(postgres_connection, elasticapm_client):
# separately
import psycopg2.extras

control.instrument()

try:
elasticapm_client.begin_transaction("web.django")
# as arg
Expand All @@ -257,8 +253,7 @@ def test_psycopg2_register_json(postgres_connection, elasticapm_client):

@pytest.mark.integrationtest
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
def test_psycopg2_tracing_outside_of_elasticapm_transaction(postgres_connection, elasticapm_client):
control.instrument()
def test_psycopg2_tracing_outside_of_elasticapm_transaction(instrument, postgres_connection, elasticapm_client):
cursor = postgres_connection.cursor()
# check that the cursor is a proxy, even though we're not in an elasticapm
# transaction
Expand All @@ -270,12 +265,11 @@ def test_psycopg2_tracing_outside_of_elasticapm_transaction(postgres_connection,

@pytest.mark.integrationtest
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
def test_psycopg2_select_LIKE(postgres_connection, elasticapm_client):
def test_psycopg2_select_LIKE(instrument, postgres_connection, elasticapm_client):
"""
Check that we pass queries with %-notation but without parameters
properly to the dbapi backend
"""
control.instrument()
cursor = postgres_connection.cursor()
query = "SELECT * FROM test WHERE name LIKE 't%'"

Expand All @@ -298,11 +292,10 @@ def test_psycopg2_select_LIKE(postgres_connection, elasticapm_client):
@pytest.mark.integrationtest
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
@pytest.mark.skipif(not has_sql_module, reason="psycopg2.sql module missing")
def test_psycopg2_composable_query_works(postgres_connection, elasticapm_client):
def test_psycopg2_composable_query_works(instrument, postgres_connection, elasticapm_client):
"""
Check that we parse queries that are psycopg2.sql.Composable correctly
"""
control.instrument()
cursor = postgres_connection.cursor()
query = sql.SQL("SELECT * FROM {table} WHERE {row} LIKE 't%' ORDER BY {row} DESC").format(
table=sql.Identifier('test'),
Expand Down
28 changes: 14 additions & 14 deletions tests/instrumentation/pymongo_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def mongo_database():

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_bulk_write(elasticapm_client, mongo_database):
def test_collection_bulk_write(instrument, elasticapm_client, mongo_database):
elasticapm_client.begin_transaction('transaction.test')
requests = [pymongo.InsertOne({'x': 1}),
pymongo.DeleteOne({'x': 1}),
Expand All @@ -37,7 +37,7 @@ def test_collection_bulk_write(elasticapm_client, mongo_database):


@pytest.mark.integrationtest
def test_collection_count(elasticapm_client, mongo_database):
def test_collection_count(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
mongo_database.blogposts.insert(blogpost)
Expand All @@ -54,7 +54,7 @@ def test_collection_count(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_delete_one(elasticapm_client, mongo_database):
def test_collection_delete_one(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
mongo_database.blogposts.insert_one(blogpost)
Expand All @@ -70,7 +70,7 @@ def test_collection_delete_one(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_delete_many(elasticapm_client, mongo_database):
def test_collection_delete_many(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
mongo_database.blogposts.insert_one(blogpost)
Expand All @@ -85,7 +85,7 @@ def test_collection_delete_many(elasticapm_client, mongo_database):


@pytest.mark.integrationtest
def test_collection_insert(elasticapm_client, mongo_database):
def test_collection_insert(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
elasticapm_client.begin_transaction('transaction.test')
Expand All @@ -100,7 +100,7 @@ def test_collection_insert(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_insert_one(elasticapm_client, mongo_database):
def test_collection_insert_one(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
elasticapm_client.begin_transaction('transaction.test')
Expand All @@ -115,7 +115,7 @@ def test_collection_insert_one(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_insert_many(elasticapm_client, mongo_database):
def test_collection_insert_many(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
elasticapm_client.begin_transaction('transaction.test')
Expand All @@ -130,7 +130,7 @@ def test_collection_insert_many(elasticapm_client, mongo_database):


@pytest.mark.integrationtest
def test_collection_find(elasticapm_client, mongo_database):
def test_collection_find(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
blogposts = []
Expand All @@ -151,7 +151,7 @@ def test_collection_find(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_find_one(elasticapm_client, mongo_database):
def test_collection_find_one(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
r = mongo_database.blogposts.insert_one(blogpost)
Expand All @@ -166,7 +166,7 @@ def test_collection_find_one(elasticapm_client, mongo_database):


@pytest.mark.integrationtest
def test_collection_remove(elasticapm_client, mongo_database):
def test_collection_remove(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
r = mongo_database.blogposts.insert(blogpost)
Expand All @@ -181,7 +181,7 @@ def test_collection_remove(elasticapm_client, mongo_database):


@pytest.mark.integrationtest
def test_collection_update(elasticapm_client, mongo_database):
def test_collection_update(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
r = mongo_database.blogposts.insert(blogpost)
Expand All @@ -198,7 +198,7 @@ def test_collection_update(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_update_one(elasticapm_client, mongo_database):
def test_collection_update_one(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
r = mongo_database.blogposts.insert(blogpost)
Expand All @@ -215,7 +215,7 @@ def test_collection_update_one(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (3, 0), reason='New in 3.0')
def test_collection_update_many(elasticapm_client, mongo_database):
def test_collection_update_many(instrument, elasticapm_client, mongo_database):
blogpost = {'author': 'Tom', 'text': 'Foo',
'date': datetime.datetime.utcnow()}
r = mongo_database.blogposts.insert(blogpost)
Expand All @@ -232,7 +232,7 @@ def test_collection_update_many(elasticapm_client, mongo_database):

@pytest.mark.integrationtest
@pytest.mark.skipif(pymongo.version_tuple < (2, 7), reason='New in 2.7')
def test_bulk_execute(elasticapm_client, mongo_database):
def test_bulk_execute(instrument, elasticapm_client, mongo_database):
elasticapm_client.begin_transaction('transaction.test')
bulk = mongo_database.test_bulk.initialize_ordered_bulk_op()
bulk.insert({'x': 'y'})
Expand Down
2 changes: 1 addition & 1 deletion tests/instrumentation/python_memcached_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@pytest.mark.integrationtest
def test_memcached(elasticapm_client):
def test_memcached(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_memcached", "test"):
host = os.environ.get('MEMCACHED_HOST', 'localhost')
Expand Down
7 changes: 3 additions & 4 deletions tests/instrumentation/redis_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from functools import partial

import mock
import pytest
import redis
from redis.client import StrictRedis
Expand All @@ -21,7 +20,7 @@ def redis_conn():


@pytest.mark.integrationtest
def test_pipeline(elasticapm_client, redis_conn):
def test_pipeline(instrument, elasticapm_client, redis_conn):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_pipeline", "test"):
pipeline = redis_conn.pipeline()
Expand All @@ -47,7 +46,7 @@ def test_pipeline(elasticapm_client, redis_conn):


@pytest.mark.integrationtest
def test_rq_patches_redis(elasticapm_client, redis_conn):
def test_rq_patches_redis(instrument, elasticapm_client, redis_conn):
# Let's go ahead and change how something important works
redis_conn._pipeline = partial(StrictRedis.pipeline, redis_conn)

Expand Down Expand Up @@ -77,7 +76,7 @@ def test_rq_patches_redis(elasticapm_client, redis_conn):


@pytest.mark.integrationtest
def test_redis_client(elasticapm_client, redis_conn):
def test_redis_client(instrument, elasticapm_client, redis_conn):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_redis_client", "test"):
redis_conn.rpush("mykey", "a", "b")
Expand Down
12 changes: 6 additions & 6 deletions tests/instrumentation/requests_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
responses.add('GET', '/', status=200, adding_headers={'Location': 'http://example.com/foo'})


def test_requests_instrumentation(elasticapm_client):
def test_requests_instrumentation(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_request", "test"):
# NOTE: The `allow_redirects` argument has to be set to `False`,
Expand All @@ -30,7 +30,7 @@ def test_requests_instrumentation(elasticapm_client):
assert 'http://example.com/' == spans[0]['context']['url']


def test_requests_instrumentation_via_session(elasticapm_client):
def test_requests_instrumentation_via_session(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_request", "test"):
s = requests.Session()
Expand All @@ -43,7 +43,7 @@ def test_requests_instrumentation_via_session(elasticapm_client):
assert 'http://example.com/' == spans[0]['context']['url']


def test_requests_instrumentation_via_prepared_request(elasticapm_client):
def test_requests_instrumentation_via_prepared_request(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_request", "test"):
r = requests.Request('get', 'http://example.com')
Expand All @@ -58,21 +58,21 @@ def test_requests_instrumentation_via_prepared_request(elasticapm_client):
assert 'http://example.com/' == spans[0]['context']['url']


def test_requests_instrumentation_malformed_none(elasticapm_client):
def test_requests_instrumentation_malformed_none(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_request", "test"):
with pytest.raises(MissingSchema):
requests.get(None)


def test_requests_instrumentation_malformed_schema(elasticapm_client):
def test_requests_instrumentation_malformed_schema(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_request", "test"):
with pytest.raises(MissingSchema):
requests.get('')


def test_requests_instrumentation_malformed_path(elasticapm_client):
def test_requests_instrumentation_malformed_path(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")
with capture_span("test_request", "test"):
with pytest.raises(InvalidURL):
Expand Down
2 changes: 1 addition & 1 deletion tests/instrumentation/sqlite_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sqlite3


def test_connect(elasticapm_client):
def test_connect(instrument, elasticapm_client):
elasticapm_client.begin_transaction("transaction.test")

conn = sqlite3.connect(":memory:")
Expand Down
2 changes: 1 addition & 1 deletion tests/instrumentation/urllib3_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@mock.patch("elasticapm.traces.TransactionsStore.should_collect")
def test_urllib3(should_collect, elasticapm_client, httpserver):
def test_urllib3(should_collect, instrument, elasticapm_client, httpserver):
should_collect.return_value = False
httpserver.serve_content('')
url = httpserver.url + '/hello_world'
Expand Down