Skip to content

Commit b1eef1d

Browse files
mfulblavarou
authored andcommitted
tests(agent): add more predis integration tests (#712)
This PR migrates the `predis` tests from multiverse to be included in the regular agent integration tests. Tested with PHP 5.5+.
1 parent 17c6f5c commit b1eef1d

File tree

3 files changed

+300
-0
lines changed

3 files changed

+300
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 basic Predis functionality with datastore instance reporting disabled.
9+
*/
10+
11+
/*SKIPIF
12+
<?php
13+
if (!function_exists('newrelic_get_trace_json')) {
14+
die("skip: release builds of the agent do not include newrelic_get_trace_json()");
15+
}
16+
*/
17+
18+
/*INI
19+
newrelic.datastore_tracer.database_name_reporting.enabled = 0
20+
newrelic.datastore_tracer.instance_reporting.enabled = 0
21+
newrelic.transaction_tracer.explain_enabled = true
22+
newrelic.transaction_tracer.explain_threshold = 0
23+
*/
24+
25+
/*EXPECT
26+
ok - key does not exist
27+
ok - get key
28+
ok - no instance metadata found
29+
*/
30+
31+
/*EXPECT_METRICS
32+
[
33+
"?? agent run id",
34+
"?? start time",
35+
"?? stop time",
36+
[
37+
[{"name":"Datastore/all"}, [5, "??", "??", "??", "??", "??"]],
38+
[{"name":"Datastore/allOther"}, [5, "??", "??", "??", "??", "??"]],
39+
[{"name":"Datastore/Redis/all"}, [5, "??", "??", "??", "??", "??"]],
40+
[{"name":"Datastore/Redis/allOther"}, [5, "??", "??", "??", "??", "??"]],
41+
[{"name":"Datastore/operation/Redis/del"}, [1, "??", "??", "??", "??", "??"]],
42+
[{"name":"Datastore/operation/Redis/del","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
43+
[{"name":"Datastore/operation/Redis/exists"}, [1, "??", "??", "??", "??", "??"]],
44+
[{"name":"Datastore/operation/Redis/exists","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
45+
[{"name":"Datastore/operation/Redis/get"}, [1, "??", "??", "??", "??", "??"]],
46+
[{"name":"Datastore/operation/Redis/get","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
47+
[{"name":"Datastore/operation/Redis/incr"}, [1, "??", "??", "??", "??", "??"]],
48+
[{"name":"Datastore/operation/Redis/incr","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
49+
[{"name":"Datastore/operation/Redis/set"}, [1, "??", "??", "??", "??", "??"]],
50+
[{"name":"Datastore/operation/Redis/set","scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
51+
[{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/all"}, [1, "??", "??", "??", "??", "??"]],
52+
[{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther"}, [1, "??", "??", "??", "??", "??"]],
53+
[{"name":"OtherTransaction/all"}, [1, "??", "??", "??", "??", "??"]],
54+
[{"name":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
55+
[{"name":"OtherTransactionTotalTime"}, [1, "??", "??", "??", "??", "??"]],
56+
[{"name":"OtherTransactionTotalTime/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
57+
[{"name":"Supportability/library/Predis/detected"}, [1, "??", "??", "??", "??", "??"]],
58+
[{"name":"Supportability/library/Guzzle 4-5/detected"}, [1, "??", "??", "??", "??", "??"]],
59+
[{"name":"Supportability/Logging/Forwarding/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]],
60+
[{"name":"Supportability/Logging/Metrics/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]]
61+
]
62+
]
63+
*/
64+
65+
/*EXPECT_TRACED_ERRORS null */
66+
67+
use NewRelic\Integration\Transaction;
68+
use Predis\Client;
69+
70+
require_once __DIR__.'/predis.inc';
71+
require_once(__DIR__.'/../../include/config.php');
72+
require_once(__DIR__.'/../../include/helpers.php');
73+
require_once(__DIR__.'/../../include/tap.php');
74+
require_once(__DIR__.'/../../include/integration.php');
75+
76+
global $REDIS_HOST, $REDIS_PORT;
77+
$client = new Predis\Client(array('host' => $REDIS_HOST, 'port' => $REDIS_PORT));
78+
try {
79+
$client->connect();
80+
} catch (Exception $e) {
81+
die("skip: " . $e->getMessage() . "\n");
82+
}
83+
84+
$key = uniqid(__FILE__, true);
85+
tap_equal(0, $client->exists($key), 'key does not exist');
86+
87+
$client->set($key, 1);
88+
$client->incr($key);
89+
tap_equal('2', $client->get($key), 'get key');
90+
91+
$client->del($key);
92+
93+
// Test that we did not generate datastore instance metadata.
94+
$txn = new Transaction;
95+
foreach ($txn->getTrace()->findSegmentsByName('Datastore/operation/Redis/set') as $segment) {
96+
tap_equal(null, $segment->getDatastoreInstance(), 'no instance metadata found');
97+
break;
98+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 basic Predis functionality with datastore instance reporting enabled.
9+
*/
10+
11+
/*SKIPIF
12+
<?php
13+
if (!function_exists('newrelic_get_trace_json')) {
14+
die("skip: release builds of the agent do not include newrelic_get_trace_json()");
15+
}
16+
*/
17+
18+
/*INI
19+
newrelic.datastore_tracer.database_name_reporting.enabled = 1
20+
newrelic.datastore_tracer.instance_reporting.enabled = 1
21+
newrelic.transaction_tracer.explain_enabled = true
22+
newrelic.transaction_tracer.explain_threshold = 0
23+
*/
24+
25+
/*EXPECT
26+
ok - key does not exist
27+
ok - get key
28+
ok - instance host matches
29+
ok - instance port matches
30+
ok - instance database matches
31+
ok - instance host matches
32+
ok - instance port matches
33+
ok - instance database matches
34+
ok - instance host matches
35+
ok - instance port matches
36+
ok - instance database matches
37+
ok - instance host matches
38+
ok - instance port matches
39+
ok - instance database matches
40+
ok - instance host matches
41+
ok - instance port matches
42+
ok - instance database matches
43+
ok - instance host matches
44+
ok - instance port matches
45+
ok - instance database matches
46+
*/
47+
48+
/*EXPECT_TRACED_ERRORS null */
49+
50+
use NewRelic\Integration\Transaction;
51+
use Predis\Client;
52+
53+
require_once __DIR__.'/predis.inc';
54+
require_once(__DIR__.'/../../include/config.php');
55+
require_once(__DIR__.'/../../include/helpers.php');
56+
require_once(__DIR__.'/../../include/tap.php');
57+
require_once(__DIR__.'/../../include/integration.php');
58+
59+
global $REDIS_HOST, $REDIS_PORT;
60+
$client = new Predis\Client(array('host' => $REDIS_HOST, 'port' => $REDIS_PORT, 'database' => 7));
61+
try {
62+
$client->connect();
63+
} catch (Exception $e) {
64+
die("skip: " . $e->getMessage() . "\n");
65+
}
66+
67+
$key = uniqid(__FILE__, true);
68+
tap_equal(0, $client->exists($key), 'key does not exist');
69+
70+
$client->set($key, 1);
71+
$client->incr($key);
72+
tap_equal('2', $client->get($key), 'get key');
73+
74+
$client->del($key);
75+
76+
$txn = new Transaction;
77+
foreach ($txn->getTrace()->findSegmentsWithDatastoreInstances() as $segment) {
78+
$instance = $segment->getDatastoreInstance();
79+
tap_assert($instance->isHost($REDIS_HOST), 'instance host matches');
80+
tap_equal((string) $REDIS_PORT, (string) $instance->portPathOrId, 'instance port matches');
81+
tap_equal("7", (string) $instance->databaseName, 'instance database matches');
82+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)