Social Icons

twitterfacebookgoogle pluslinkedinrss feed

Featured Posts

Saturday, June 07, 2014

WSO2 Governance Registry - Monitor database operations using log4jdbc

LOG4JDBC is a Java based database driver that can be used to log SQL and/or JDBC calls. So here I am going to show how to monitor JDBC operations on Governance Registry using log4jdbc.

Here I believe you have already configured Governance Registry instance with MySQL. If not, please follow the instruction available in the Governance Registry documentation.

1). Download the log4jdbc driver

 You can download log4jdbc driver from below location: https://code.google.com/p/log4jdbc/

2). Add log4jdbc driver

 Copy log4jdbc driver into CARBON_HOME/repository/components/lib directory.

3). Configure log4j.properties file.

Navigate to log4j.properties file located in CARBON_HOME/repository/conf/ directory and add below entry in to log4j.properties file.

# Log all JDBC calls except for ResultSet calls
log4j.logger.jdbc.audit=INFO,jdbc
log4j.additivity.jdbc.audit=false

# Log only JDBC calls to ResultSet objects
log4j.logger.jdbc.resultset=INFO,jdbc
log4j.additivity.jdbc.resultset=false

# Log only the SQL that is executed.
log4j.logger.jdbc.sqlonly=DEBUG,sql
log4j.additivity.jdbc.sqlonly=false

# Log timing information about the SQL that is executed.
log4j.logger.jdbc.sqltiming=DEBUG,sqltiming
log4j.additivity.jdbc.sqltiming=false

# Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=FATAL,connection
log4j.additivity.jdbc.connection=false

# the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.FileAppender
log4j.appender.sql.File=${carbon.home}/repository/logs/sql.log
log4j.appender.sql.Append=false
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n

# the appender used for the JDBC API layer call logging above, sql timing
log4j.appender.sqltiming=org.apache.log4j.FileAppender
log4j.appender.sqltiming.File=${carbon.home}/repository/logs/sqltiming.log
log4j.appender.sqltiming.Append=false
log4j.appender.sqltiming.layout=org.apache.log4j.PatternLayout
log4j.appender.sqltiming.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n

# the appender used for the JDBC API layer call logging above
log4j.appender.jdbc=org.apache.log4j.FileAppender
log4j.appender.jdbc.File=${carbon.home}/repository/logs/jdbc.log
log4j.appender.jdbc.Append=false
log4j.appender.jdbc.layout=org.apache.log4j.PatternLayout
log4j.appender.jdbc.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n

# the appender used for the JDBC Connection open and close events
log4j.appender.connection=org.apache.log4j.FileAppender
log4j.appender.connection.File=${carbon.home}/repository/logs/connection.log
log4j.appender.connection.Append=false
log4j.appender.connection.layout=org.apache.log4j.PatternLayout
log4j.appender.connection.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n



4). Update the master-datasources.xml file

Update the master-datasources.xml file located in CARBON_HOME/repository/conf/datasources directory. There each datasource URL and Drivers as below

<url>jdbc:log4jdbc:mysql://localhost:3306/amdb?autoReconnect=true</url>
<driverClassName>net.sf.log4jdbc.DriverSpy</driverClassName>

5). Enjoy

Some database drvers may not support by default(ex :db2), there you can pass database driver name as VM argument

-Dlog4jdbc.drivers=com.ibm.db2.jcc.DB2Driver

Restart the server and enjoy your work with log4jdbc. Log files are created under CARBON_HOME/repository/logs/ directory. So using sqltiming.log file you can monitor execution time of each query.

PS : If  you want to simulate lower bandwidth situation, you can use trickle when the server is starting.
(More)
         Example sh wso2server.sh trickle -d 64 -u 64

Friday, February 14, 2014

WSO2 Governance Registry - Apply Tags using Handler

There can be scenarios, where users want to apply tags into their resources, while resource is inserted. I am writing this post based on my answer provided to this Stack Overflow question.

Question was on how to apply a tag into a service in WSO2 Governance Registry at the time of the service creation. We can use Registry Handler for achieving this requirement. Handlers are the well-known extension points in WSO2 Governance Registry.

"Handlers are pluggable components, that contain custom processing logic for handling resources. All handlers extend an abstract class named Handler, which provides default implementations for resource handling methods as well as a few utilities useful for concrete handler implementations."[WSO2 Governance Registry Docs]

I have modified handler sample, which is shipped with Governance Registry pack. Using above link please download the G-Reg pack and handler sample is located at GREG_HOME/samples/handler.

 
public void put(RequestContext requestContext) throws RegistryException {

 if (!CommonUtil.isUpdateLockAvailable()) {
            return;
        }
        CommonUtil.acquireUpdateLock();
        try {
   String resourcePath = requestContext.getResourcePath().getPath();
   Registry registry = requestContext.getRegistry();
   registry.applyTag(resourcePath, "CustomTag");

  } finally {
   CommonUtil.releaseUpdateLock();
  }
 }

Compiled jar file need to be added into GREG_HOME/repository/components/dropins folder and register handler using either management console or registry.xml file.

<handler class="org.wso2.carbon.registry.samples.handler.CustomServiceHandler">
    <filter class="org.wso2.carbon.registry.core.jdbc.handlers.filters.MediaTypeMatcher">
        <property name="mediaType">application/vnd.wso2-service+xml</property>
    </filter>
