1

GitHub - samlitowitz/php-new-relic-event-api-client: PHP Client for New Relics E...

 2 years ago
source link: https://github.com/samlitowitz/php-new-relic-event-api-client
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP New Relic Event API Client

Table of Contents

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\String_(2.1)
			),
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('intAttr'),
				new CustomEvent\Attribute\Value\String_(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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK