Skip to content

Commit 1cf4ba3

Browse files
stympysubzero10
andauthored
fix: use RFC3339 extended date format (#200)
* Use RFC3339 extended date format * ci: tests --------- Co-authored-by: Pangratios Cosma <[email protected]>
1 parent 278b897 commit 1cf4ba3

6 files changed

+83
-63
lines changed

src/Honeybadger.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ public function event($eventTypeOrPayload, array $payload = null): void
232232
}
233233

234234
$event = array_merge(
235-
['ts' => (new DateTime())->format(DATE_ATOM)],
235+
['ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED)],
236236
$payload
237237
);
238238

239239
// if 'ts' is set, we need to make sure it's a string in the correct format
240240
if ($event['ts'] instanceof DateTime) {
241-
$event['ts'] = $event['ts']->format(DATE_ATOM);
241+
$event['ts'] = $event['ts']->format(DATE_RFC3339_EXTENDED);
242242
}
243243

244244
$this->events->addEvent($event);

src/LogEventHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function write($record): void
4545
*/
4646
protected function getEventPayloadFromMonologRecord($record): array {
4747
$payload = [
48-
'ts' => $record['datetime']->format(DATE_ATOM),
48+
'ts' => $record['datetime']->format(DATE_RFC3339_EXTENDED),
4949
'severity' => strtolower($record['level_name']),
5050
'message' => $record['message'],
5151
'channel' => $record['channel'],

tests/BulkEventDispatcherTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function it_adds_event_to_the_queue() {
2828
]);
2929
$hbMock = $this->createMock(HoneybadgerClient::class);
3030
$dispatcher = new BulkEventDispatcher($config, $hbMock);
31-
$dispatcher->addEvent(['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM)]);
31+
$dispatcher->addEvent(['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED)]);
3232
$this->assertTrue($dispatcher->hasEvents());
3333
}
3434

@@ -42,8 +42,8 @@ public function it_sends_events_when_threshold_is_reached() {
4242
]
4343
]);
4444
$events = [
45-
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM), 'message' => 'test 1'],
46-
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM), 'message' => 'test 2'],
45+
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), 'message' => 'test 1'],
46+
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), 'message' => 'test 2'],
4747
];
4848
$hbMock = $this->createMock(HoneybadgerClient::class);
4949
$hbMock->expects($this->once())->method('events')->with($events);
@@ -64,8 +64,8 @@ public function it_sends_events_when_interval_is_reached() {
6464
]
6565
]);
6666
$events = [
67-
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM), 'message' => 'test 1'],
68-
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM), 'message' => 'test 2'],
67+
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), 'message' => 'test 1'],
68+
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), 'message' => 'test 2'],
6969
];
7070
$hbMock = $this->createMock(HoneybadgerClient::class);
7171
$hbMock->expects($this->once())->method('events')->with($events);
@@ -87,8 +87,8 @@ public function it_flushes_events() {
8787
]
8888
]);
8989
$events = [
90-
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM), 'message' => 'test 1'],
91-
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_ATOM), 'message' => 'test 2'],
90+
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), 'message' => 'test 1'],
91+
['event_type' => 'log', 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), 'message' => 'test 2'],
9292
];
9393
$hbMock = $this->createMock(HoneybadgerClient::class);
9494
$hbMock->expects($this->once())->method('events')->with($events);

tests/HoneybadgerClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function throws_generic_exception_for_events()
5858
$events = [
5959
[
6060
'event_type' => 'log',
61-
'ts' => (new DateTime())->format(DATE_ATOM),
61+
'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED),
6262
'message' => 'Test message'
6363
]
6464
];

tests/HoneybadgerTest.php

+54-36
Original file line numberDiff line numberDiff line change
@@ -918,50 +918,68 @@ public function wont_send_event_if_disabled() {
918918

919919
/** @test */
920920
public function it_adds_event_type_and_ts_to_event_payload() {
921-
$eventsDispatcher = $this->createMock(BulkEventDispatcher::class);
922-
$eventsDispatcher
923-
->expects($this->once())
924-
->method('addEvent')
925-
->with([
926-
'event_type' => 'log',
927-
'ts' => (new DateTime())->format(DATE_ATOM),
928-
'message' => 'Test message',
929-
]);
930-
931-
$client = HoneybadgerClient::new([
932-
new Response(201),
933-
]);
934-
$badger = Honeybadger::new([
935-
'api_key' => 'asdf',
921+
$client = $this->createMock(\Honeybadger\HoneybadgerClient::class);
922+
$config = new Config([
923+
'api_key' => '1234',
936924
'events' => [
937-
'enabled' => true,
938-
],
939-
], $client->make(), $eventsDispatcher);
940-
925+
'enabled' => true
926+
]
927+
]);
928+
$eventsDispatcher = new class($config, $client) extends BulkEventDispatcher {
929+
public $events = [];
930+
931+
public function __construct(Config $config, \Honeybadger\HoneybadgerClient $client)
932+
{
933+
parent::__construct($config, $client);
934+
}
935+
936+
public function addEvent($event): void
937+
{
938+
$this->events[] = $event;
939+
}
940+
};
941+
$badger = new Honeybadger($config->all(), null, $eventsDispatcher);
941942
$badger->event('log', ['message' => 'Test message']);
943+
944+
$this->assertCount(1, $eventsDispatcher->events);
945+
946+
$event = $eventsDispatcher->events[0];
947+
$this->assertArrayHasKey('ts', $event);
948+
$this->assertEquals('log', $event['event_type']);
949+
$this->assertEquals('Test message', $event['message']);
942950
}
943951