</handler>

When you are inserting a new service, this handler will get hit and it will add the given tag into the Service.

Thursday, November 21, 2013

How to configure WSO2 G-Reg with ELB - Updated

This is the updated post for configuring WSO2 ELB 2.1.0 and G-Reg 4.6.0 releases. If you are using earlier releases, please refer the early post.

 When we are fronting WSO2 ELB(Elastic Load Balancer) for a WSO2 G-Reg(Governance Registry) node, all the incoming messages will go through ELB node and it will act as a HTTP and HTTPS proxy to GREG node. So when you configure ELB to the GREG front, GREG won't be able to access alone without the ELB.

1. Download WSO2 ELB and WSO2 GREG. Rename the extracted ELB as ELB-HOME and extracted GREG as GREG-HOME.

ELB Configuration 

2. Go to ELB-HOME/repository/conf/loadbalancer.conf and add the following entry. There can be multiple entries according to the clustering requirements.

 governance {

  domains   {

           wso2.governance.domain {

               tenant_range *;
               group_mgt_port 4000;
               mgt{
                   hosts governance.local.wso2.com;
               }
           }
       }
}

 3. Update hosts files in nodes with relevant IP and Domain information. In my example, G-Reg and ELB will be hosted on the same node.

127.0.0.1 governance.local.wso2.com 
if not this governance.local.wso2.com domain should be mapped into ELB node.

 4 Navigate to the repository/conf/axis2 directory and open the axis2.xml file and update localMemberPort parameter value as below. This port number should not conflict with any other port.

<parameter name="localMemberPort">5000</parameter>

Greg Configuration

4. Then clustering should be enabled in G-Reg as well. To enable it, go to GREG-HOME/repository/conf/axis2/axis2.xml and modify clustering configuration as below.


<clustering class="org.wso2.carbon.core.clustering.hazelcast.HazelcastClusteringAgent" enable="true">
 <parameter name="membershipScheme">wka </parameter> 
</clustering>

 5. Uncomment localmemberhost element in GREG-HOME/repository/conf/axis2/axis2.xml and specify the IP address (or host name) to be exposed to members of the cluster.

<parameter name="localMemberHost">127.0.0.1</parameter>


6. Then define clustering domain information.(in GREG-HOME/repository/conf/axis2/axis2.xml). This "domain" name value should be same as in loadbalancer.conf.

<parameter name="domain">wso2.governance.domain</parameter>
<parameter name="localMemberPort">4250</parameter>

7. As shown below, add "subDomain" information into the same axis2.xml file.

<parameter name="properties">
    <property name="backendServerURL" value="https://${hostName}:${httpsPort}/services/"/>
    <property name="mgtConsoleURL" value="https://${hostName}:${httpsPort}/"/>
    <property name="subDomain" value="mgt"/>
   </parameter>

8. Add load balancer IP address or host information in to same axis2.xml file. In this example scenario IP address is 127.0.0.1.

<members>
     <member>
        <hostname>127.0.0.1 </hostname>
        <port>4000</port>
     </member>
  </members>
</members>

9. Open the GREG-HOME/repository/conf/tomcat/catalina-server.xml file and add HTTP and HTTPS proxy port information to that file.

<connector port="9763" protocol="org.apache.coyote.http11.Http11NioProtocol" proxyport="8280"/> 
<connector port="9443" protocol="org.apache.coyote.http11.Http11NioProtocol" proxyport="8243"/>


10. Go to GREG-HOME/repository/conf/carbon.xml file and update the "HostName" and "MgtHostName" as below.

<hostname>governance.local.wso2.com</hostname>
<mgthostname>governance.local.wso2.com</mgthostname>

11. This step only need to be followed if you are hosting G-Reg and ELB both in same node. In this sample we have configured both ELB and G-Reg in same node. Therefore we should avoid port conflicts between ELB and G-Reg. To avoid such problems we can use "Offset" entry in carbon.xml file. Go to GREG-HOME/repository/conf/carbon.xml and change port offset value.

<offset>1</offset>

12. Start the ELB instance

13. Start the G-Reg instance(s)

14.You can access G-Reg instance using following URL        "https://governance.local.wso2.com:8243/carbon/"

Monday, September 30, 2013

Manage SOAPAction of the Out Message

 
 When you are sending a request message to a backend service through WSO2 ESB, there could be some scenarios where you need to remove or change the SOAPAction header value.


Using header mediator and property mediator which are available in WSO2 ESB, we can remove SOAPAction or set it empty.

Set SOAPAction as Empty:
<header name="Action" value=""/>
<property name="SOAPAction" scope="transport" value=""/>

Remove SOAPAction:
<header action="remove" name="Action"/> 
<property action="remove" name="SOAPAction" scope="transport"/>

Modify SOAPAction:

When setting SOAPAction one of the below approches can be used

1) .
<header name="Action" value="fixedAction"/>

2).
<header expression="xpath-expression" name="Action"/>

More Info: Header Mediator

TCPMon:

If we need to monitor the messages getting passed between ESB and backend service, we can point TCPMon[1] in between back-end and ESB. Using TCPMon, we can monitor messages and their header information(Including SOAPAction).

Bottom of the TCPMon there is a special control available to view Messages in XML format.

[1]. http://ws.apache.org/tcpmon/tcpmontutorial.html