An EventBatchSender is created by configuring an EventBatchSenderFactory object with a License + * Key. Then, an event is created with an EventType, Attributes, and the current time in + * milliseconds (UTC time). The event is added to an EventBuffer and sent via sender.sendBatch(). + * + *
To run this example, provide a command line argument for your License Key. + */ +public class EventExample { + public static void main(String[] args) throws Exception { -public class TelemetryClientExample { + String licenseKey = args[0]; + EventBatchSenderFactory factory = + EventBatchSenderFactory.fromHttpImplementation(OkHttpPoster::new); - public static void main(String[] args) throws Exception { - String insightsInsertKey = args[0]; + EventBatchSender sender = + EventBatchSender.create(factory.configureWith(NEW_RELIC_INSERT_KEY).useLicenseKey(true).build()); - // create a TelemetryClient with an http connect timeout of 10 seconds. - TelemetryClient telemetryClient = - TelemetryClient.create( - () -> new OkHttpPoster(Duration.of(10, ChronoUnit.SECONDS)), insightsInsertKey); - Attributes commonAttributes = - new Attributes() - .put("exampleName", "TelemetryClientExample") - .put("service.name", "TelemetryClientExampleService") - .put("host.hostname", InetAddress.getLocalHost().getHostName()) - .put("environment", "staging"); + EventBuffer eventBuffer = new EventBuffer(getCommonAttributes()); + Attributes attr = new Attributes(); + attr.put("foo", 1234); + attr.put("bar", "baz"); + attr.put("quux", true); - Span span = sendSampleEvent(telemetryClient, commonAttributes); + long timestamp = System.currentTimeMillis(); + Event event = new Event("SampleEvent", attr, timestamp); + eventBuffer.addEvent(event); - // make sure to shutdown the client, else the background Executor will stop the program from - // exiting. - telemetryClient.shutdown(); + sender.sendBatch(eventBuffer.createBatch()); } - private static void sendSampleEvent( - TelemetryClient telemetryClient, Attributes commonAttributes) { - Event event = new Event("TestEvent", new Attributes().put("testKey", "testValue")); - telemetryClient.sendBatch(new EventBatch(singleton(event), commonAttributes)); + private static Attributes getCommonAttributes() { + return new Attributes().put("exampleName", "CountExample"); } - +} ``` ### Alternative Options Other options available include using prometheus data, and installing a flex agent. We won't cover those in this guide, but here are some links to review these options. * [Prometheus Data](https://docs.newrelic.com/docs/infrastructure/prometheus-integrations/get-started/send-prometheus-metric-data-new-relic) - Prometheus data can be used by New Relic in two ways, [remote write](https://docs.newrelic.com/docs/infrastructure/prometheus-integrations/install-configure-remote-write/set-your-prometheus-remote-write-integration) and [OpenMetrics](https://docs.newrelic.com/docs/infrastructure/prometheus-integrations/install-configure-openmetrics/install-update-or-uninstall-your-prometheus-openmetrics-integration). At a very high level, remote write should be used if you manage your own prometheus servers, and OpenMetrics should be used if you do not. -* [Flex Agent](https://github.com/newrelic/nri-flex/blob/master/docs/basic-tutorial.md#flex-step-by-step-tutorial) - Our serverless flex agent is a possibility if the other options do not work for you, but might be a more complex integration to get started. \ No newline at end of file +* **Manual Implementation** - If you are using a code library that we do not have instrumented with an SDK, you can always manually instrument your own library to make a POST request to the [New Relic Event API](https://docs.newrelic.com/docs/data-apis/ingest-apis/introduction-event-api/). +* [Flex Agent](https://github.com/newrelic/nri-flex/blob/master/docs/basic-tutorial.md#flex-step-by-step-tutorial) - Our serverless flex agent is a possibility, but might be a more complex integration to get started.