Search in sources :

Example 1 with RequestMetric

use of io.apiman.gateway.engine.metrics.RequestMetric in project apiman-plugins by apiman.

the class KeycloakOauthPolicy method doTokenAuth.

private Holder<Boolean> doTokenAuth(Holder<Boolean> successStatus, ApiRequest request, IPolicyContext context, KeycloakOauthConfigBean config, IPolicyChain<ApiRequest> chain, String rawToken) {
    try {
        TokenVerifier<AccessToken> verifier = TokenVerifier.create(rawToken, AccessToken.class).withChecks(TokenVerifier.IS_ACTIVE);
        AccessToken parsedToken;
        if (config.getRealmCertificate() != null) {
            parsedToken = verifier.publicKey(config.getRealmCertificate().getPublicKey()).verify().getToken();
        } else {
            String kid = verifier.getHeader().getKeyId();
            parsedToken = verifier.publicKey(keycloakDeployment.getPublicKeyLocator().getPublicKey(kid, keycloakDeployment)).verify().getToken();
        }
        delegateKerberosTicket(request, config, parsedToken);
        forwardHeaders(request, config, rawToken, parsedToken);
        stripAuthTokens(request, config);
        forwardAuthRoles(context, config, parsedToken);
        RequestMetric metric = context.getAttribute(PolicyContextKeys.REQUEST_METRIC, (RequestMetric) null);
        if (metric != null) {
            metric.setUser(parsedToken.getPreferredUsername());
        }
        return successStatus.setValue(true);
    } catch (VerificationException e) {
        System.out.println(e);
        chain.doFailure(failureFactory.verificationException(context, e));
        return successStatus.setValue(false);
    }
}
Also used : AccessToken(org.keycloak.representations.AccessToken) RequestMetric(io.apiman.gateway.engine.metrics.RequestMetric) VerificationException(org.keycloak.common.VerificationException)

Example 2 with RequestMetric

use of io.apiman.gateway.engine.metrics.RequestMetric in project apiman by apiman.

the class EsMetrics method processQueue.

/**
 * Process the next item in the queue.
 */
