1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.synapse.statistics; 20 21 import org.apache.synapse.SynapseConstants; 22 import org.apache.synapse.MessageContext; 23 import org.apache.synapse.endpoints.Endpoint; 24 import org.apache.synapse.core.SynapseEnvironment; 25 import org.apache.synapse.statistics.impl.SequenceStatisticsStack; 26 import org.apache.synapse.statistics.impl.EndPointStatisticsStack; 27 import org.apache.synapse.statistics.impl.ProxyServiceStatisticsStack; 28 29 /** 30 * A utils to process statistics 31 * 32 */ 33 34 public class StatisticsUtils { 35 36 /** 37 * To process statistics related to the proxy services 38 * 39 * @param synCtx 40 */ 41 public static void processProxyServiceStatistics(MessageContext synCtx) { 42 43 StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx); 44 boolean isFault = synCtx.getEnvelope().getBody().hasFault(); 45 ProxyServiceStatisticsStack proxyServiceStatisticsStack = (ProxyServiceStatisticsStack) 46 synCtx.getProperty(SynapseConstants.PROXY_STATS); 47 if (proxyServiceStatisticsStack != null) { 48 proxyServiceStatisticsStack.reportToStatisticsCollector(statisticsCollector,isFault); 49 } 50 ProxyServiceStatisticsStack synapseServiceStatisticsStack = (ProxyServiceStatisticsStack) 51 synCtx.getProperty(SynapseConstants.SERVICE_STATS); 52 if (synapseServiceStatisticsStack != null) { 53 synapseServiceStatisticsStack.reportToStatisticsCollector(statisticsCollector,isFault); 54 } 55 } 56 57 /** 58 * To process statistics related to the End Points 59 * 60 * @param synCtx 61 */ 62 public static void processEndPointStatistics(MessageContext synCtx) { 63 StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx); 64 boolean isFault = synCtx.getEnvelope().getBody().hasFault(); 65 EndPointStatisticsStack endPointStatisticsStack = (EndPointStatisticsStack) 66 synCtx.getProperty(SynapseConstants.ENDPOINT_STATS); 67 if (endPointStatisticsStack != null) { 68 Object endpointObj = synCtx.getProperty(SynapseConstants.PROCESSED_ENDPOINT); 69 if (endpointObj instanceof Endpoint) { 70 Endpoint endpoint = (Endpoint) endpointObj; 71 String name = endpoint.getName(); 72 if (name == null) { 73 endPointStatisticsStack.reportToStatisticsCollector( 74 statisticsCollector, isFault); 75 } else { 76 endPointStatisticsStack.reportToStatisticsCollector( 77 statisticsCollector, isFault, name); 78 } 79 endPointStatisticsStack.reportAllToStatisticsCollector(statisticsCollector, true); 80 } 81 } 82 } 83 84 /** 85 * To process statistics related to the sequence 86 * 87 * @param synCtx 88 */ 89 public static void processSequenceStatistics(MessageContext synCtx) { 90 StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx); 91 boolean isFault = synCtx.getEnvelope().getBody().hasFault(); 92 SequenceStatisticsStack sequenceStatisticsStack = (SequenceStatisticsStack) 93 synCtx.getProperty(SynapseConstants.SEQUENCE_STATS); 94 if (sequenceStatisticsStack != null) { 95 sequenceStatisticsStack.reportToStatisticsCollector(statisticsCollector,isFault); 96 } 97 } 98 99 /** 100 * To process all statistics related to the sequence 101 * 102 * @param synCtx 103 */ 104 public static void processAllSequenceStatistics(MessageContext synCtx) { 105 StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx); 106 boolean isFault = synCtx.getEnvelope().getBody().hasFault(); 107 SequenceStatisticsStack sequenceStatisticsStack = (SequenceStatisticsStack) 108 synCtx.getProperty(SynapseConstants.SEQUENCE_STATS); 109 if (sequenceStatisticsStack != null) { 110 sequenceStatisticsStack.reportAllToStatisticsCollector(statisticsCollector,isFault); 111 } 112 } 113 /** 114 * A helper method to get StatisticsCollector from the Synapse Message Context 115 * 116 * @param synCtx 117 * @return StatisticsCollector 118 */ 119 private static StatisticsCollector getStatisticsCollector(MessageContext synCtx) { 120 SynapseEnvironment synEnv = synCtx.getEnvironment(); 121 StatisticsCollector statisticsCollector = null; 122 if (synEnv != null) { 123 statisticsCollector = synEnv.getStatisticsCollector(); 124 if (statisticsCollector == null) { 125 statisticsCollector = new StatisticsCollector(); 126 synEnv.setStatisticsCollector(statisticsCollector); 127 } 128 } 129 return statisticsCollector; 130 } 131 }