Skip to content
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

Improve analytics data handler by including additional params #12152

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Contain the common data collectors.
*/
public abstract class CommonRequestDataCollector extends AbstractRequestDataCollector {
private static final Log log = LogFactory.getLog(SuccessRequestDataCollector.class);
private static final Log log = LogFactory.getLog(CommonRequestDataCollector.class);

public CommonRequestDataCollector(AnalyticsDataProvider provider) {
super(provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class SuccessRequestDataCollector extends CommonRequestDataCollector impl
private static final Log log = LogFactory.getLog(SuccessRequestDataCollector.class);
private RequestDataPublisher processor;
private AnalyticsDataProvider provider;

public SuccessRequestDataCollector(AnalyticsDataProvider provider, RequestDataPublisher processor) {
super(provider);
this.processor = processor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
if (APIUtil.isAnalyticsEnabled()) {
WebSocketUtils.setApiPropertyToChannel(ctx, Constants.BACKEND_END_TIME_PROPERTY,
System.currentTimeMillis());
if (msg instanceof TextWebSocketFrame) {
WebSocketUtils.setApiPropertyToChannel(ctx, Constants.RESPONSE_SIZE,
((TextWebSocketFrame) msg).text().length());

Check warning on line 134 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/WebsocketHandler.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/WebsocketHandler.java#L133-L134

Added lines #L133 - L134 were not covered by tests
}
}
// publish analytics events if analytics is enabled
publishSubscribeEvent(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@
if (APIUtil.isAnalyticsEnabled()) {
WebSocketUtils.setApiPropertyToChannel(ctx, Constants.REQUEST_END_TIME_PROPERTY,
System.currentTimeMillis());
if (msg instanceof TextWebSocketFrame) {
WebSocketUtils.setApiPropertyToChannel(ctx, Constants.RESPONSE_SIZE,
((TextWebSocketFrame) msg).text().length());

Check warning on line 274 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/WebsocketInboundHandler.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/WebsocketInboundHandler.java#L273-L274

Added lines #L273 - L274 were not covered by tests
}
}
publishPublishEvent(ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class Constants {
public static final String REQUEST_CACHE_HIT = "api.analytics.cacheHit";
public static final String API_USER_NAME_KEY = "userName";
public static final String API_CONTEXT_KEY = "apiContext";
public static final String RESPONSE_SIZE = "responseSize";
public static final String RESPONSE_CONTENT_TYPE = "responseContentType";
public static final String API_ANALYTICS_CUSTOM_DATA_PROVIDER_CLASS = "publisher.custom.data.provider.class";

public static final String REGION_ID_PROP = "apim.gw.region";
Expand Down Expand Up @@ -68,6 +70,6 @@ public static final class ERROR_CODE_RANGES {
public static final String API_GOOGLE_ANALYTICS_MEASUREMENT_ID = "MeasurementID";
public static final String API_GOOGLE_ANALYTICS_API_SECRET = "APISecret";
public static final String X_FORWARDED_FOR_HEADER = "X-Forwarded-For";

public static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR";
public static final String BUILD_RESPONSE_MESSAGE_CONFIG = "build_response_message";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

package org.wso2.carbon.apimgt.gateway.handlers.analytics;

import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHeaders;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.commons.CorrelationConstants;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.RESTConstants;
import org.apache.synapse.transport.passthru.util.RelayUtils;
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsCustomDataProvider;
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider;
import org.wso2.carbon.apimgt.common.analytics.exceptions.DataNotFoundException;
Expand All @@ -43,22 +47,30 @@
import org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext;
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder;
import org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore;
import org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException;
import org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS;
import static org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants.UNKNOWN_VALUE;

public class SynapseAnalyticsDataProvider implements AnalyticsDataProvider {

private static final Log log = LogFactory.getLog(SynapseAnalyticsDataProvider.class);
private MessageContext messageContext;
private AnalyticsCustomDataProvider analyticsCustomDataProvider;
private Boolean buildResponseMessage = null;

Check warning on line 73 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L73

Added line #L73 was not covered by tests

public SynapseAnalyticsDataProvider(MessageContext messageContext) {

Expand Down Expand Up @@ -337,6 +349,8 @@
}
customProperties.put(Constants.API_USER_NAME_KEY, getUserName());
customProperties.put(Constants.API_CONTEXT_KEY, getApiContext());
customProperties.put(Constants.RESPONSE_SIZE, getResponseSize());
customProperties.put(Constants.RESPONSE_CONTENT_TYPE, getResponseContentType());

Check warning on line 353 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L352-L353

Added lines #L352 - L353 were not covered by tests
return customProperties;
}

Expand Down Expand Up @@ -421,4 +435,49 @@
return System.currentTimeMillis() - backendEndTime;
}

public int getResponseSize() {
int responseSize = 0;

Check warning on line 439 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L439

Added line #L439 was not covered by tests
if (buildResponseMessage == null) {
Map<String,String> configs = APIManagerConfiguration.getAnalyticsProperties();

Check warning on line 441 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L441

Added line #L441 was not covered by tests
if (configs.containsKey(Constants.BUILD_RESPONSE_MESSAGE_CONFIG)) {
buildResponseMessage = Boolean.parseBoolean(configs.get(Constants.BUILD_RESPONSE_MESSAGE_CONFIG));

Check warning on line 443 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L443

Added line #L443 was not covered by tests
} else {
buildResponseMessage = false;

Check warning on line 445 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L445

Added line #L445 was not covered by tests
}
}
Map headers = (Map) messageContext.getProperty(TRANSPORT_HEADERS);

Check warning on line 448 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L448

Added line #L448 was not covered by tests
if (headers != null && headers.get(HttpHeaders.CONTENT_LENGTH) != null) {
responseSize = Integer.parseInt(headers.get(HttpHeaders.CONTENT_LENGTH).toString());

Check warning on line 450 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L450

Added line #L450 was not covered by tests
}
if (responseSize == 0 && buildResponseMessage) {
try {
RelayUtils.buildMessage(((Axis2MessageContext) messageContext).getAxis2MessageContext());
} catch (IOException ex) {

Check warning on line 455 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L454-L455

Added lines #L454 - L455 were not covered by tests
//In case of an exception, it won't be propagated up,and set response size to 0
log.error("Error occurred while building the message to" +

Check warning on line 457 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L457

Added line #L457 was not covered by tests
" calculate the response body size", ex);
} catch (XMLStreamException ex) {
log.error("Error occurred while building the message to calculate the response" +

Check warning on line 460 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L459-L460

Added lines #L459 - L460 were not covered by tests
" body size", ex);
}

Check warning on line 462 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L462

Added line #L462 was not covered by tests

SOAPEnvelope env = messageContext.getEnvelope();

Check warning on line 464 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L464

Added line #L464 was not covered by tests
if (env != null) {
SOAPBody soapbody = env.getBody();

Check warning on line 466 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L466

Added line #L466 was not covered by tests
if (soapbody != null) {
byte[] size = soapbody.toString().getBytes(Charset.defaultCharset());
responseSize = size.length;

Check warning on line 469 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L468-L469

Added lines #L468 - L469 were not covered by tests
}
}
}
return responseSize;

Check warning on line 473 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L473

Added line #L473 was not covered by tests
}

public String getResponseContentType() {
Map headers = (Map) messageContext.getProperty(TRANSPORT_HEADERS);

Check warning on line 477 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L477

Added line #L477 was not covered by tests
if (headers != null && headers.get(HttpHeaders.CONTENT_TYPE) != null) {
return headers.get(HttpHeaders.CONTENT_TYPE).toString();

Check warning on line 479 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L479

Added line #L479 was not covered by tests
}
return UNKNOWN_VALUE;

Check warning on line 481 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java#L481

Added line #L481 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@
}
customProperties.put(Constants.API_USER_NAME_KEY, getUserName());
customProperties.put(Constants.API_CONTEXT_KEY, getApiContext());
customProperties.put(Constants.RESPONSE_SIZE,
WebSocketUtils.getPropertyFromChannel(Constants.RESPONSE_SIZE, ctx));

Check warning on line 319 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/streaming/websocket/WebSocketAnalyticsDataProvider.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/streaming/websocket/WebSocketAnalyticsDataProvider.java#L318-L319

Added lines #L318 - L319 were not covered by tests
return customProperties;
}

Expand Down
Loading