Skip to content

Commit

Permalink
fixes #1773 update the abstract metrics handler to send metics even i…
Browse files Browse the repository at this point in the history
…f auditInfo is null (#1774)
  • Loading branch information
stevehu authored May 24, 2023
1 parent 9948164 commit 80d4efe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void handleRequest(HttpServerExchange httpServerExchange) throws Exceptio
if(logger.isDebugEnabled()) logger.debug("RouterHandler.handleRequest starts.");
long startTime = System.nanoTime();
proxyHandler.handleRequest(httpServerExchange);
if(config.isMetricsInjection() && metricsHandler != null) metricsHandler.injectMetrics(httpServerExchange, startTime, config.getMetricsName());
if(config.isMetricsInjection() && metricsHandler != null) metricsHandler.injectMetrics(httpServerExchange, startTime, config.getMetricsName(), null);
if(logger.isDebugEnabled()) logger.debug("RouterHandler.handleRequest ends.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
if (config.getPathHostMappings() != null) {
for(String[] parts: config.getPathHostMappings()) {
if(requestPath.startsWith(parts[0])) {
String endpoint = parts[1] + "@" + exchange.getRequestMethod().toString();
// handle the url rewrite here. It has to be the right path that applied for external service to do the url rewrite.
if(config.getUrlRewriteRules() != null && config.getUrlRewriteRules().size() > 0) {
boolean matched = false;
Expand Down Expand Up @@ -164,7 +165,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
logger.error("wrong http method " + method + " for request path " + requestPath);
setExchangeStatus(exchange, METHOD_NOT_ALLOWED, method, requestPath);
if(logger.isDebugEnabled()) logger.debug("ExternalServiceHandler.handleRequest ends with an error.");
if(config.isMetricsInjection() && metricsHandler != null) metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName());
if(config.isMetricsInjection() && metricsHandler != null) metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName(), endpoint);
return;
}
if(client == null) {
Expand Down Expand Up @@ -207,7 +208,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
if(logger.isDebugEnabled()) logger.debug("ExternalServiceHandler.handleRequest ends.");
if(config.isMetricsInjection() && metricsHandler != null) {
if(logger.isTraceEnabled()) logger.trace("injecting metrics for " + config.getMetricsName());
metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName());
metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName(), endpoint);
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void handleRequest(HttpServerExchange httpServerExchange) throws Exceptio
httpServerExchange.getRequestHeaders().put(HttpString.tryFromString(CLAIMS_KEY), new ObjectMapper().writeValueAsString(jwtClaims.getClaimsMap()));
}
proxyHandler.handleRequest(httpServerExchange);
if(config.isMetricsInjection() && metricsHandler != null) metricsHandler.injectMetrics(httpServerExchange, startTime, config.getMetricsName());
if(config.isMetricsInjection() && metricsHandler != null) metricsHandler.injectMetrics(httpServerExchange, startTime, config.getMetricsName(), null);
if(logger.isDebugEnabled()) logger.debug("LightProxyHandler.handleRequest ends.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {

for(String key: config.getPathPrefixAuth().keySet()) {
if(requestPath.startsWith(key)) {
String endpoint = key + "@" + exchange.getRequestMethod().toString();
// handle the url rewrite here.
if(config.getUrlRewriteRules() != null && config.getUrlRewriteRules().size() > 0) {
boolean matched = false;
Expand Down Expand Up @@ -187,17 +188,17 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}
}
invokeApi(exchange, (String)config.getAccessToken().get(config.SERVICE_HOST), requestPath, "Bearer " + accessToken, startTime);
invokeApi(exchange, (String)config.getAccessToken().get(config.SERVICE_HOST), requestPath, "Bearer " + accessToken, startTime, endpoint);
if(logger.isDebugEnabled()) logger.debug("MrasHandler.handleRequest ends.");
return;
} else if(config.getPathPrefixAuth().get(key).equals(config.BASIC_AUTH)) {
// only basic authentication is used for the access.
invokeApi(exchange, (String)config.getBasicAuth().get(config.SERVICE_HOST), requestPath, "Basic " + encodeCredentials((String)config.getBasicAuth().get(config.USERNAME), (String)config.getBasicAuth().get(config.PASSWORD)), startTime);
invokeApi(exchange, (String)config.getBasicAuth().get(config.SERVICE_HOST), requestPath, "Basic " + encodeCredentials((String)config.getBasicAuth().get(config.USERNAME), (String)config.getBasicAuth().get(config.PASSWORD)), startTime, endpoint);
if(logger.isDebugEnabled()) logger.debug("MrasHandler.handleRequest ends.");
return;
} else if(config.getPathPrefixAuth().get(key).equals(config.ANONYMOUS)) {
// no authorization header for this type of the request.
invokeApi(exchange, (String)config.getAnonymous().get(config.SERVICE_HOST), requestPath, null, startTime);
invokeApi(exchange, (String)config.getAnonymous().get(config.SERVICE_HOST), requestPath, null, startTime, endpoint);
if(logger.isDebugEnabled()) logger.debug("MrasHandler.handleRequest ends.");
return;
} else if(config.getPathPrefixAuth().get(key).equals(config.MICROSOFT)) {
Expand All @@ -214,7 +215,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}
}
invokeApi(exchange, (String)config.getMicrosoft().get(config.SERVICE_HOST), requestPath, "Bearer " + microsoft, startTime);
invokeApi(exchange, (String)config.getMicrosoft().get(config.SERVICE_HOST), requestPath, "Bearer " + microsoft, startTime, endpoint);
if(logger.isDebugEnabled()) logger.debug("MrasHandler.handleRequest ends.");
return;
}
Expand All @@ -226,7 +227,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
Handler.next(exchange, next);
}