944952
/** @test */
945953
public function it_adds_ts_to_event_payload() {
946-
$eventsDispatcher = $this->createMock(BulkEventDispatcher::class);
947-
$eventsDispatcher
948-
->expects($this->once())
949-
->method('addEvent')
950-
->with([
951-
'ts' => (new DateTime())->format(DATE_ATOM),
952-
'message' => 'Test message',
953-
]);
954-
955-
$client = HoneybadgerClient::new([
956-
new Response(201),
957-
]);
958-
$badger = Honeybadger::new([
959-
'api_key' => 'asdf',
954+
$client = $this->createMock(\Honeybadger\HoneybadgerClient::class);
955+
$config = new Config([
956+
'api_key' => '1234',
960957
'events' => [
961-
'enabled' => true,
962-
],
963-
], $client->make(), $eventsDispatcher);
958+
'enabled' => true
959+
]
960+
]);
961+
$eventsDispatcher = new class($config, $client) extends BulkEventDispatcher {
962+
public $events = [];
963+
964+
public function __construct(Config $config, \Honeybadger\HoneybadgerClient $client)
965+
{
966+
parent::__construct($config, $client);
967+
}
968+
969+
public function addEvent($event): void
970+
{
971+
$this->events[] = $event;
972+
}
973+
};
974+
$badger = new Honeybadger($config->all(), null, $eventsDispatcher);
975+
964976
$badger->event(['message' => 'Test message']);
977+
$this->assertCount(1, $eventsDispatcher->events);
978+
979+
$event = $eventsDispatcher->events[0];
980+
$this->assertArrayHasKey('ts', $event);
981+
$this->assertArrayNotHasKey('event_type', $event);
982+
$this->assertEquals('Test message', $event['message']);
965983
}
966984

967985
/** @test */

tests/LogEventHandlerTest.php

+18-16
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ public function addEvent($event): void
5454

5555
$logger->info('Test log message', ['some' => 'data']);
5656

57-
$this->assertEquals([[
58-
'event_type' => 'log',
59-
'ts' => (new \DateTime())->format(DATE_ATOM),
60-
'channel' => 'test-logger',
61-
'message' => 'Test log message',
62-
'severity' => 'info',
63-
'some' => 'data',
64-
]], $eventsDispatcher->events);
57+
$this->assertCount(1, $eventsDispatcher->events);
58+
59+
$event = $eventsDispatcher->events[0];
60+
$this->assertArrayHasKey('ts', $event);
61+
$this->assertEquals('log', $event['event_type']);
62+
$this->assertEquals('test-logger', $event['channel']);
63+
$this->assertEquals('Test log message', $event['message']);
64+
$this->assertEquals('info', $event['severity']);
65+
$this->assertEquals('data', $event['some']);
6566
}
6667

6768
/** @test */
@@ -94,13 +95,14 @@ public function addEvent($event): void
9495
$logger->debug('Test debug message', ['some' => 'data']);
9596
$logger->warning('Test warning message', ['some' => 'data']);
9697

97-
$this->assertEquals([[
98-
'event_type' => 'log',
99-
'ts' => (new \DateTime())->format(DATE_ATOM),
100-
'channel' => 'test-logger',
101-
'message' => 'Test warning message',
102-
'severity' => 'warning',
103-
'some' => 'data',
104-
]], $eventsDispatcher->events);
98+
$this->assertCount(1, $eventsDispatcher->events);
99+
100+
$event = $eventsDispatcher->events[0];
101+
$this->assertArrayHasKey('ts', $event);
102+
$this->assertEquals('log', $event['event_type']);
103+
$this->assertEquals('test-logger', $event['channel']);
104+
$this->assertEquals('Test warning message', $event['message']);
105+
$this->assertEquals('warning', $event['severity']);
106+
$this->assertEquals('data', $event['some']);
105107
}
106108
}

0 commit comments

Comments
 (0)