samlitowitz/php-new-relic-event-api-client

PHP Client for the New Relic Event API

v1.0.1 2022-11-14 01:19 UTC

This package is auto-updated.

Last update: 2024-04-14 04:19:22 UTC


README

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. References

Introduction

This library introduces an interface and an implementation to interact with the New Relic Event API.

Interface

The interface is provided to allow drop-in replacement and composition.

interface Client {
	public function send(PhpNewRelic\CustomEventCollection $customEvents): PhpNewRelic\EventAPI\Http\Response;
}

Implementation

The only validation done client size is to ensure the payload is below the maximum allowed size. Parsing errors will be handled by New Relic as described in the documentation.

The client defaults to using the Guzzle HTTP client with a timeout of 12 seconds, however the constructor accepts anything which implements the Psr\Http\Client\ClientInterface interface.

Installation

composer require samlitowitz/php-new-relic-event-api-client

Usage

<?php

use PhpNewRelic\CustomEvent;
use PhpNewRelic\CustomEventCollection;
use PhpNewRelic\EventAPI\Http\Client;
use PhpNewRelic\EventAPI\Http\DomainName;
use PhpNewRelic\EventAPI\Http\Exception\GZipEncodingFailedException;
use PhpNewRelic\EventAPI\Http\Exception\PayloadExceedsMaximumSizeException;
use PhpNewRelic\EventAPI\Http\Exception\SubmissionErrorException;

define('NEW_RELIC_ACCOUNT_ID', '<YOUR-NEW-RELIC-ACCOUNT-ID>');
define('NEW_RELIC_API_KEY', '<YOUR-NEW-RELIC-API-KEY>');

// Instantiate the client
$client = new Client(NEW_RELIC_ACCOUNT_ID, NEW_RELIC_API_KEY, DomainName::US);

// Create custom event(s)
$events = CustomEventCollection::fromArray([
	CustomEvent::fromArray(
		'yourCustomEventType',
		[
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('stringAttr'),
				new CustomEvent\Attribute\Value\String_('string')
			),
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('floatAttr'),
				new CustomEvent\Attribute\Value\Float_(2.1)
			),
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('intAttr'),
				new CustomEvent\Attribute\Value\Integer(2)
			),
		]
	),
	// ...
]);
try {
	$response = $client->send($events);
} catch (GZipEncodingFailedException $e) {
	// Handle a failure to encode here
} catch (PayloadExceedsMaximumSizeException $e) {
	// Handle a too large payload side here
} catch (SubmissionErrorException $e) {
	// Handle submission errors here
} catch (Throwable $t) {
	// Handle other throws here
}

References

  1. New Relic Event API Documentation