public void handleRequest()

in aws-xray-agent/src/main/java/com/amazonaws/xray/agent/runtime/handlers/downstream/SqlHandler.java [37:69]


    public void handleRequest(Event event) {
        // If a parent SQL transaction is already in progress, we return to avoid an infinite loop. This is because
        // in order to populate a SQL subsegment, we make several calls to the JDBC Driver's DatabaseMetaData object.
        // For example, if a driver's implementation of DatabaseMetaData.getUserName() uses executeQuery("SELECT USER")
        // to get the DB user, executeQuery would be intercepted by the Disco JDBC plugin, trigger this handler to
        // create subegment, and we'd call getUserName to populate that subsegment and so on.
        if (incrementSqlTransactionCount() > 1) {
            return;
        }

        ServiceDownstreamRequestEvent requestEvent = (ServiceDownstreamRequestEvent) event;
        Statement statement = (Statement) requestEvent.getRequest();
        String queryString = requestEvent.getOperation();
        boolean recordSql = XRaySDKConfiguration.getInstance().shouldCollectSqlQueries();
        final Connection connection;

        try {
            connection = statement.getConnection();
        } catch (SQLException e) {
            log.debug("Encountered exception when creating subsegment for query of "
                    + requestEvent.getService() + ", starting blank subsegment", e);
            AWSXRay.beginSubsegment(SqlSubsegments.DEFAULT_DATABASE_NAME);
            return;
        }

        // If the query string wasn't provided by current DiSCo event, check the preparedMap cache
        if (queryString == null && statement instanceof PreparedStatement) {
            queryString = XRayTransactionState.getPreparedQuery((PreparedStatement) statement);
        }

        // If user opted-in to record their Queries, include them in the subsegment
        SqlSubsegments.forQuery(connection, recordSql ? queryString : null);
    }