-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resource level filter logging feature added #12106
Changes from all commits
e1b423e
d57654e
1de9212
d69f1b1
2fadd3d
d3f2e0b
2c446ab
7eb9ec2
b71e155
a29e40d
2529c77
8f19598
dacb9dd
557a1d5
391d5ea
5f63a96
fc24c50
93ac087
3b43bea
ca44923
cf75c98
bac6cf5
c1dfcf2
bf26843
97315bb
13f8260
e599fcc
7e7e6a2
b34c138
d14d56d
e5dda07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ | |
*/ | ||
public class APILoggerManager { | ||
private static final Log log = LogFactory.getLog(APILoggerManager.class); | ||
private static final Map<String, String> logProperties = new HashMap<>(); | ||
private Map<Map<String, String>, String> logProperties = new HashMap<>(); | ||
private static final APILoggerManager apiLoggerManager = new APILoggerManager(); | ||
private final EventHubConfigurationDto eventHubConfigurationDto; | ||
public static final int RETRIEVAL_RETRIES = 15; | ||
|
@@ -61,7 +61,17 @@ public void initializeAPILoggerList() { | |
JSONArray apiLogArray = responseJson.getJSONArray("apis"); | ||
for (int i = 0; i < apiLogArray.length(); i++) { | ||
JSONObject apiLoggerObject = apiLogArray.getJSONObject(i); | ||
logProperties.put(apiLoggerObject.getString("context"), apiLoggerObject.getString("logLevel")); | ||
String resourceMethod = null; | ||
String resourcePath = null; | ||
if(!apiLoggerObject.isNull("resourceMethod") && !apiLoggerObject.isNull("resourcePath") ){ | ||
resourceMethod = apiLoggerObject.getString("resourceMethod"); | ||
resourcePath = apiLoggerObject.getString("resourcePath"); | ||
} | ||
Map<String, String> properties = new HashMap<>(); | ||
properties.put("context", apiLoggerObject.getString("context")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use constants and refer. There are many places referring to string like |
||
properties.put("resourceMethod", resourceMethod); | ||
properties.put("resourcePath", resourcePath); | ||
logProperties.put(properties, apiLoggerObject.getString("logLevel")); | ||
} | ||
if (log.isDebugEnabled()) { | ||
log.debug("Response : " + responseString); | ||
|
@@ -71,11 +81,15 @@ public void initializeAPILoggerList() { | |
} | ||
} | ||
|
||
public void updateLoggerMap(String apiContext, String logLevel) { | ||
logProperties.put(apiContext, logLevel); | ||
public void updateLoggerMap(String apiContext, String logLevel, String resourceMethod, String resourcePath) { | ||
Map<String, String> properties = new HashMap<>(); | ||
properties.put("context", apiContext); | ||
properties.put("resourcePath", resourcePath); | ||
properties.put("resourceMethod", resourceMethod); | ||
logProperties.put(properties, logLevel); | ||
} | ||
|
||
public Map<String, String> getPerAPILoggerList() { | ||
public Map<Map<String, String>, String> getPerAPILoggerList() { | ||
return logProperties; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,19 @@ | |
|
||
package org.wso2.carbon.apimgt.gateway.handlers; | ||
|
||
import org.apache.axis2.Constants; | ||
import org.apache.http.HttpHeaders; | ||
import org.apache.synapse.MessageContext; | ||
import org.apache.synapse.api.API; | ||
import org.apache.synapse.api.ApiUtils; | ||
import org.apache.synapse.api.Resource; | ||
import org.apache.synapse.api.dispatch.DispatcherHelper; | ||
import org.apache.synapse.api.dispatch.RESTDispatcher; | ||
import org.apache.synapse.core.axis2.Axis2MessageContext; | ||
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; | ||
import org.wso2.carbon.apimgt.impl.APIConstants; | ||
|
||
import java.util.Map; | ||
import java.util.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better not to use wild card imports. Importing specific class improves readability |
||
|
||
/** | ||
* Provides util methods for the LogsHandler | ||
|
@@ -91,7 +97,7 @@ protected static String getElectedResource(org.apache.synapse.MessageContext mes | |
return (String) messageContext.getProperty("API_ELECTED_RESOURCE"); | ||
} | ||
|
||
protected static String getResourceCacheKey(org.apache.synapse.MessageContext messageContext){ | ||
protected static String getResourceCacheKey(org.apache.synapse.MessageContext messageContext) { | ||
return (String) messageContext.getProperty("API_RESOURCE_CACHE_KEY"); | ||
} | ||
|
||
|
@@ -119,16 +125,99 @@ protected static String getTransportInURL(org.apache.synapse.MessageContext mess | |
return transportInURL.substring(1); | ||
} | ||
|
||
protected static String getMatchingLogLevel(MessageContext ctx, Map<String, String> logProperties) { | ||
String apiCtx = LogUtils.getTransportInURL(ctx); | ||
for (Map.Entry<String, String> entry : logProperties.entrySet()) { | ||
String key = entry.getKey().substring(1); | ||
if (apiCtx.startsWith(key + "/") || apiCtx.equals(key)) { | ||
ctx.setProperty(LogsHandler.LOG_LEVEL, entry.getValue()); | ||
ctx.setProperty("API_TO", apiCtx); | ||
return entry.getValue(); | ||
protected static String getMatchingLogLevel(MessageContext messageContext, | ||
Map<Map<String, String>, String> logProperties) { | ||
//initializing variables to store resource level logging | ||
String apiLogLevel = null; | ||
String resourceLogLevel = null; | ||
String resourcePath = null; | ||
String resourceMethod = null; | ||
Resource selectedResource = null; | ||
//obtain the selected API by context and path | ||
API selectedApi = null; | ||
Collection<API> apiSet = messageContext.getEnvironment().getSynapseConfiguration().getAPIs(); | ||
//identify the api | ||
for (API api : apiSet) { | ||
if (ApiUtils.identifyApi(api, messageContext)) { | ||
selectedApi = api; | ||
break; | ||
} | ||
} | ||
return null; | ||
String apiContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext() | ||
.getProperty("TransportInURL").toString(); | ||
String httpMethod = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext() | ||
.getProperty(Constants.Configuration.HTTP_METHOD); | ||
|
||
if (selectedApi != null) { | ||
Utils.setSubRequestPath(selectedApi, messageContext); | ||
//iterating through all the existing resources to match with the requesting method | ||
Map<String, Resource> resourcesMap = selectedApi.getResourcesMap(); | ||
Set<Resource> acceptableResources = ApiUtils | ||
.getAcceptableResources(resourcesMap, messageContext); | ||
if (!acceptableResources.isEmpty()) { | ||
for (RESTDispatcher dispatcher : ApiUtils.getDispatchers()) { | ||
selectedResource = dispatcher.findResource(messageContext, acceptableResources); | ||
if (selectedResource != null) { | ||
DispatcherHelper helper = selectedResource.getDispatcherHelper(); | ||
for (Map.Entry<Map<String, String>, String> entry : logProperties.entrySet()) { | ||
Map<String, String> key = entry.getKey(); | ||
//if resource path is empty, proceeding with API level logs | ||
if (selectedApi.getContext().equals(key.get("context"))) { | ||
if (key.get("resourcePath") == null && key.get("resourceMethod") == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use constants previously defined |
||
apiLogLevel = entry.getValue(); | ||
//matching the methods first and then the resource path | ||
} else if (httpMethod.equals(key.get("resourceMethod"))) { | ||
if (helper.getString().equals(key.get("resourcePath"))) { | ||
resourceLogLevel = entry.getValue(); | ||
resourcePath = key.get("resourcePath"); | ||
resourceMethod = key.get("resourceMethod"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
boolean isResourceLevelHasHighPriority = false; | ||
if (resourceLogLevel != null) { | ||
switch (resourceLogLevel) { | ||
case APIConstants.LOG_LEVEL_FULL: | ||
isResourceLevelHasHighPriority = true; | ||
break; | ||
case APIConstants.LOG_LEVEL_STANDARD: | ||
if (apiLogLevel != null && (apiLogLevel.equals(APIConstants.LOG_LEVEL_BASIC) | ||
|| apiLogLevel.equals(APIConstants.LOG_LEVEL_OFF))) { | ||
isResourceLevelHasHighPriority = true; | ||
break; | ||
} else { | ||
break; | ||
} | ||
case APIConstants.LOG_LEVEL_BASIC: | ||
if (apiLogLevel == null || apiLogLevel.equals(APIConstants.LOG_LEVEL_OFF)) { | ||
isResourceLevelHasHighPriority = true; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (isResourceLevelHasHighPriority || apiLogLevel == null) { | ||
messageContext.setProperty(LogsHandler.LOG_LEVEL, resourceLogLevel); | ||
messageContext.setProperty(LogsHandler.RESOURCE_PATH, resourcePath); | ||
messageContext.setProperty(LogsHandler.RESOURCE_METHOD, resourceMethod); | ||
messageContext.setProperty("API_TO", apiContext); | ||
return resourceLogLevel; | ||
} else { | ||
messageContext.setProperty(LogsHandler.LOG_LEVEL, apiLogLevel); | ||
messageContext.setProperty("API_TO", apiContext); | ||
return apiLogLevel; | ||
} | ||
} else if (apiLogLevel != null) { | ||
messageContext.setProperty(LogsHandler.LOG_LEVEL, apiLogLevel); | ||
messageContext.setProperty("API_TO", apiContext); | ||
return apiLogLevel; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
404 for not found