|
| 1 | +# BSD 3-Clause License |
| 2 | +# |
| 3 | +# Copyright (c) 2019, Elasticsearch BV |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# * Neither the name of the copyright holder nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +import pytest # isort:skip |
| 32 | + |
| 33 | +aioredis = pytest.importorskip("aioredis") # isort:skip |
| 34 | + |
| 35 | +import os |
| 36 | + |
| 37 | +from elasticapm.conf.constants import TRANSACTION |
| 38 | +from elasticapm.traces import capture_span |
| 39 | + |
| 40 | +pytestmark = [pytest.mark.asyncio, pytest.mark.aioredis] |
| 41 | + |
| 42 | +if "REDIS_HOST" not in os.environ: |
| 43 | + pytestmark.append(pytest.mark.skip("Skipping redis tests, no REDIS_HOST environment variable set")) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture() |
| 47 | +async def redis_conn(): |
| 48 | + _host = os.environ["REDIS_HOST"] |
| 49 | + _port = os.environ.get("REDIS_PORT", 6379) |
| 50 | + conn = await aioredis.create_redis_pool(f"redis://{_host}:{_port}") |
| 51 | + |
| 52 | + yield conn |
| 53 | + |
| 54 | + conn.close() |
| 55 | + await conn.wait_closed() |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.integrationtest |
| 59 | +async def test_pipeline(instrument, elasticapm_client, redis_conn): |
| 60 | + elasticapm_client.begin_transaction("transaction.test") |
| 61 | + with capture_span("test_pipeline", "test"): |
| 62 | + pipeline = redis_conn.pipeline() |
| 63 | + pipeline.rpush("mykey", "a", "b") |
| 64 | + pipeline.expire("mykey", 1000) |
| 65 | + await pipeline.execute() |
| 66 | + elasticapm_client.end_transaction("MyView") |
| 67 | + |
| 68 | + transactions = elasticapm_client.events[TRANSACTION] |
| 69 | + spans = elasticapm_client.spans_for_transaction(transactions[0]) |
| 70 | + |
| 71 | + assert spans[0]["name"] in ("StrictPipeline.execute", "Pipeline.execute") |
| 72 | + assert spans[0]["type"] == "db" |
| 73 | + assert spans[0]["subtype"] == "redis" |
| 74 | + assert spans[0]["action"] == "query" |
| 75 | + assert spans[0]["context"]["destination"] == { |
| 76 | + "address": os.environ.get("REDIS_HOST", "localhost"), |
| 77 | + "port": int(os.environ.get("REDIS_PORT", 6379)), |
| 78 | + "service": {"name": "aioredis", "resource": "redis", "type": "db"}, |
| 79 | + } |
| 80 | + |
| 81 | + assert spans[1]["name"] == "test_pipeline" |
| 82 | + assert spans[1]["type"] == "test" |
| 83 | + |
| 84 | + assert len(spans) == 2 |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.integrationtest |
| 88 | +async def test_redis_client(instrument, elasticapm_client, redis_conn): |
| 89 | + elasticapm_client.begin_transaction("transaction.test") |
| 90 | + with capture_span("test_redis_client", "test"): |
| 91 | + await redis_conn.rpush("mykey", "a", "b") |
| 92 | + await redis_conn.expire("mykey", 1000) |
| 93 | + elasticapm_client.end_transaction("MyView") |
| 94 | + |
| 95 | + transactions = elasticapm_client.events[TRANSACTION] |
| 96 | + spans = elasticapm_client.spans_for_transaction(transactions[0]) |
| 97 | + |
| 98 | + spans = sorted(spans, key=lambda x: x["name"]) |
| 99 | + |
| 100 | + assert {t["name"] for t in spans} == {"test_redis_client", "RPUSH", "EXPIRE"} |
| 101 | + |
| 102 | + assert spans[0]["name"] == "EXPIRE" |
| 103 | + assert spans[0]["type"] == "db" |
| 104 | + assert spans[0]["subtype"] == "redis" |
| 105 | + assert spans[0]["action"] == "query" |
| 106 | + assert spans[0]["context"]["destination"] == { |
| 107 | + "address": os.environ.get("REDIS_HOST", "localhost"), |
| 108 | + "port": int(os.environ.get("REDIS_PORT", 6379)), |
| 109 | + "service": {"name": "aioredis", "resource": "redis", "type": "db"}, |
| 110 | + } |
| 111 | + |
| 112 | + assert spans[1]["name"] == "RPUSH" |
| 113 | + assert spans[1]["type"] == "db" |
| 114 | + assert spans[1]["subtype"] == "redis" |
| 115 | + assert spans[1]["action"] == "query" |
| 116 | + assert spans[1]["context"]["destination"] == { |
| 117 | + "address": os.environ.get("REDIS_HOST", "localhost"), |
| 118 | + "port": int(os.environ.get("REDIS_PORT", 6379)), |
| 119 | + "service": {"name": "aioredis", "resource": "redis", "type": "db"}, |
| 120 | + } |
| 121 | + |
| 122 | + assert spans[2]["name"] == "test_redis_client" |
| 123 | + assert spans[2]["type"] == "test" |
| 124 | + |
| 125 | + assert len(spans) == 3 |
0 commit comments