|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/*DESCRIPTION |
| 8 | +Test that the Predis instrumentation still creates metrics when the transaction |
| 9 | +is restarted mid-request. |
| 10 | +*/ |
| 11 | + |
| 12 | +/*SKIPIF |
| 13 | +<?php |
| 14 | +if (!function_exists('newrelic_get_trace_json')) { |
| 15 | + die("skip: release builds of the agent do not include newrelic_get_trace_json()"); |
| 16 | +} |
| 17 | +*/ |
| 18 | + |
| 19 | +/*INI |
| 20 | +newrelic.datastore_tracer.database_name_reporting.enabled = 0 |
| 21 | +newrelic.datastore_tracer.instance_reporting.enabled = 0 |
| 22 | +newrelic.transaction_tracer.explain_enabled = true |
| 23 | +newrelic.transaction_tracer.explain_threshold = 0 |
| 24 | +*/ |
| 25 | + |
| 26 | +/*EXPECT |
| 27 | +ok - key does not exist |
| 28 | +ok - get key |
| 29 | +ok - no instance metadata found |
| 30 | +*/ |
| 31 | + |
| 32 | +/*EXPECT_METRICS |
| 33 | +[ |
| 34 | + "?? agent run id", |
| 35 | + "?? start time", |
| 36 | + "?? stop time", |
| 37 | + [ |
| 38 | + [{"name":"Datastore/all"}, [5, "??", "??", "??", "??", "??"]], |
| 39 | + [{"name":"Datastore/allOther"}, [5, "??", "??", "??", "??", "??"]], |
| 40 | + [{"name":"Datastore/Redis/all"}, [5, "??", "??", "??", "??", "??"]], |
| 41 | + [{"name":"Datastore/Redis/allOther"}, [5, "??", "??", "??", "??", "??"]], |
| 42 | + [{"name":"Datastore/operation/Redis/del"}, [1, "??", "??", "??", "??", "??"]], |
| 43 | + [{"name":"Datastore/operation/Redis/del","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 44 | + [{"name":"Datastore/operation/Redis/exists"}, [1, "??", "??", "??", "??", "??"]], |
| 45 | + [{"name":"Datastore/operation/Redis/exists","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 46 | + [{"name":"Datastore/operation/Redis/get"}, [1, "??", "??", "??", "??", "??"]], |
| 47 | + [{"name":"Datastore/operation/Redis/get","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 48 | + [{"name":"Datastore/operation/Redis/incr"}, [1, "??", "??", "??", "??", "??"]], |
| 49 | + [{"name":"Datastore/operation/Redis/incr","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 50 | + [{"name":"Datastore/operation/Redis/set"}, [1, "??", "??", "??", "??", "??"]], |
| 51 | + [{"name":"Datastore/operation/Redis/set","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 52 | + [{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/all"}, [1, "??", "??", "??", "??", "??"]], |
| 53 | + [{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther"}, [1, "??", "??", "??", "??", "??"]], |
| 54 | + [{"name":"OtherTransaction/all"}, [1, "??", "??", "??", "??", "??"]], |
| 55 | + [{"name":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 56 | + [{"name":"OtherTransactionTotalTime"}, [1, "??", "??", "??", "??", "??"]], |
| 57 | + [{"name":"OtherTransactionTotalTime/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 58 | + [{"name":"Supportability/api/set_appname/after"}, [1, "??", "??", "??", "??", "??"]], |
| 59 | + [{"name":"Supportability/api/set_appname/with_license"}, [1, "??", "??", "??", "??", "??"]], |
| 60 | + [{"name":"Supportability/Logging/Forwarding/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]], |
| 61 | + [{"name":"Supportability/Logging/Metrics/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]] |
| 62 | + ] |
| 63 | +] |
| 64 | +*/ |
| 65 | + |
| 66 | +/*EXPECT_TRACED_ERRORS null */ |
| 67 | + |
| 68 | +/* |
| 69 | +what_DONT_EXPECT_ANALYTICS_EVENTS |
| 70 | +null |
| 71 | +*/ |
| 72 | + |
| 73 | +use NewRelic\Integration\Transaction; |
| 74 | +use Predis\Client; |
| 75 | + |
| 76 | +require_once __DIR__.'/predis.inc'; |
| 77 | +require_once(__DIR__.'/../../include/config.php'); |
| 78 | +require_once(__DIR__.'/../../include/helpers.php'); |
| 79 | +require_once(__DIR__.'/../../include/tap.php'); |
| 80 | +require_once(__DIR__.'/../../include/integration.php'); |
| 81 | + |
| 82 | +global $REDIS_HOST, $REDIS_PORT; |
| 83 | +$client = new Predis\Client(array('host' => $REDIS_HOST, 'port' => $REDIS_PORT, 'database' => 0)); |
| 84 | + |
| 85 | +// Force predis to connect - usually it will lazily connect as needed. |
| 86 | +// In this case the 'select' operation will still be captured because |
| 87 | +// the connection wont occur until the 'exists' operation is executed |
| 88 | +// below. |
| 89 | +// Following code forces predis to connect now and therefore 'select' |
| 90 | +// operation happens before transaction is ended below and so will not |
| 91 | +// appear in the operations metrics |
| 92 | +try { |
| 93 | + $client->connect(); |
| 94 | +} catch (Exception $e) { |
| 95 | + die("skip: " . $e->getMessage() . "\n"); |
| 96 | +} |
| 97 | + |
| 98 | +// Restart the transaction. If you compare the expected metrics in this test to |
| 99 | +// test_basic.php, you'll note that the detection metrics go away (because |
| 100 | +// they're thrown away with the initial transaction), but we still look for the |
| 101 | +// Redis datastore metrics created by the $client method calls below. |
| 102 | +$appname = ini_get("newrelic.appname"); |
| 103 | +$license = ini_get("newrelic.license"); |
| 104 | +newrelic_set_appname($appname, $license, false); |
| 105 | + |
| 106 | +$key = uniqid(__FILE__, true); |
| 107 | +tap_equal(0, $client->exists($key), 'key does not exist'); |
| 108 | + |
| 109 | +$client->set($key, 1); |
| 110 | +$client->incr($key); |
| 111 | +tap_equal('2', $client->get($key), 'get key'); |
| 112 | + |
| 113 | +$client->del($key); |
| 114 | + |
| 115 | +// Test that we did not generate datastore instance metadata. |
| 116 | +$txn = new Transaction; |
| 117 | +foreach ($txn->getTrace()->findSegmentsByName('Datastore/operation/Redis/set') as $segment) { |
| 118 | + tap_equal(null, $segment->getDatastoreInstance(), 'no instance metadata found'); |
| 119 | + break; |
| 120 | +} |
0 commit comments