Skip to content

Commit

Permalink
CAMEL-1933: Overhaul of JMX. Tracing can now be managed. And can enab…
Browse files Browse the repository at this point in the history
…led on the fly even if tracing was not defined when started. So you can start tracing running platforms. Need to expose more attributes/operations though so you can adjust trace formatting etc.

git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@808866 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
davsclaus committed Aug 28, 2009
1 parent 530147f commit f47adf7
Show file tree
Hide file tree
Showing 20 changed files with 447 additions and 26 deletions.
7 changes: 7 additions & 0 deletions camel-core/src/main/java/org/apache/camel/CamelContext.java
Expand Up @@ -535,4 +535,11 @@ public interface CamelContext extends Service, RuntimeConfiguration {
*/
void setManagementStrategy(ManagementStrategy strategy);

/**
* Gets the default tracer
*
* @return the default tracer
*/
InterceptStrategy getDefaultTracer();

}
Expand Up @@ -127,6 +127,7 @@ public class DefaultCamelContext extends ServiceSupport implements CamelContext
// so if we have 6 endpoints in the pool, we have 6 x 100 producers in total
private ServicePool<Endpoint, Producer> producerServicePool = new DefaultProducerServicePool(100);
private NodeIdFactory nodeIdFactory = new DefaultNodeIdFactory();
private Tracer defaultTracer;

