Search in sources :

Example 1 with DatastoreConfig

use of com.newrelic.agent.config.DatastoreConfig in project newrelic-java-agent by newrelic.

the class DefaultTracer method recordExternalMetricsDatastore.

private void recordExternalMetricsDatastore(DatastoreParameters datastoreParameters) {
    Transaction tx = getTransactionActivity().getTransaction();
    if (tx != null && datastoreParameters != null) {
        DatastoreMetrics.collectDatastoreMetrics(datastoreParameters.getProduct(), tx, this, datastoreParameters.getCollection(), datastoreParameters.getOperation(), datastoreParameters.getHost(), datastoreParameters.getPort(), datastoreParameters.getPathOrId(), datastoreParameters.getDatabaseName());
        DatastoreConfig datastoreConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getDatastoreConfig();
        boolean allUnknown = datastoreParameters.getHost() == null && datastoreParameters.getPort() == null && datastoreParameters.getPathOrId() == null;
        if (datastoreConfig.isInstanceReportingEnabled() && !allUnknown) {
            setAgentAttribute(DatastoreMetrics.DATASTORE_HOST, DatastoreMetrics.replaceLocalhost(datastoreParameters.getHost()));
            setAgentAttribute(DatastoreMetrics.DATASTORE_PORT_PATH_OR_ID, DatastoreMetrics.getIdentifierOrPort(datastoreParameters.getPort(), datastoreParameters.getPathOrId()));
        }
        // Spec says this is a should, only send database name when we actually have one.
        if (datastoreConfig.isDatabaseNameReportingEnabled() && datastoreParameters.getDatabaseName() != null) {
            setAgentAttribute(DatastoreMetrics.DB_INSTANCE, datastoreParameters.getDatabaseName());
        }
    } else {
        Agent.LOG.log(Level.FINE, "Datastore metrics will not be applied because the tracer is not in a transaction.");
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) DatastoreConfig(com.newrelic.agent.config.DatastoreConfig)

Example 2 with DatastoreConfig

use of com.newrelic.agent.config.DatastoreConfig in project newrelic-java-agent by newrelic.

the class DefaultSlowQueryListener method noticeTracer.

@Override
public <T> void noticeTracer(Tracer tracer, SlowQueryDatastoreParameters<T> slowQueryDatastoreParameters) {
    if (tracer.getDurationInMilliseconds() > thresholdInMillis) {
        T rawQuery = slowQueryDatastoreParameters.getRawQuery();
        QueryConverter<T> queryConverter = slowQueryDatastoreParameters.getQueryConverter();
        if (rawQuery == null || queryConverter == null) {
            // Ignore tracer
            return;
        }
        String rawQueryString = queryConverter.toRawQueryString(rawQuery);
        if (rawQueryString == null || rawQueryString.trim().isEmpty()) {
            // Ignore tracer
            return;
        }
        String obfuscatedQueryString = queryConverter.toObfuscatedQueryString(rawQuery);
        if (obfuscatedQueryString == null) {
            // Ignore tracer if no obfuscated query is provided
            return;
        }
        // Handle an "input query" from an ORM or a framework that automatically generates queries
        if (slowQueryDatastoreParameters instanceof SlowQueryWithInputDatastoreParameters) {
            handleInputQuery(tracer, (SlowQueryWithInputDatastoreParameters) slowQueryDatastoreParameters);
        }
        // This allows transaction traces to show slow queries directly in the trace details
        tracer.setAgentAttribute(SqlTracer.SQL_PARAMETER_NAME, rawQueryString);
        tracer.setAgentAttribute(SqlTracer.SQL_OBFUSCATED_PARAMETER_NAME, obfuscatedQueryString);
        DatastoreConfig datastoreConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getDatastoreConfig();
        boolean allUnknown = slowQueryDatastoreParameters.getHost() == null && slowQueryDatastoreParameters.getPort() == null && slowQueryDatastoreParameters.getPathOrId() == null;
        if (datastoreConfig.isInstanceReportingEnabled() && !allUnknown) {
            tracer.setAgentAttribute(DatastoreMetrics.DATASTORE_HOST, DatastoreMetrics.replaceLocalhost(slowQueryDatastoreParameters.getHost()));
            tracer.setAgentAttribute(DatastoreMetrics.DATASTORE_PORT_PATH_OR_ID, DatastoreMetrics.getIdentifierOrPort(slowQueryDatastoreParameters.getPort(), slowQueryDatastoreParameters.getPathOrId()));
        }
        if (datastoreConfig.isDatabaseNameReportingEnabled() && slowQueryDatastoreParameters.getDatabaseName() != null) {
            tracer.setAgentAttribute(DatastoreMetrics.DB_INSTANCE, slowQueryDatastoreParameters.getDatabaseName());
        }
        if (slowQueryInfoCache == null) {
            slowQueryInfoCache = new BoundedConcurrentCache<>(MAX_SQL_TRACERS);
        }
        SlowQueryInfo existingInfo = slowQueryInfoCache.get(obfuscatedQueryString);
        if (existingInfo != null) {
            // Aggregate tracers by SQL.
            existingInfo.aggregate(tracer);
            slowQueryInfoCache.putReplace(obfuscatedQueryString, existingInfo);
        } else {
            SlowQueryInfo sqlInfo = new SlowQueryInfo(null, tracer, rawQueryString, obfuscatedQueryString, tracer.getTransactionActivity().getTransaction().getAgentConfig().getSqlTraceConfig());
            sqlInfo.aggregate(tracer);
            slowQueryInfoCache.putIfAbsent(obfuscatedQueryString, sqlInfo);
        }
    }
}
Also used : DatastoreConfig(com.newrelic.agent.config.DatastoreConfig) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters)

