Search code examples
loggingapache-camelload-balancing

how to log real destination endpoint of load balancer in apache camel


With Apache Camel I want to send files to a ftp server and if this server becomes unreachable then I want to send files to a backup ftp server. I do it with load balancer

<route id="ftp.failover">
  <from uri="file:/path/to/files"/>
  <loadBalance inheritErrorHandler="false">
    <failover roundRobin="false" sticky="false"/>
    <to uri="ftp://ftp_host"/>
    <to uri="ftp://backup_ftp_host"/>
  </loadBalance>
  <log message="Sent ${headers}"/>
</route>

Now, I want to see in log to which ftp server each file was sent and only if it was sent. Unfortunately, my <log /> message doesn't show any info about destination. I suppose that I need to add some ${out.header.foo} in log message but I can't imagine what header name I should specify instead of foo. So the question: is there a way to get logged the real destination in case of load balance in apache camel?

UPDATED: [solution]

Thanks to Vimsha, the solution was simple. There is only one thing which I want to mention: the CamelInterceptedEndpoint header populates as is without sanitizing so passwords become visible if present in uri. Additional log entry slightly pollute log file also. So instead of <log /> I simply rewrite CamelInterceptedEndpoint header.

<interceptSendToEndpoint uri="ftp:*">
    <setHeader headerName="CamelInterceptedEndpoint">
        <simple>${headers.CamelInterceptedEndpoint.replaceAll("\?.*","")}</simple>
    </setHeader>
</interceptSendToEndpoint>

After this my initial route began to log the real destination.

Also it is possible to simply sanitize the uri:

<setHeader headerName="CamelInterceptedEndpoint">
  <javaScript>
    org.apache.camel.util.URISupport.sanitizeUri(request.headers.get('CamelInterceptedEndpoint'))
  </javaScript>
</setHeader>

May be there is more convenient way to call camel's method but javascript is more simple to me.


Solution

  • Add an interceptor for ftp endpoints and log using the header CamelInterceptedEndpoint

    <interceptSendToEndpoint uri="ftp:*">
       <log message="Sending to ${headers.CamelInterceptedEndpoint}"/>
    </interceptSendToEndpoint>
    

    Your full route will look something like this

    <interceptSendToEndpoint uri="ftp:*">
       <log message="Sending to ${headers.CamelInterceptedEndpoint}"/>
    </interceptSendToEndpoint>
    
    <route id="ftp.failover">
      <from uri="file:/path/to/files"/>
      <loadBalance inheritErrorHandler="false">
         <failover roundRobin="false" sticky="false"/>
         <to uri="ftp://ftp_host"/>
         <to uri="ftp://backup_ftp_host"/>
      </loadBalance>
      <log message="Sent ${headers}"/>
    </route>