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;
}
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);
}
}
}
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);
}
}
}
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;
}
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);
}
}
Aggregations