private void invokeApi(HttpServerExchange exchange, String serviceHost, String requestPath, String authorization, long startTime) throws Exception {
private void invokeApi(HttpServerExchange exchange, String serviceHost, String requestPath, String authorization, long startTime, String endpoint) throws Exception {
// call the MRAS API directly here with the token from the cache.
String method = exchange.getRequestMethod().toString();
String queryString = exchange.getQueryString();
Expand Down Expand Up @@ -333,7 +334,7 @@ private void invokeApi(HttpServerExchange exchange, String serviceHost, String r
exchange.getResponseSender().send(ByteBuffer.wrap(responseBody));
if(config.isMetricsInjection() && metricsHandler != null) {
if(logger.isTraceEnabled()) logger.trace("inject metrics for " + config.getMetricsName());
metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName());
metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName(), endpoint);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
// make sure that the request path is in the key set. remember that key set only contains prefix not the full request path.
for(PathPrefixAuth pathPrefixAuth: config.getPathPrefixAuths()) {
if(requestPath.startsWith(pathPrefixAuth.getPathPrefix())) {
String endpoint = pathPrefixAuth.getPathPrefix() + "@" + exchange.getRequestMethod().toString();
// handle the url rewrite here.
if(config.getUrlRewriteRules() != null && config.getUrlRewriteRules().size() > 0) {
boolean matched = false;
Expand Down Expand Up @@ -185,7 +186,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}
}
invokeApi(exchange, "Bearer " + pathPrefixAuth.getAccessToken(), pathPrefixAuth.getServiceHost(), requestPath, startTime);
invokeApi(exchange, "Bearer " + pathPrefixAuth.getAccessToken(), pathPrefixAuth.getServiceHost(), requestPath, startTime, endpoint);
if(logger.isDebugEnabled()) logger.debug("SalesforceHandler.handleRequest ends.");
return;
}
Expand Down Expand Up @@ -370,7 +371,7 @@ private Result<TokenResponse> getAccessToken(String serverUrl, String jwt) throw
}
}

