Search in sources :

Example 1 with AttributeSender

use of com.newrelic.agent.attributes.AttributeSender 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 2 with AttributeSender

use of com.newrelic.agent.attributes.AttributeSender in project newrelic-java-agent by newrelic.

the class LogSenderServiceImpl method createValidatedEvent.

/**
 * Create a validated LogEvent
 * @param attributes Map of attributes to create a LogEvent from
 * @return LogEvent instance
 */
private static LogEvent createValidatedEvent(Map<String, ?> attributes) {
    Map<String, String> logEventLinkingMetadata = AgentLinkingMetadata.getLogEventLinkingMetadata(TraceMetadataImpl.INSTANCE, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
    // Initialize new logEventAttributes map with agent linking metadata
    Map<String, Object> logEventAttributes = new HashMap<>(logEventLinkingMetadata);
    LogEvent event = new LogEvent(logEventAttributes, 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 LogEventAttributeSender(logEventAttributes);
    for (Map.Entry<String, ?> entry : attributes.entrySet()) {
        String key = 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, "Log 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) LogEvent(com.newrelic.agent.model.LogEvent) AttributeSender(com.newrelic.agent.attributes.AttributeSender) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

AttributeSender (com.newrelic.agent.attributes.AttributeSender)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 CustomInsightsEvent (com.newrelic.agent.model.CustomInsightsEvent)1 LogEvent (com.newrelic.agent.model.LogEvent)1