Skip to content

Commit

Permalink
Merge pull request #12211 from RakhithaRR/AI-5285
Browse files Browse the repository at this point in the history
Add null checks and default values for backend related metric properties
  • Loading branch information
RakhithaRR authored Jan 26, 2024
2 parents 6df6f70 + 1300378 commit 61fae1e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.commons.CorrelationConstants;
Expand Down Expand Up @@ -204,6 +205,9 @@ public Target getTarget() {
Target target = new Target();

String endpointAddress = (String) messageContext.getProperty(APIMgtGatewayConstants.SYNAPSE_ENDPOINT_ADDRESS);
if (endpointAddress == null) {
endpointAddress = APIMgtGatewayConstants.DUMMY_ENDPOINT_ADDRESS;
}
int targetResponseCode = getTargetResponseCode();
target.setResponseCacheHit(isCacheHit());
target.setDestination(endpointAddress);
Expand Down Expand Up @@ -268,6 +272,9 @@ public int getProxyResponseCode() {

Object clientResponseCodeObj = ((Axis2MessageContext) messageContext).getAxis2MessageContext()
.getProperty(SynapseConstants.HTTP_SC);
if (clientResponseCodeObj == null) {
return HttpStatus.SC_OK;
}
int proxyResponseCode;
if (clientResponseCodeObj instanceof Integer) {
proxyResponseCode = (int) clientResponseCodeObj;
Expand Down Expand Up @@ -405,9 +412,15 @@ public long getBackendLatency() {
if (isCacheHit()) {
return 0L;
}
long backendStartTime = (long) messageContext.getProperty(Constants.BACKEND_START_TIME_PROPERTY);
long backendEndTime = (long) messageContext.getProperty(Constants.BACKEND_END_TIME_PROPERTY);
return backendEndTime - backendStartTime;
Object backendStartTimeObj = messageContext.getProperty(Constants.BACKEND_START_TIME_PROPERTY);
long backendStartTime = backendStartTimeObj == null ? 0L : (long) backendStartTimeObj;
Object backendEndTimeObj = messageContext.getProperty(Constants.BACKEND_END_TIME_PROPERTY);
long backendEndTime = backendEndTimeObj == null ? 0L : (long) backendEndTimeObj;
if (backendStartTime == 0L || backendEndTime == 0L) {
return 0L;
} else {
return backendEndTime - backendStartTime;
}
}

public long getResponseLatency() {
Expand All @@ -422,17 +435,27 @@ public long getRequestMediationLatency() {
if (isCacheHit()) {
return System.currentTimeMillis() - requestInTime;
}
long backendStartTime = (long) messageContext.getProperty(Constants.BACKEND_START_TIME_PROPERTY);
return backendStartTime - requestInTime;
Object backendStartTimeObj = messageContext.getProperty(Constants.BACKEND_START_TIME_PROPERTY);
long backendStartTime = backendStartTimeObj == null ? 0L : (long) backendStartTimeObj;
if (backendStartTime == 0L) {
return System.currentTimeMillis() - requestInTime;
} else {
return backendStartTime - requestInTime;
}
}

public long getResponseMediationLatency() {

if (isCacheHit()) {
return 0;
}
long backendEndTime = (long) messageContext.getProperty(Constants.BACKEND_END_TIME_PROPERTY);
return System.currentTimeMillis() - backendEndTime;
Object backendEndTimeObj = messageContext.getProperty(Constants.BACKEND_END_TIME_PROPERTY);
long backendEndTime = backendEndTimeObj == null ? 0L : (long) backendEndTimeObj;
if (backendEndTime == 0L) {
return 0L;
} else {
return System.currentTimeMillis() - backendEndTime;
}
}

public int getResponseSize() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsCustomDataProvider;
import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants;

public class SynapseAnalyticsDataProviderTestCase {

@Test
public void testMetricsWhenMessageContextPropertiesAreNull() {
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
org.apache.axis2.context.MessageContext axis2MsgCntxt =
Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
AnalyticsCustomDataProvider analyticsCustomDataProvider = Mockito.mock(AnalyticsCustomDataProvider.class);
SynapseAnalyticsDataProvider synapseAnalyticsDataProvider = new SynapseAnalyticsDataProvider(messageContext,
analyticsCustomDataProvider);
Mockito.when((messageContext.getProperty(APIMgtGatewayConstants.SYNAPSE_ENDPOINT_ADDRESS))).thenReturn(null);
Mockito.when((messageContext.getProperty(SynapseConstants.HTTP_SC))).thenReturn(null);
Mockito.when((messageContext.getProperty(Constants.BACKEND_START_TIME_PROPERTY))).thenReturn(null);
Mockito.when((messageContext.getProperty(Constants.BACKEND_END_TIME_PROPERTY))).thenReturn(null);
Assert.assertEquals(APIMgtGatewayConstants.DUMMY_ENDPOINT_ADDRESS,
synapseAnalyticsDataProvider.getTarget().getDestination());
Assert.assertEquals(200, synapseAnalyticsDataProvider.getProxyResponseCode());
Assert.assertEquals(0, synapseAnalyticsDataProvider.getBackendLatency());
Assert.assertEquals(0, synapseAnalyticsDataProvider.getResponseMediationLatency());
}
}

0 comments on commit 61fae1e

Please sign in to comment.