Search in sources :

Example 1 with CustomInsightsEvent

use of com.newrelic.agent.model.CustomInsightsEvent in project newrelic-java-agent by newrelic.

the class IntrospectorInsightsService method getEvents.

public Collection<Event> getEvents(String type) {
    List<CustomInsightsEvent> currentEvents = events.get(type);
    if (currentEvents == null) {
        return null;
    }
    List<Event> output = new ArrayList<>(currentEvents.size());
    for (CustomInsightsEvent current : currentEvents) {
        output.add(new EventImpl(current.getType(), current.getUserAttributesCopy()));
    }
    return output;
}
Also used : ArrayList(java.util.ArrayList) CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent) AnalyticsEvent(com.newrelic.agent.model.AnalyticsEvent) Event(com.newrelic.agent.introspec.Event) CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent)

Example 2 with CustomInsightsEvent

use of com.newrelic.agent.model.CustomInsightsEvent in project newrelic-java-agent by newrelic.

the class InsightsServiceImpl method harvestEvents.

public void harvestEvents(final String appName) {
    if (!getIsEnabledForApp(ServiceFactory.getConfigService().getAgentConfig(appName), appName)) {
        reservoirForApp.remove(appName);
        return;
    }
    if (maxSamplesStored <= 0) {
        clearReservoir(appName);
        return;
    }
    long startTimeInNanos = System.nanoTime();
    final DistributedSamplingPriorityQueue<CustomInsightsEvent> reservoir = this.reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<CustomInsightsEvent>(appName, "Insights Service", maxSamplesStored));
    if (reservoir != null && reservoir.size() > 0) {
        try {
            ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendCustomAnalyticsEvents(maxSamplesStored, reservoir.getNumberOfTries(), Collections.unmodifiableList(reservoir.asList()));
            final long durationInNanos = System.nanoTime() - startTimeInNanos;
            ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

                @Override
                public void doWork(StatsEngine statsEngine) {
                    recordSupportabilityMetrics(statsEngine, durationInNanos, reservoir);
                }

                @Override
                public String getAppName() {
                    return appName;
                }
            }, reservoir.getServiceName());
            if (reservoir.size() < reservoir.getNumberOfTries()) {
                int dropped = reservoir.getNumberOfTries() - reservoir.size();
                Agent.LOG.log(Level.FINE, "Dropped {0} custom events out of {1}.", dropped, reservoir.getNumberOfTries());
            }
        } catch (HttpError e) {
            if (!e.discardHarvestData()) {
                Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be included in the next harvest.", e);
                // Save unsent data by merging it with current data using reservoir algorithm
                DistributedSamplingPriorityQueue<CustomInsightsEvent> currentReservoir = reservoirForApp.get(appName);
                currentReservoir.retryAll(reservoir);
            } else {
                // discard harvest data
                reservoir.clear();
                Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be dropped.", e);
            }
        } catch (Exception e) {
            // discard harvest data
            reservoir.clear();
            Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be dropped.", e);
        }
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) HttpError(com.newrelic.agent.transport.HttpError) StatsEngine(com.newrelic.agent.stats.StatsEngine) CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent)

Example 3 with CustomInsightsEvent

use of com.newrelic.agent.model.CustomInsightsEvent in project newrelic-java-agent by newrelic.

the class InsightsServiceImpl method storeEvents.

private void storeEvents(String appName, float priority, Collection<CustomInsightsEvent> events) {
    if (events.size() > 0) {
        DistributedSamplingPriorityQueue<CustomInsightsEvent> eventList = getReservoir(appName);
        for (CustomInsightsEvent event : events) {
            // Set "priority" on CustomEvents based on priority value from Transaction
            event.setPriority(priority);
            eventList.add(event);
        }
    }
}
Also used : CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent)

Example 4 with CustomInsightsEvent

use of com.newrelic.agent.model.CustomInsightsEvent in project newrelic-java-agent by newrelic.

