Skip to content

Commit aa5aaba

Browse files
committed
tests(agent): Adds log label forwarding tests for Monolog2
1 parent a413353 commit aa5aaba

18 files changed

+3932
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
/*
3+
* Copyright 2022 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
Test that Monolog2 instrumentation can forward logs with labels when:
9+
- label forwarding is enabled
10+
- newrelic.labels set to "label1:value1;label2:value2"
11+
- default label exclusion rule
12+
13+
Expect:
14+
- tags.label1 and tags.label2 to be added to common attributes for log event harvest
15+
- "Supportability/Logging/Labels/PHP/enabled" to exist and have a value of 1.
16+
*/
17+
18+
/*SKIPIF
19+
<?php
20+
21+
require('skipif.inc');
22+
23+
*/
24+
25+
/*INI
26+
newrelic.application_logging.enabled = true
27+
newrelic.application_logging.forwarding.enabled = true
28+
newrelic.application_logging.metrics.enabled = true
29+
newrelic.application_logging.forwarding.max_samples_stored = 10
30+
newrelic.application_logging.forwarding.log_level = DEBUG
31+
newrelic.application_logging.forwarding.labels.enabled = true
32+
newrelic.labels = "label1:value1;label2:value2"
33+
*/
34+
35+
/*EXPECT
36+
monolog2.DEBUG: debug []
37+
monolog2.INFO: info []
38+
monolog2.NOTICE: notice []
39+
monolog2.WARNING: warning []
40+
monolog2.ERROR: error []
41+
monolog2.CRITICAL: critical []
42+
monolog2.ALERT: alert []
43+
monolog2.EMERGENCY: emergency []
44+
*/
45+
46+
/*EXPECT_METRICS
47+
[
48+
"?? agent run id",
49+
"?? timeframe start",
50+
"?? timeframe stop",
51+
[
52+
[{"name": "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all"}, [1, "??", "??", "??", "??", "??"]],
53+
[{"name": "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther"}, [1, "??", "??", "??", "??", "??"]],
54+
[{"name": "Logging/lines"}, [8, "??", "??", "??", "??", "??"]],
55+
[{"name": "Logging/lines/ALERT"}, [1, "??", "??", "??", "??", "??"]],
56+
[{"name": "Logging/lines/CRITICAL"}, [1, "??", "??", "??", "??", "??"]],
57+
[{"name": "Logging/lines/DEBUG"}, [1, "??", "??", "??", "??", "??"]],
58+
[{"name": "Logging/lines/EMERGENCY"}, [1, "??", "??", "??", "??", "??"]],
59+
[{"name": "Logging/lines/ERROR"}, [1, "??", "??", "??", "??", "??"]],
60+
[{"name": "Logging/lines/INFO"}, [1, "??", "??", "??", "??", "??"]],
61+
[{"name": "Logging/lines/NOTICE"}, [1, "??", "??", "??", "??", "??"]],
62+
[{"name": "Logging/lines/WARNING"}, [1, "??", "??", "??", "??", "??"]],
63+
[{"name": "OtherTransaction/all"}, [1, "??", "??", "??", "??", "??"]],
64+
[{"name": "OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
65+
[{"name": "OtherTransactionTotalTime"}, [1, "??", "??", "??", "??", "??"]],
66+
[{"name": "OtherTransactionTotalTime/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
67+
[{"name": "Supportability/Logging/PHP/Monolog/enabled"}, [1, "??", "??", "??", "??", "??"]],
68+
[{"name": "Supportability/PHP/package/monolog/monolog/2/detected"}, [1, "??", "??", "??", "??", "??"]],
69+
[{"name": "Supportability/library/Monolog/detected"}, [1, "??", "??", "??", "??", "??"]],
70+
[{"name": "Supportability/Logging/LocalDecorating/PHP/disabled"}, [1, "??", "??", "??", "??", "??"]],
71+
[{"name": "Supportability/Logging/Forwarding/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]],
72+
[{"name": "Supportability/Logging/Metrics/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]],
73+
[{"name": "Supportability/Logging/Labels/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]]
74+
]
75+
]
76+
*/
77+
78+
79+
/*EXPECT_LOG_EVENTS
80+
[
81+
{
82+
"common": {
83+
"attributes": {
84+
"tags.label1": "value1",
85+
"tags.label2": "value2"
86+
}
87+
},
88+
"logs": [
89+
{
90+
"message": "error",
91+
"level": "ERROR",
92+
"timestamp": "??",
93+
"trace.id": "??",
94+
"span.id": "??",
95+
"entity.guid": "??",
96+
"entity.name": "tests/integration/logging/monolog2__FILE__",
97+
"hostname": "__HOST__"
98+
},
99+
{
100+
"message": "critical",
101+
"level": "CRITICAL",
102+
"timestamp": "??",
103+
"trace.id": "??",
104+
"span.id": "??",
105+
"entity.guid": "??",
106+
"entity.name": "tests/integration/logging/monolog2__FILE__",
107+
"hostname": "__HOST__"
108+
},
109+
{
110+
"message": "notice",
111+
"level": "NOTICE",
112+
"timestamp": "??",
113+
"trace.id": "??",
114+
"span.id": "??",
115+
"entity.guid": "??",
116+
"entity.name": "tests/integration/logging/monolog2__FILE__",
117+
"hostname": "__HOST__"
118+
},
119+
{
120+
"message": "warning",
121+
"level": "WARNING",
122+
"timestamp": "??",
123+
"trace.id": "??",
124+
"span.id": "??",
125+
"entity.guid": "??",
126+
"entity.name": "tests/integration/logging/monolog2__FILE__",
127+
"hostname": "__HOST__"
128+
},
129+
{
130+
"message": "info",
131+
"level": "INFO",
132+
"timestamp": "??",
133+
"trace.id": "??",
134+
"span.id": "??",
135+
"entity.guid": "??",
136+
"entity.name": "tests/integration/logging/monolog2__FILE__",
137+
"hostname": "__HOST__"
138+
},
139+
{
140+
"message": "alert",
141+
"level": "ALERT",
142+
"timestamp": "??",
143+
"trace.id": "??",
144+
"span.id": "??",
145+
"entity.guid": "??",
146+
"entity.name": "tests/integration/logging/monolog2__FILE__",
147+
"hostname": "__HOST__"
148+
},
149+
{
150+
"message": "emergency",
151+
"level": "EMERGENCY",
152+
"timestamp": "??",
153+
"trace.id": "??",
154+
"span.id": "??",
155+
"entity.guid": "??",
156+
"entity.name": "tests/integration/logging/monolog2__FILE__",
157+
"hostname": "__HOST__"
158+
},
159+
{
160+
"message": "debug",
161+
"level": "DEBUG",
162+
"timestamp": "??",
163+
"trace.id": "??",
164+
"span.id": "??",
165+
"entity.guid": "??",
166+
"entity.name": "tests/integration/logging/monolog2__FILE__",
167+
"hostname": "__HOST__"
168+
}
169+
]
170+
}
171+
]
172+
*/
173+
174+
require_once(realpath(dirname(__FILE__)) . '/../../../include/config.php');
175+
require_once(realpath(dirname(__FILE__)) . '/../../../include/monolog.php');
176+
require_monolog(2);
177+
178+
use Monolog\Logger;
179+
use Monolog\Handler\StreamHandler;
180+
use Monolog\Formatter\LineFormatter;
181+
182+
183+
function test_logging() {
184+
$logger = new Logger('monolog2');
185+
186+
$logfmt = "%channel%.%level_name%: %message% %context%\n";
187+
$formatter = new LineFormatter($logfmt);
188+
189+
$stdoutHandler = new StreamHandler('php://stdout', Logger::DEBUG);
190+
$stdoutHandler->setFormatter($formatter);
191+
192+
$logger->pushHandler($stdoutHandler);
193+
194+
// insert delays between log messages to allow priority sampling
195+
// to resolve that later messages have higher precedence
196+
// since timestamps are only millisecond resolution
197+
// without delays sometimes order in output will reflect
198+
// all having the same timestamp.
199+
$logger->debug("debug");
200+
usleep(10000);
201+
$logger->info("info");
202+
usleep(10000);
203+
$logger->notice("notice");
204+
usleep(10000);
205+
$logger->warning("warning");
206+
usleep(10000);
207+
$logger->error("error");
208+
usleep(10000);
209+
$logger->critical("critical");
210+
usleep(10000);
211+
$logger->alert("alert");
212+
usleep(10000);
213+
$logger->emergency("emergency");
214+
}
215+
216+
test_logging();

0 commit comments

Comments
 (0)