protected void processQueue() {
    try {
        RestHighLevelClient client = getClient();
        Collection<RequestMetric> batch = new ArrayList<>(this.batchSize);
        RequestMetric rm = queue.take();
        batch.add(rm);
        queue.drainTo(batch, this.batchSize - 1);
        BulkRequest request = new BulkRequest();
        for (RequestMetric metric : batch) {
            IndexRequest index = new IndexRequest(getIndexPrefix());
            index.source(JSON_MAPPER.writeValueAsString(metric), XContentType.JSON);
            request.add(index);
        }
        ActionListener<BulkResponse> listener = new ActionListener<>() {

            @Override
            public void onResponse(BulkResponse bulkItemResponses) {
                if (bulkItemResponses.hasFailures()) {
                    LOGGER.warn("Errors were reported when submitting bulk metrics into Elasticsearch. " + "This may have resulted in a loss of data: ", bulkItemResponses.buildFailureMessage());
                }
            }

            @Override
            public void onFailure(Exception e) {
                // $NON-NLS-1$
                LOGGER.error("Failed to add metric(s) to ES", e);
            }
        };
        client.bulkAsync(request, RequestOptions.DEFAULT, listener);
    } catch (InterruptedException | JsonProcessingException e) {
        // $NON-NLS-1$
        LOGGER.error("Failed to add metric(s) to ES", e);
    }
}
Also used : ArrayList(java.util.ArrayList) RequestMetric(io.apiman.gateway.engine.metrics.RequestMetric) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IndexRequest(org.elasticsearch.action.index.IndexRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ActionListener(org.elasticsearch.action.ActionListener) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with RequestMetric

use of io.apiman.gateway.engine.metrics.RequestMetric in project apiman by apiman.

the class JdbcMetrics method processQueue.

/**
 * Process the next item in the queue.
 */
@SuppressWarnings("nls")
protected void processQueue() {
    try {
        RequestMetric metric = queue.take();
        QueryRunner run = new QueryRunner(ds);
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
        cal.setTime(metric.getRequestStart());
        long rstart = cal.getTimeInMillis();
        long rend = metric.getRequestEnd().getTime();
        long duration = metric.getRequestDuration();
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        long minute = cal.getTimeInMillis();
        cal.set(Calendar.MINUTE, 0);
        long hour = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        long day = cal.getTimeInMillis();
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        long week = cal.getTimeInMillis();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        long month = cal.getTimeInMillis();
        String api_org_id = metric.getApiOrgId();
        String api_id = metric.getApiId();
        String api_version = metric.getApiVersion();
        String client_org_id = metric.getClientOrgId();
        String client_id = metric.getClientId();
        String client_version = metric.getClientVersion();
        String plan = metric.getPlanId();
        String user_id = metric.getUser();
        String rtype = null;
        if (metric.isFailure()) {
            rtype = "failure";
        } else if (metric.isError()) {
            rtype = "error";
        }
        long bytes_up = metric.getBytesUploaded();
        long bytes_down = metric.getBytesDownloaded();
        // Now insert a row for the metric.
        run.update("INSERT INTO gw_requests (" + "rstart, rend, duration, month, week, day, hour, minute, " + "api_org_id, api_id, api_version, " + "client_org_id, client_id, client_version, plan, " + "user_id, resp_type, bytes_up, bytes_down) VALUES (" + "?, ?, ?, ?, ?, ?, ?, ?," + "?, ?, ?," + "?, ?, ?, ?," + "?, ?, ?, ?)", rstart, rend, duration, month, week, day, hour, minute, api_org_id, api_id, api_version, client_org_id, client_id, client_version, plan, user_id, rtype, bytes_up, bytes_down);
    } catch (InterruptedException ie) {
        // This means that the thread was stopped.
        LOGGER.trace("Metrics was stopped {0}", ie.getMessage());
    } catch (Exception e) {
        LOGGER.error(e, "An unexpected error occurred when attempting to add a metric " + "to the database. {0}", e.getMessage());
    }
}
Also used : Calendar(java.util.Calendar) RequestMetric(io.apiman.gateway.engine.metrics.RequestMetric) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 4 with RequestMetric

use of io.apiman.gateway.engine.metrics.RequestMetric in project apiman by apiman.

the class JdbcMetricsTest method request.

/**
 * @throws ParseException
 */
private RequestMetric request(String requestStart, long requestDuration, String url, String resource, String method, String apiOrgId, String apiId, String apiVersion, String planId, String clientOrgId, String clientId, String clientVersion, String contractId, String user, int responseCode, String responseMessage, boolean failure, int failureCode, String failureReason, boolean error, String errorMessage, long bytesUploaded, long bytesDownloaded) throws ParseException {
    Date start = ISO8601Utils.parse(requestStart, new ParsePosition(0));
    RequestMetric rval = new RequestMetric();
    rval.setRequestStart(start);
    rval.setRequestEnd(new Date(start.getTime() + requestDuration));
    rval.setApiStart(start);
    rval.setApiEnd(rval.getRequestEnd());
    rval.setApiDuration(requestDuration);
    rval.setUrl(url);
    rval.setResource(resource);
    rval.setMethod(method);
    rval.setApiOrgId(apiOrgId);
    rval.setApiId(apiId);
    rval.setApiVersion(apiVersion);
    rval.setPlanId(planId);
    rval.setClientOrgId(clientOrgId);
    rval.setClientId(clientId);
    rval.setClientVersion(clientVersion);
    rval.setContractId(contractId);
    rval.setUser(user);
    rval.setResponseCode(responseCode);
    rval.setResponseMessage(responseMessage);
    rval.setFailure(failure);
    rval.setFailureCode(failureCode);
    rval.setFailureReason(failureReason);
    rval.setError(error);
    rval.setErrorMessage(errorMessage);
    rval.setBytesUploaded(bytesUploaded);
    rval.setBytesDownloaded(bytesDownloaded);
    return rval;
}
Also used : RequestMetric(io.apiman.gateway.engine.metrics.RequestMetric) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 5 with RequestMetric

use of io.apiman.gateway.engine.metrics.RequestMetric in project apiman by apiman.

the class BasicMetricsTest method errorMetrics.

@Test
public void errorMetrics() throws IOException {
    @SuppressWarnings("serial") final Set<String> expected = new LinkedHashSet<String>() {

        {
            add("apiman_request_duration_milliseconds_count{method=\"GET\",responseCode=\"404\",api=\"apiId\",apiVersion=\"apiVersion\",client=\"clientId\",} 1.0");
            add("apiman_request_duration_milliseconds_sum{method=\"GET\",responseCode=\"404\",api=\"apiId\",apiVersion=\"apiVersion\",client=\"clientId\",} 644.0");
            add("apiman_requests_total{method=\"GET\",responseCode=\"404\",api=\"apiId\",apiVersion=\"apiVersion\",client=\"clientId\",} 1.0");
            add("apiman_errors_total{method=\"GET\",responseCode=\"404\",api=\"apiId\",apiVersion=\"apiVersion\",client=\"clientId\",} 1.0");
        }
    };
    final RequestMetric requestMetric = buildRequestMetric("clientId", true, 404, "could not find hamsters");
    prometheusMetrics.record(requestMetric);
    final Request request = new Request.Builder().url("http://localhost:9876/").get().build();
    final Response response = client.newCall(request).execute();
    final String rString = response.body().string();
    assertTrue(equals(expected, rString));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) RequestMetric(io.apiman.gateway.engine.metrics.RequestMetric) Test(org.junit.Test)

Aggregations

RequestMetric (io.apiman.gateway.engine.metrics.RequestMetric)9 Request (com.squareup.okhttp.Request)3 Response (com.squareup.okhttp.Response)3 LinkedHashSet (java.util.LinkedHashSet)3 Test (org.junit.Test)3 Date (java.util.Date)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ParsePosition (java.text.ParsePosition)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 QueryRunner (org.apache.commons.dbutils.QueryRunner)1 ActionListener (org.elasticsearch.action.ActionListener)1 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)1 AfterClass (org.junit.AfterClass)1 VerificationException (org.keycloak.common.VerificationException)1 AccessToken (org.keycloak.representations.AccessToken)1