public DefaultCamelContext() {
super();
Expand Down Expand Up @@ -888,12 +889,8 @@ protected synchronized void doStart() throws Exception {
}

if (isTracing()) {
// only add a new tracer if not already configured
if (Tracer.getTracer(this) == null) {
Tracer tracer = Tracer.createTracer(this);
LOG.debug("Tracing is enabled");
addInterceptStrategy(tracer);
}
// tracing is added in the DefaultChannel so we can enable it on the fly
LOG.debug("Tracing is enabled");
}

if (isHandleFault()) {
Expand Down Expand Up @@ -1191,6 +1188,13 @@ public void setManagementStrategy(ManagementStrategy managementStrategy) {
this.managementStrategy = managementStrategy;
}

public InterceptStrategy getDefaultTracer() {
if (defaultTracer == null) {
defaultTracer = new Tracer();
}
return defaultTracer;
}

protected synchronized String getEndpointKey(String uri, Endpoint endpoint) {
if (endpoint.isSingleton()) {
return uri;
Expand Down
Expand Up @@ -49,6 +49,7 @@ public class DefaultRouteContext implements RouteContext {
private final List<Processor> eventDrivenProcessors = new ArrayList<Processor>();
private CamelContext camelContext;
private List<InterceptStrategy> interceptStrategies = new ArrayList<InterceptStrategy>();
private InterceptStrategy managedInterceptStrategy;
private boolean routeAdded;
private Boolean trace;
private Boolean stramCache;
Expand Down Expand Up @@ -172,6 +173,14 @@ public void addInterceptStrategy(InterceptStrategy interceptStrategy) {
getInterceptStrategies().add(interceptStrategy);
}

public void setManagedInterceptStrategy(InterceptStrategy interceptStrategy) {
this.managedInterceptStrategy = interceptStrategy;
}

public InterceptStrategy getManagedInterceptStrategy() {
return managedInterceptStrategy;
}

public boolean isRouteAdded() {
return routeAdded;
}
Expand Down
Expand Up @@ -45,6 +45,7 @@
import org.apache.camel.management.mbean.ManagedRoute;
import org.apache.camel.management.mbean.ManagedSendProcessor;
import org.apache.camel.management.mbean.ManagedThrottler;
import org.apache.camel.management.mbean.ManagedTracer;
import org.apache.camel.model.AOPDefinition;
import org.apache.camel.model.InterceptDefinition;
import org.apache.camel.model.OnCompletionDefinition;
Expand All @@ -54,6 +55,7 @@
import org.apache.camel.processor.Delayer;
import org.apache.camel.processor.SendProcessor;
import org.apache.camel.processor.Throttler;
import org.apache.camel.processor.interceptor.Tracer;
import org.apache.camel.spi.BrowsableEndpoint;
import org.apache.camel.spi.ClassResolver;
import org.apache.camel.spi.LifecycleStrategy;
Expand Down Expand Up @@ -241,7 +243,10 @@ public void onServiceAdd(CamelContext context, Service service) {
}

Object managedObject;
if (service instanceof Processor) {
if (service instanceof Tracer) {
// special for tracer
managedObject = new ManagedTracer(context, (Tracer) service);
} else if (service instanceof Processor) {
// special for processors
managedObject = getManagedObjectForProcessor(context, (Processor) service);
} else {
Expand Down Expand Up @@ -271,7 +276,11 @@ public void onServiceRemove(CamelContext context, Service service) {
}

Object managedObject;
if (service instanceof Processor) {
if (service instanceof Tracer) {
// special for tracer
managedObject = new ManagedTracer(context, (Tracer) service);
} else if (service instanceof Processor) {
// special for processors
managedObject = getManagedObjectForProcessor(context, (Processor) service);
} else {
// regular for services
Expand Down Expand Up @@ -403,9 +412,9 @@ public void onRouteContextCreate(RouteContext routeContext) {
registerPerformanceCounters(routeContext, processor, registeredCounters);
}

// add intercept strategy that executes the JMX instrumentation for performance metrics
// set this managed intercept strategy that executes the JMX instrumentation for performance metrics
// so our registered counters can be used for fine grained performance instrumentation
routeContext.addInterceptStrategy(new InstrumentationInterceptStrategy(registeredCounters, wrappedProcessors));
routeContext.setManagedInterceptStrategy(new InstrumentationInterceptStrategy(registeredCounters, wrappedProcessors));
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.apache.camel.management.mbean.ManagedProcessor;
import org.apache.camel.management.mbean.ManagedRoute;
import org.apache.camel.management.mbean.ManagedService;
import org.apache.camel.management.mbean.ManagedTracer;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.spi.ManagementNamingStrategy;
import org.apache.camel.spi.RouteContext;
Expand All @@ -50,6 +51,7 @@ public class DefaultManagementNamingStrategy implements ManagementNamingStrategy
public static final String TYPE_CONSUMER = "consumers";
public static final String TYPE_ROUTE = "routes";
public static final String TYPE_COMPONENT = "components";
public static final String TYPE_TRACER = "tracer";

protected String domainName;
protected String hostName = "localhost";
Expand Down Expand Up @@ -138,6 +140,17 @@ public ObjectName getObjectName(ManagedService mbean) throws MalformedObjectName
return null;
}

public ObjectName getObjectName(ManagedTracer mbean) throws MalformedObjectNameException {
StringBuffer buffer = new StringBuffer();
buffer.append(domainName).append(":");
buffer.append(KEY_CONTEXT + "=").append(getContextId(mbean.getCamelContext())).append(",");
buffer.append(KEY_TYPE + "=" + TYPE_TRACER + ",");
buffer.append(KEY_NAME + "=")
.append("Tracer")
.append("(").append(getIdentityHashCode(mbean.getTracer())).append(")");
return createObjectName(buffer);
}

public ObjectName getObjectName(ManagedRoute mbean) throws MalformedObjectNameException {
Route route = mbean.getRoute();
Endpoint ep = route.getEndpoint();
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.apache.camel.management.mbean.ManagedEndpoint;
import org.apache.camel.management.mbean.ManagedProcessor;
import org.apache.camel.management.mbean.ManagedRoute;
import org.apache.camel.management.mbean.ManagedTracer;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.spi.ManagementAgent;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -98,6 +99,11 @@ public <T> T getManagedObjectName(Object managedObject, String customName, Class
objectName = getManagementNamingStrategy().getObjectName(ms);
}

if (managedObject instanceof ManagedTracer) {
ManagedTracer mt = (ManagedTracer) managedObject;
objectName = getManagementNamingStrategy().getObjectName(mt);
}

return nameType.cast(objectName);
}

Expand Down
Expand Up @@ -66,6 +66,16 @@ public Map<String, String> getProperties() {
return context.getProperties();
}

@ManagedAttribute(description = "Tracing")
public Boolean getTracing() {
return context.isTracing();
}

@ManagedAttribute(description = "Tracing")
public void setTracing(Boolean tracing) {
context.setTracing(tracing);
}

@ManagedOperation(description = "Start Camel")
public void start() throws Exception {
context.start();
Expand Down
Expand Up @@ -84,6 +84,16 @@ public String getCamelId() {
return context.getName();
}

@ManagedAttribute(description = "Tracing")
public Boolean getTracing() {
return route.getRouteContext().isTracing();
}

@ManagedAttribute(description = "Tracing")
public void setTracing(Boolean tracing) {
route.getRouteContext().setTracing(tracing);
}

@ManagedOperation(description = "Start Route")
public void start() throws Exception {
context.startRoute(getRouteId());
Expand Down
@@ -0,0 +1,60 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.camel.management.mbean;

import org.apache.camel.CamelContext;
import org.apache.camel.processor.interceptor.Tracer;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;

/**
* @version $Revision$
*/
@ManagedResource(description = "Managed Tracer")
public class ManagedTracer extends ManagedPerformanceCounter {

private CamelContext camelContext;
private Tracer tracer;

public ManagedTracer(CamelContext camelContext, Tracer tracer) {
super(camelContext.getManagementStrategy());
this.camelContext = camelContext;
this.tracer = tracer;
}

public CamelContext getCamelContext() {
return camelContext;
}

public Tracer getTracer() {
return tracer;
}

@ManagedAttribute(description = "Enabled")
public boolean getEnabled() {
return tracer.isEnabled();
}

@ManagedAttribute(description = "Enabled")
public void setEnabled(boolean enabled) {
tracer.setEnabled(enabled);
}

// TODO: add management for formatter etc.


}
Expand Up @@ -166,6 +166,9 @@ protected Processor wrapChannel(RouteContext routeContext, Processor processor)
// add interceptor strategies to the channel must be in this order: camel context, route context, local
addInterceptStrategies(routeContext, channel, routeContext.getCamelContext().getInterceptStrategies());
addInterceptStrategies(routeContext, channel, routeContext.getInterceptStrategies());
if (routeContext.getManagedInterceptStrategy() != null) {
channel.addInterceptStrategy(routeContext.getManagedInterceptStrategy());
}
addInterceptStrategies(routeContext, channel, this.getInterceptStrategies());

// init the channel
Expand Down Expand Up @@ -193,10 +196,6 @@ protected Processor wrapChannel(RouteContext routeContext, Processor processor)
*/
protected void addInterceptStrategies(RouteContext routeContext, Channel channel, List<InterceptStrategy> strategies) {
for (InterceptStrategy strategy : strategies) {
if (!routeContext.isTracing() && strategy instanceof Tracer) {
// trace is disabled so we should not add it
continue;
}
if (!routeContext.isStreamCaching() && strategy instanceof StreamCaching) {
// stream cache is disabled so we should not add it
continue;
Expand Down
Expand Up @@ -373,11 +373,7 @@ protected RouteContext addRoutes(Collection<Route> routes, FromDefinition fromTy
if (log.isDebugEnabled()) {
log.debug("Tracing is enabled on route: " + this);
}
// only add a new tracer if not already a global configured on camel context
if (Tracer.getTracer(camelContext) == null) {
Tracer tracer = Tracer.createTracer(camelContext);
addInterceptStrategy(tracer);
}
// tracing is added in the DefaultChannel so we can enable it on the fly
}
}

Expand Down

0 comments on commit f47adf7

Please sign in to comment.