private void invokeApi(HttpServerExchange exchange, String authorization, String requestHost, String requestPath, long startTime) throws Exception {
private void invokeApi(HttpServerExchange exchange, String authorization, String requestHost, String requestPath, long startTime, String endpoint) throws Exception {
// call the Salesforce API directly here with the token from the cache.
String method = exchange.getRequestMethod().toString();
String queryString = exchange.getQueryString();
Expand Down Expand Up @@ -435,7 +436,7 @@ private void invokeApi(HttpServerExchange exchange, String authorization, String
exchange.getResponseSender().send(ByteBuffer.wrap(responseBody));
if(config.isMetricsInjection() && metricsHandler != null) {
if(logger.isTraceEnabled()) logger.trace("injecting metrics for " + config.getMetricsName());
metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName());
metricsHandler.injectMetrics(exchange, startTime, config.getMetricsName(), endpoint);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ public void incCounterForStatusCode(int statusCode, Map<String, String> commonTa
* @param httpServerExchange the HttpServerExchange that is used to get the auditInfo to collect the metrics tag.
* @param startTime the start time passed in to calculate the response time.
* @param metricsName the name of the metrics that is collected.
* @param endpoint the endpoint that is used to collect the metrics. It is optional and only provided by the external handlers.
*/
public void injectMetrics(HttpServerExchange httpServerExchange, long startTime, String metricsName) {
public void injectMetrics(HttpServerExchange httpServerExchange, long startTime, String metricsName, String endpoint) {
Map<String, Object> auditInfo = httpServerExchange.getAttachment(AttachmentConstants.AUDIT_INFO);
if(logger.isTraceEnabled()) logger.trace("auditInfo = " + auditInfo);
Map<String, String> tags = new HashMap<>();
if (auditInfo != null) {
Map<String, String> tags = new HashMap<>();
tags.put(Constants.ENDPOINT_STRING, (String) auditInfo.get(Constants.ENDPOINT_STRING));
String clientId = auditInfo.get(Constants.CLIENT_ID_STRING) != null ? (String) auditInfo.get(Constants.CLIENT_ID_STRING) : "unknown";
if(logger.isTraceEnabled()) logger.trace("clientId = " + clientId);
Expand Down Expand Up @@ -127,13 +128,26 @@ public void injectMetrics(HttpServerExchange httpServerExchange, long startTime,
}
}
}
MetricName metricName = new MetricName(metricsName);
metricName = metricName.tagged(commonTags);
metricName = metricName.tagged(tags);
long time = System.nanoTime() - startTime;
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.TIMERS).update(time, TimeUnit.NANOSECONDS);
if(logger.isTraceEnabled()) logger.trace("metricName = " + metricName + " commonTags = " + JsonMapper.toJson(commonTags) + " tags = " + JsonMapper.toJson(tags));
incCounterForStatusCode(httpServerExchange.getStatusCode(), commonTags, tags);
} else {
// for MRAS and Salesforce handlers that do not have auditInfo in the exchange as they may be called anonymously.
tags.put(Constants.ENDPOINT_STRING, httpServerExchange.getRequestPath());
tags.put("clientId", "unknown");
if (config.isSendScopeClientId()) {
tags.put("scopeClientId", "unknown");
}
if (config.isSendCallerId()) {
tags.put("callerId", "unknown");
}
if (config.isSendIssuer()) {
tags.put("issuer", "unknown");
}
}
MetricName metricName = new MetricName(metricsName);
metricName = metricName.tagged(commonTags);
metricName = metricName.tagged(tags);
long time = System.nanoTime() - startTime;
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.TIMERS).update(time, TimeUnit.NANOSECONDS);
if(logger.isTraceEnabled()) logger.trace("metricName = " + metricName + " commonTags = " + JsonMapper.toJson(commonTags) + " tags = " + JsonMapper.toJson(tags));
incCounterForStatusCode(httpServerExchange.getStatusCode(), commonTags, tags);
}
}

0 comments on commit 80d4efe

Please sign in to comment.