Example 3 with DatastoreConfig

use of com.newrelic.agent.config.DatastoreConfig in project newrelic-java-agent by newrelic.

the class SlowQueryInfo method createParameters.

@SuppressWarnings("unchecked")
private Map<String, Object> createParameters(Tracer tracer) {
    Map<String, Object> parameters = new HashMap<>();
    // Check for an explain plan (right now this only potentially exists for a SqlTracer)
    Object explainPlan = tracer.getAgentAttribute(SqlTracer.EXPLAIN_PLAN_PARAMETER_NAME);
    if (explainPlan != null) {
        parameters.put(SlowQueryAggregatorImpl.EXPLAIN_PLAN_KEY, explainPlan);
    }
    // A backtrace could exist for any type of tracer
    List<StackTraceElement> backtrace = (List<StackTraceElement>) tracer.getAgentAttribute(DefaultTracer.BACKTRACE_PARAMETER_NAME);
    if (backtrace != null) {
        backtrace = StackTraces.scrubAndTruncate(backtrace);
        List<String> backtraceStrings = StackTraces.toStringList(backtrace);
        parameters.put(SlowQueryAggregatorImpl.BACKTRACE_KEY, backtraceStrings);
    }
    DatastoreConfig datastoreConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getDatastoreConfig();
    String host = (String) tracer.getAgentAttribute(DatastoreMetrics.DATASTORE_HOST);
    String port_path_or_id = (String) tracer.getAgentAttribute(DatastoreMetrics.DATASTORE_PORT_PATH_OR_ID);
    boolean allUnknown = host == null && port_path_or_id == null;
    if (datastoreConfig.isInstanceReportingEnabled() && !allUnknown) {
        parameters.put(DatastoreMetrics.DATASTORE_HOST, host);
        parameters.put(DatastoreMetrics.DATASTORE_PORT_PATH_OR_ID, port_path_or_id);
    }
    String databaseName = (String) tracer.getAgentAttribute(DatastoreMetrics.DB_INSTANCE);
    if (datastoreConfig.isDatabaseNameReportingEnabled() && databaseName != null) {
        parameters.put(DatastoreMetrics.DB_INSTANCE, databaseName);
    }
    // An input query could exist for any type of tracer and records ORM-like query strings
    Map<String, String> inputQuery = (Map<String, String>) tracer.getAgentAttribute(DatastoreMetrics.INPUT_QUERY_ATTRIBUTE);
    if (inputQuery != null) {
        parameters.put(DatastoreMetrics.INPUT_QUERY_ATTRIBUTE, inputQuery);
    }
    Transaction txn = tracer.getTransactionActivity().getTransaction();
    DistributedTracePayloadImpl inboundPayload = txn.getSpanProxy().getInboundDistributedTracePayload();
    DistributedTraceService distributedTraceService = ServiceFactory.getDistributedTraceService();
    DistributedTracingConfig distributedTracingConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getDistributedTracingConfig();
    if (distributedTracingConfig.isEnabled()) {
        String traceId = txn.getOrCreateTraceId();
        String parentId = inboundPayload == null ? null : inboundPayload.txnId;
        String parentSpanId = inboundPayload == null ? null : inboundPayload.guid;
        Map<String, Object> intrinsics = distributedTraceService.getIntrinsics(inboundPayload, txn.getGuid(), traceId, txn.getTransportType(), txn.getTransportDurationInMillis(), txn.getLargestTransportDurationInMillis(), parentId, parentSpanId, txn.getPriority());
        parameters.putAll(intrinsics);
    }
    parameters.put("priority", txn.getPriority());
    return parameters;
}
Also used : DistributedTraceService(com.newrelic.agent.tracing.DistributedTraceService) DistributedTracingConfig(com.newrelic.agent.config.DistributedTracingConfig) HashMap(java.util.HashMap) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) Transaction(com.newrelic.agent.Transaction) DatastoreConfig(com.newrelic.agent.config.DatastoreConfig) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DatastoreConfig (com.newrelic.agent.config.DatastoreConfig)3 Transaction (com.newrelic.agent.Transaction)2 DistributedTracingConfig (com.newrelic.agent.config.DistributedTracingConfig)1 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)1 DistributedTraceService (com.newrelic.agent.tracing.DistributedTraceService)1 SlowQueryWithInputDatastoreParameters (com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1