the class InsightsServiceImpl method createValidatedEvent.

private static CustomInsightsEvent createValidatedEvent(String eventType, Map<String, ?> attributes) {
    Map<String, Object> userAttributes = new HashMap<>(attributes.size());
    CustomInsightsEvent event = new CustomInsightsEvent(mapInternString(eventType), System.currentTimeMillis(), userAttributes, DistributedTraceServiceImpl.nextTruncatedFloat());
    // Now add the attributes from the argument map to the event using an AttributeSender.
    // An AttributeSender is the way to reuse all the existing attribute validations. We
    // also locally "intern" Strings because we anticipate a lot of reuse of the keys and,
    // possibly, the values. But there's an interaction: if the key or value is chopped
    // within the attribute sender, the modified value won't be "interned" in our map.
    AttributeSender sender = new CustomEventAttributeSender(userAttributes);
    final String method = "add custom event attribute";
    for (Map.Entry entry : attributes.entrySet()) {
        String key = (String) entry.getKey();
        Object value = entry.getValue();
        // key or value is null, skip it with a log message and iterate to next entry in attributes.entrySet()
        if (key == null || value == null) {
            Agent.LOG.log(Level.WARNING, "Custom event with invalid attributes key or value of null was reported for a transaction but ignored." + " Each key should be a String and each value should be a String, Number, or Boolean.");
            continue;
        }
        mapInternString(key);
        if (value instanceof String) {
            sender.addAttribute(key, mapInternString((String) value), method);
        } else if (value instanceof Number) {
            sender.addAttribute(key, (Number) value, method);
        } else if (value instanceof Boolean) {
            sender.addAttribute(key, (Boolean) value, method);
        } else {
            // Java Agent specific - toString the value. This allows for e.g. enums as arguments.
            sender.addAttribute(key, mapInternString(value.toString()), method);
        }
    }
    return event;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AttributeSender(com.newrelic.agent.attributes.AttributeSender) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent)

Example 5 with CustomInsightsEvent

use of com.newrelic.agent.model.CustomInsightsEvent in project newrelic-java-agent by newrelic.

the class IntrospectorInsightsService method recordCustomEvent.

@Override
public void recordCustomEvent(String eventType, Map<String, ?> attributes) {
    if (AnalyticsEvent.isValidType(eventType)) {
        Map<String, Object> atts = Maps.newHashMap(attributes);
        CustomInsightsEvent event = new CustomInsightsEvent(eventType, System.currentTimeMillis(), atts, DistributedTraceServiceImpl.nextTruncatedFloat());
        storeEvent("TestApp", event);
    }
}
Also used : CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent)

Aggregations

CustomInsightsEvent (com.newrelic.agent.model.CustomInsightsEvent)8 MockRPMService (com.newrelic.agent.MockRPMService)2 StatsEngine (com.newrelic.agent.stats.StatsEngine)2 HashMap (java.util.HashMap)2 Gson (com.google.gson.Gson)1 HarvestService (com.newrelic.agent.HarvestService)1 MockServiceManager (com.newrelic.agent.MockServiceManager)1 RPMService (com.newrelic.agent.RPMService)1 RPMServiceManager (com.newrelic.agent.RPMServiceManager)1 Transaction (com.newrelic.agent.Transaction)1 TransactionService (com.newrelic.agent.TransactionService)1 AttributeSender (com.newrelic.agent.attributes.AttributeSender)1 ConfigService (com.newrelic.agent.config.ConfigService)1 Event (com.newrelic.agent.introspec.Event)1 AnalyticsEvent (com.newrelic.agent.model.AnalyticsEvent)1 ServiceManager (com.newrelic.agent.service.ServiceManager)1 InsightsServiceImpl (com.newrelic.agent.service.analytics.InsightsServiceImpl)1 TransactionInsights (com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights)1 StatsWork (com.newrelic.agent.stats.StatsWork)1 HttpError (com.newrelic.agent.transport.HttpError)1