CAMEL-7554: Renamed package to include component which is the way we do in Camel.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/acf0fa39 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/acf0fa39 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/acf0fa39 Branch: refs/heads/master Commit: acf0fa39634346a86c7d1f5d0a99f9ef79e94a36 Parents: f52b7a5 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Jul 30 14:17:47 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jul 30 14:17:47 2014 +0200 ---------------------------------------------------------------------- components/camel-metrics/README.md | 2 +- components/camel-metrics/pom.xml | 147 ++++++------ .../metrics/AbstractMetricsEndpoint.java | 53 +++++ .../metrics/AbstractMetricsProducer.java | 72 ++++++ .../component/metrics/MetricsComponent.java | 134 +++++++++++ .../camel/component/metrics/MetricsType.java | 53 +++++ .../metrics/counter/CounterEndpoint.java | 66 ++++++ .../metrics/counter/CounterProducer.java | 51 +++++ .../metrics/histogram/HistogramEndpoint.java | 55 +++++ .../metrics/histogram/HistogramProducer.java | 49 ++++ .../component/metrics/meter/MeterEndpoint.java | 55 +++++ .../component/metrics/meter/MeterProducer.java | 44 ++++ .../component/metrics/timer/TimerEndpoint.java | 59 +++++ .../component/metrics/timer/TimerProducer.java | 85 +++++++ .../camel/metrics/AbstractMetricsEndpoint.java | 53 ----- .../camel/metrics/AbstractMetricsProducer.java | 74 ------ .../apache/camel/metrics/MetricsComponent.java | 134 ----------- .../org/apache/camel/metrics/MetricsType.java | 53 ----- .../camel/metrics/counter/CounterEndpoint.java | 66 ------ .../camel/metrics/counter/CounterProducer.java | 51 ----- .../metrics/histogram/HistogramEndpoint.java | 55 ----- .../metrics/histogram/HistogramProducer.java | 49 ---- .../camel/metrics/meter/MeterEndpoint.java | 55 ----- .../camel/metrics/meter/MeterProducer.java | 44 ---- .../camel/metrics/timer/TimerEndpoint.java | 59 ----- .../camel/metrics/timer/TimerProducer.java | 85 ------- .../services/org/apache/camel/component/metrics | 19 +- .../metrics/AbstractMetricsEndpointTest.java | 105 +++++++++ .../metrics/AbstractMetricsProducerTest.java | 186 +++++++++++++++ .../metrics/MetricComponentSpringTest.java | 91 ++++++++ .../metrics/MetricsComponentRouteTest.java | 113 +++++++++ .../component/metrics/MetricsComponentTest.java | 217 ++++++++++++++++++ .../component/metrics/MetricsTypeTest.java | 34 +++ .../metrics/counter/CounterEndpointTest.java | 102 +++++++++ .../metrics/counter/CounterProducerTest.java | 195 ++++++++++++++++ .../metrics/counter/CounterRouteTest.java | 209 +++++++++++++++++ .../histogram/HistogramEndpointTest.java | 88 +++++++ .../histogram/HistogramProducerTest.java | 125 ++++++++++ .../metrics/histogram/HistogramRouteTest.java | 123 ++++++++++ .../metrics/meter/MeterEndpointTest.java | 89 +++++++ .../metrics/meter/MeterProducerTest.java | 126 ++++++++++ .../component/metrics/meter/MeterRouteTest.java | 165 +++++++++++++ .../metrics/timer/TimerEndpointTest.java | 89 +++++++ .../metrics/timer/TimerProducerTest.java | 229 +++++++++++++++++++ .../component/metrics/timer/TimerRouteTest.java | 144 ++++++++++++ .../metrics/AbstractMetricsEndpointTest.java | 105 --------- .../metrics/AbstractMetricsProducerTest.java | 186 --------------- .../metrics/MetricComponentSpringTest.java | 91 -------- .../metrics/MetricsComponentRouteTest.java | 113 --------- .../camel/metrics/MetricsComponentTest.java | 217 ------------------ .../apache/camel/metrics/MetricsTypeTest.java | 33 --- .../metrics/counter/CounterEndpointTest.java | 102 --------- .../metrics/counter/CounterProducerTest.java | 195 ---------------- .../camel/metrics/counter/CounterRouteTest.java | 209 ----------------- .../histogram/HistogramEndpointTest.java | 88 ------- .../histogram/HistogramProducerTest.java | 125 ---------- .../metrics/histogram/HistogramRouteTest.java | 123 ---------- .../camel/metrics/meter/MeterEndpointTest.java | 89 ------- .../camel/metrics/meter/MeterProducerTest.java | 126 ---------- .../camel/metrics/meter/MeterRouteTest.java | 165 ------------- .../camel/metrics/timer/TimerEndpointTest.java | 89 ------- .../camel/metrics/timer/TimerProducerTest.java | 229 ------------------- .../camel/metrics/timer/TimerRouteTest.java | 144 ------------ .../src/test/resources/log4j.properties | 38 ++- .../features/src/main/resources/features.xml | 5 + .../camel/itest/karaf/CamelMetricsTest.java | 40 ++++ 66 files changed, 3378 insertions(+), 3286 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/README.md ---------------------------------------------------------------------- diff --git a/components/camel-metrics/README.md b/components/camel-metrics/README.md index b0b7bab..1e2b7c6 100644 --- a/components/camel-metrics/README.md +++ b/components/camel-metrics/README.md @@ -260,7 +260,7 @@ Message header can be used to override `action` value specified in Metrics compo | Name | Description | Expected type | |-------------------------|------------------------------|----------------------------------------------------------| -| CamelMetricsTimerAction | Override timer action in URI | org.apache.camel.metrics.timer.TimerEndpoint.TimerAction | +| CamelMetricsTimerAction | Override timer action in URI | org.apache.camel.component.timer.TimerEndpoint.TimerAction | ```java // sets timer action using header http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-metrics/pom.xml b/components/camel-metrics/pom.xml index 940a9b6..58aaf69 100644 --- a/components/camel-metrics/pom.xml +++ b/components/camel-metrics/pom.xml @@ -14,81 +14,86 @@ the specific language governing permissions and limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.camel</groupId> - <artifactId>components</artifactId> - <version>2.14-SNAPSHOT</version> - </parent> + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>2.14-SNAPSHOT</version> + </parent> - <artifactId>camel-metrics</artifactId> - <packaging>bundle</packaging> - <name>Camel :: Metrics</name> - <description>Camel Metrics based monitoring component</description> + <artifactId>camel-metrics</artifactId> + <packaging>bundle</packaging> + <name>Camel :: Metrics</name> + <description>Camel Metrics based monitoring component</description> - <properties> - <camel.osgi.export.pkg> - org.apache.camel.component.metrics.* - </camel.osgi.export.pkg> - <camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=metrics</camel.osgi.export.service> - </properties> + <properties> + <camel.osgi.export.pkg> + org.apache.camel.component.metrics.* + </camel.osgi.export.pkg> + <camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=metrics</camel.osgi.export.service> + </properties> - <dependencies> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-core</artifactId> - </dependency> - <dependency> - <groupId>com.codahale.metrics</groupId> - <artifactId>metrics-core</artifactId> - <version>${metrics-version}</version> - </dependency> + <dependencies> - <!-- testing --> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-test</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-spring</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-spring-javaconfig</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-test-spring3</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> - <version>${hamcrest-version}</version> - <scope>test</scope> - </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + <dependency> + <groupId>com.codahale.metrics</groupId> + <artifactId>metrics-core</artifactId> + <version>${metrics-version}</version> + </dependency> + + <!-- testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-javaconfig</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring3</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-all</artifactId> + <version>${hamcrest-version}</version> + <scope>test</scope> + </dependency> + + <!-- logging --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + + </dependencies> - <!-- logging --> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <scope>test</scope> - </dependency> - </dependencies> </project> http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsEndpoint.java new file mode 100644 index 0000000..594e0c3 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsEndpoint.java @@ -0,0 +1,53 @@ +/** + * 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.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.impl.DefaultEndpoint; + + +public abstract class AbstractMetricsEndpoint extends DefaultEndpoint { + + protected final MetricRegistry registry; + protected final String metricsName; + + public AbstractMetricsEndpoint(MetricRegistry registry, String metricsName) { + this.registry = registry; + this.metricsName = metricsName; + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new RuntimeCamelException("Cannot consume from " + getClass().getSimpleName() + ": " + getEndpointUri()); + } + + @Override + public boolean isSingleton() { + return false; + } + + public MetricRegistry getRegistry() { + return registry; + } + + public String getMetricsName() { + return metricsName; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsProducer.java new file mode 100644 index 0000000..539847a --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/AbstractMetricsProducer.java @@ -0,0 +1,72 @@ +/** + * 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.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public abstract class AbstractMetricsProducer<T extends AbstractMetricsEndpoint> extends DefaultProducer { + + public static final String HEADER_PATTERN = MetricsComponent.HEADER_PERFIX + "*"; + private static final Logger LOG = LoggerFactory.getLogger(AbstractMetricsProducer.class); + + public AbstractMetricsProducer(T endpoint) { + super(endpoint); + } + + @Override + public void process(Exchange exchange) throws Exception { + @SuppressWarnings("unchecked") + T endpoint = (T) getEndpoint(); + Message in = exchange.getIn(); + String defaultMetricsName = endpoint.getMetricsName(); + String finalMetricsName = getMetricsName(in, defaultMetricsName); + MetricRegistry registry = endpoint.getRegistry(); + try { + doProcess(exchange, endpoint, registry, finalMetricsName); + } catch (Exception e) { + LOG.warn("Failed to produce metrics for {} in {} - {}", new Object[] { + finalMetricsName, getClass().getSimpleName(), e.getMessage() }); + } + clearMetricsHeaders(in); + } + + protected abstract void doProcess(Exchange exchange, T endpoint, MetricRegistry registry, String metricsName) throws Exception; + + public String getMetricsName(Message in, String defaultValue) { + return getStringHeader(in, MetricsComponent.HEADER_METRIC_NAME, defaultValue); + } + + public String getStringHeader(Message in, String header, String defaultValue) { + String headerValue = in.getHeader(header, String.class); + return ObjectHelper.isNotEmpty(headerValue) ? headerValue : defaultValue; + } + + public Long getLongHeader(Message in, String header, Long defaultValue) { + return in.getHeader(header, defaultValue, Long.class); + } + + protected boolean clearMetricsHeaders(Message in) { + return in.removeHeaders(HEADER_PATTERN); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java new file mode 100644 index 0000000..bb2813a --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java @@ -0,0 +1,134 @@ +/** + * 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.component.metrics; + +import java.util.Map; +import java.util.concurrent.TimeUnit; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Slf4jReporter; +import org.apache.camel.Endpoint; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.impl.DefaultComponent; +import org.apache.camel.component.metrics.counter.CounterEndpoint; +import org.apache.camel.component.metrics.histogram.HistogramEndpoint; +import org.apache.camel.component.metrics.meter.MeterEndpoint; +import org.apache.camel.component.metrics.timer.TimerEndpoint; +import org.apache.camel.spi.Registry; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Represents the component that manages {@link MetricsEndpoint}. + */ +public class MetricsComponent extends DefaultComponent { + + public static final String METRIC_REGISTRY_NAME = "metricRegistry"; + public static final MetricsType DEFAULT_METRICS_TYPE = MetricsType.METER; + public static final long DEFAULT_REPORTING_INTERVAL_SECONDS = 60L; + public static final String HEADER_PERFIX = "CamelMetrics"; + public static final String HEADER_METRIC_NAME = HEADER_PERFIX + "Name"; + public static final String HEADER_COUNTER_INCREMENT = HEADER_PERFIX + "CounterIncrement"; + public static final String HEADER_COUNTER_DECREMENT = HEADER_PERFIX + "CounterDecrement"; + public static final String HEADER_HISTOGRAM_VALUE = HEADER_PERFIX + "HistogramValue"; + public static final String HEADER_METER_MARK = HEADER_PERFIX + "MeterMark"; + public static final String HEADER_TIMER_ACTION = HEADER_PERFIX + "TimerAction"; + + private static final Logger LOG = LoggerFactory.getLogger(MetricsComponent.class); + + private MetricRegistry metricRegistry; + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + if (metricRegistry == null) { + Registry camelRegistry = getCamelContext().getRegistry(); + metricRegistry = getOrCreateMetricRegistry(camelRegistry, METRIC_REGISTRY_NAME); + } + String metricsName = getMetricsName(remaining); + MetricsType metricsType = getMetricsType(remaining); + LOG.info("Metrics type: {}; name: {}", metricsType, metricsName); + Endpoint endpoint = createNewEndpoint(metricRegistry, metricsType, metricsName); + setProperties(endpoint, parameters); + return endpoint; + } + + String getMetricsName(String remaining) { + String name = ObjectHelper.after(remaining, ":"); + return name == null ? remaining : name; + } + + Endpoint createNewEndpoint(MetricRegistry registry, MetricsType type, String metricsName) { + Endpoint endpoint; + switch (type) { + case COUNTER: + endpoint = new CounterEndpoint(registry, metricsName); + break; + case METER: + endpoint = new MeterEndpoint(registry, metricsName); + break; + case HISTOGRAM: + endpoint = new HistogramEndpoint(registry, metricsName); + break; + case TIMER: + endpoint = new TimerEndpoint(registry, metricsName); + break; + default: + throw new RuntimeCamelException("Metrics type \"" + type.toString() + "\" not supported"); + } + return endpoint; + } + + MetricsType getMetricsType(String remaining) { + String name = ObjectHelper.before(remaining, ":"); + MetricsType type; + if (name == null) { + type = DEFAULT_METRICS_TYPE; + } else { + type = MetricsType.getByName(name); + } + if (type == null) { + throw new RuntimeCamelException("Unknown metrics type \"" + name + "\""); + } + return type; + } + + MetricRegistry getOrCreateMetricRegistry(Registry camelRegistry, String registryName) { + LOG.debug("Looking up MetricRegistry from Camel Registry for name \"{}\"", registryName); + MetricRegistry result = getMetricRegistryFromCamelRegistry(camelRegistry, registryName); + if (result == null) { + LOG.debug("MetricRegistry not found from Camel Registry for name \"{}\"", registryName); + LOG.info("Creating new default MetricRegistry"); + result = createMetricRegistry(); + } + return result; + } + + MetricRegistry getMetricRegistryFromCamelRegistry(Registry camelRegistry, String registryName) { + return camelRegistry.lookupByNameAndType(registryName, MetricRegistry.class); + } + + MetricRegistry createMetricRegistry() { + MetricRegistry registry = new MetricRegistry(); + final Slf4jReporter reporter = Slf4jReporter.forRegistry(registry) + .outputTo(LOG) + .convertRatesTo(TimeUnit.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS) + .build(); + reporter.start(DEFAULT_REPORTING_INTERVAL_SECONDS, TimeUnit.SECONDS); + return registry; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsType.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsType.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsType.java new file mode 100644 index 0000000..3348578 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsType.java @@ -0,0 +1,53 @@ +/** + * 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.component.metrics; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +public enum MetricsType { + + GAUGE("gauge"), + COUNTER("counter"), + HISTOGRAM("histogram"), + METER("meter"), + TIMER("timer"); + + private static final Map<String, MetricsType> MAP = new HashMap<String, MetricsType>(); + + private final String name; + + static { + for (MetricsType type : EnumSet.allOf(MetricsType.class)) { + MAP.put(type.name, type); + } + } + + private MetricsType(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + + public static MetricsType getByName(String name) { + return MAP.get(name); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterEndpoint.java new file mode 100644 index 0000000..2eae8f2 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterEndpoint.java @@ -0,0 +1,66 @@ +/** + * 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.component.metrics.counter; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.apache.camel.component.metrics.AbstractMetricsEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; + + +@UriEndpoint(scheme = "metrics") +public class CounterEndpoint extends AbstractMetricsEndpoint { + + public static final String ENDPOINT_URI = "metrics:counter"; + + @UriParam + private Long increment; + + @UriParam + private Long decrement; + + public CounterEndpoint(MetricRegistry registry, String metricsName) { + super(registry, metricsName); + } + + @Override + public Producer createProducer() throws Exception { + return new CounterProducer(this); + } + + public Long getIncrement() { + return increment; + } + + public void setIncrement(Long increment) { + this.increment = increment; + } + + public Long getDecrement() { + return decrement; + } + + public void setDecrement(Long decrement) { + this.decrement = decrement; + } + + @Override + protected String createEndpointUri() { + return ENDPOINT_URI; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterProducer.java new file mode 100644 index 0000000..5a9dd42 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/counter/CounterProducer.java @@ -0,0 +1,51 @@ +/** + * 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.component.metrics.counter; + + +import com.codahale.metrics.Counter; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.metrics.AbstractMetricsProducer; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_COUNTER_DECREMENT; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_COUNTER_INCREMENT; + + +public class CounterProducer extends AbstractMetricsProducer<CounterEndpoint> { + + public CounterProducer(CounterEndpoint endpoint) { + super(endpoint); + } + + @Override + protected void doProcess(Exchange exchange, CounterEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { + Message in = exchange.getIn(); + Counter counter = registry.counter(metricsName); + Long increment = endpoint.getIncrement(); + Long decrement = endpoint.getDecrement(); + Long finalIncrement = getLongHeader(in, HEADER_COUNTER_INCREMENT, increment); + Long finalDecrement = getLongHeader(in, HEADER_COUNTER_DECREMENT, decrement); + if (finalIncrement != null) { + counter.inc(finalIncrement); + } else if (finalDecrement != null) { + counter.dec(finalDecrement); + } else { + counter.inc(); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramEndpoint.java new file mode 100644 index 0000000..bf7ce6c --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramEndpoint.java @@ -0,0 +1,55 @@ +/** + * 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.component.metrics.histogram; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.apache.camel.component.metrics.AbstractMetricsEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; + + +@UriEndpoint(scheme = "metrics") +public class HistogramEndpoint extends AbstractMetricsEndpoint { + + public static final String ENDPOINT_URI = "metrics:histogram"; + + @UriParam + private Long value; + + public HistogramEndpoint(MetricRegistry registry, String metricsName) { + super(registry, metricsName); + } + + @Override + public Producer createProducer() throws Exception { + return new HistogramProducer(this); + } + + public Long getValue() { + return value; + } + + public void setValue(Long value) { + this.value = value; + } + + @Override + protected String createEndpointUri() { + return ENDPOINT_URI; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramProducer.java new file mode 100644 index 0000000..161da92 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/histogram/HistogramProducer.java @@ -0,0 +1,49 @@ +/** + * 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.component.metrics.histogram; + + +import com.codahale.metrics.Histogram; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.metrics.AbstractMetricsProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; + +public class HistogramProducer extends AbstractMetricsProducer<HistogramEndpoint> { + + private static final Logger LOG = LoggerFactory.getLogger(HistogramProducer.class); + + public HistogramProducer(HistogramEndpoint endpoint) { + super(endpoint); + } + + @Override + protected void doProcess(Exchange exchange, HistogramEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { + Message in = exchange.getIn(); + Histogram histogram = registry.histogram(metricsName); + Long value = endpoint.getValue(); + Long finalValue = getLongHeader(in, HEADER_HISTOGRAM_VALUE, value); + if (finalValue != null) { + histogram.update(finalValue); + } else { + LOG.warn("Cannot update histogram \"{}\" with null value", metricsName); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterEndpoint.java new file mode 100644 index 0000000..f376261 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterEndpoint.java @@ -0,0 +1,55 @@ +/** + * 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.component.metrics.meter; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.apache.camel.component.metrics.AbstractMetricsEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; + + +@UriEndpoint(scheme = "metrics") +public class MeterEndpoint extends AbstractMetricsEndpoint { + + public static final String ENDPOINT_URI = "metrics:meter"; + + @UriParam + private Long mark; + + public MeterEndpoint(MetricRegistry registry, String metricsName) { + super(registry, metricsName); + } + + @Override + public Producer createProducer() throws Exception { + return new MeterProducer(this); + } + + public Long getMark() { + return mark; + } + + public void setMark(Long mark) { + this.mark = mark; + } + + @Override + protected String createEndpointUri() { + return ENDPOINT_URI; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterProducer.java new file mode 100644 index 0000000..b3474bb --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/meter/MeterProducer.java @@ -0,0 +1,44 @@ +/** + * 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.component.metrics.meter; + +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.metrics.AbstractMetricsProducer; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_METER_MARK; + +public class MeterProducer extends AbstractMetricsProducer<MeterEndpoint> { + + public MeterProducer(MeterEndpoint endpoint) { + super(endpoint); + } + + @Override + protected void doProcess(Exchange exchange, MeterEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { + Message in = exchange.getIn(); + Meter meter = registry.meter(metricsName); + Long mark = endpoint.getMark(); + Long finalMark = getLongHeader(in, HEADER_METER_MARK, mark); + if (finalMark == null) { + meter.mark(); + } else { + meter.mark(finalMark); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerEndpoint.java new file mode 100644 index 0000000..f1591a0 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerEndpoint.java @@ -0,0 +1,59 @@ +/** + * 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.component.metrics.timer; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.apache.camel.component.metrics.AbstractMetricsEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; + +@UriEndpoint(scheme = "metrics") +public class TimerEndpoint extends AbstractMetricsEndpoint { + + public static final String ENDPOINT_URI = "metrics:timer"; + + public enum TimerAction { + start, + stop; + } + + @UriParam + private TimerAction action; + + public TimerEndpoint(MetricRegistry registry, String metricsName) { + super(registry, metricsName); + } + + @Override + public Producer createProducer() throws Exception { + return new TimerProducer(this); + } + + public TimerAction getAction() { + return action; + } + + public void setAction(TimerAction action) { + this.action = action; + } + + @Override + protected String createEndpointUri() { + return ENDPOINT_URI; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerProducer.java new file mode 100644 index 0000000..57cf471 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/timer/TimerProducer.java @@ -0,0 +1,85 @@ +/** + * 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.component.metrics.timer; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.metrics.AbstractMetricsProducer; +import org.apache.camel.component.metrics.timer.TimerEndpoint.TimerAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_TIMER_ACTION; +import static org.apache.camel.component.metrics.timer.TimerEndpoint.ENDPOINT_URI; + +public class TimerProducer extends AbstractMetricsProducer<TimerEndpoint> { + + private static final Logger LOG = LoggerFactory.getLogger(TimerProducer.class); + + public TimerProducer(TimerEndpoint endpoint) { + super(endpoint); + } + + @Override + protected void doProcess(Exchange exchange, TimerEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { + Message in = exchange.getIn(); + TimerAction action = endpoint.getAction(); + TimerAction finalAction = in.getHeader(HEADER_TIMER_ACTION, action, TimerAction.class); + if (finalAction == TimerAction.start) { + handleStart(exchange, registry, metricsName); + } else if (finalAction == TimerAction.stop) { + handleStop(exchange, registry, metricsName); + } else { + LOG.warn("No action provided for timer \"{}\"", metricsName); + } + } + + void handleStart(Exchange exchange, MetricRegistry registry, String metricsName) { + String propertyName = getPropertyName(metricsName); + Timer.Context context = getTimerContextFromExchange(exchange, propertyName); + if (context == null) { + Timer timer = registry.timer(metricsName); + context = timer.time(); + exchange.setProperty(propertyName, context); + } else { + LOG.warn("Timer \"{}\" already running", metricsName); + } + } + + void handleStop(Exchange exchange, MetricRegistry registry, String metricsName) { + String propertyName = getPropertyName(metricsName); + Timer.Context context = getTimerContextFromExchange(exchange, propertyName); + if (context != null) { + context.stop(); + exchange.removeProperty(propertyName); + } else { + LOG.warn("Timer \"{}\" not found", metricsName); + } + } + + String getPropertyName(String metricsName) { + return new StringBuilder(ENDPOINT_URI) + .append(":") + .append(metricsName) + .toString(); + } + + Timer.Context getTimerContextFromExchange(Exchange exchange, String propertyName) { + return exchange.getProperty(propertyName, Timer.Context.class); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java deleted file mode 100644 index f437846..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsEndpoint.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * 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.metrics; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Consumer; -import org.apache.camel.Processor; -import org.apache.camel.RuntimeCamelException; -import org.apache.camel.impl.DefaultEndpoint; - - -public abstract class AbstractMetricsEndpoint extends DefaultEndpoint { - - protected final MetricRegistry registry; - protected final String metricsName; - - public AbstractMetricsEndpoint(MetricRegistry registry, String metricsName) { - this.registry = registry; - this.metricsName = metricsName; - } - - @Override - public Consumer createConsumer(Processor processor) throws Exception { - throw new RuntimeCamelException("Cannot consume from " + getClass().getSimpleName() + ": " + getEndpointUri()); - } - - @Override - public boolean isSingleton() { - return false; - } - - public MetricRegistry getRegistry() { - return registry; - } - - public String getMetricsName() { - return metricsName; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsProducer.java deleted file mode 100644 index cd11447..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/AbstractMetricsProducer.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * 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.metrics; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.impl.DefaultProducer; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; -import static org.apache.camel.metrics.MetricsComponent.HEADER_PERFIX; - - -public abstract class AbstractMetricsProducer<T extends AbstractMetricsEndpoint> extends DefaultProducer { - - public static final String HEADER_PATTERN = HEADER_PERFIX + "*"; - private static final Logger LOG = LoggerFactory.getLogger(AbstractMetricsProducer.class); - - public AbstractMetricsProducer(T endpoint) { - super(endpoint); - } - - @Override - public void process(Exchange exchange) throws Exception { - @SuppressWarnings("unchecked") - T endpoint = (T) getEndpoint(); - Message in = exchange.getIn(); - String defaultMetricsName = endpoint.getMetricsName(); - String finalMetricsName = getMetricsName(in, defaultMetricsName); - MetricRegistry registry = endpoint.getRegistry(); - try { - doProcess(exchange, endpoint, registry, finalMetricsName); - } catch (Exception e) { - LOG.warn("Failed to produce metrics for {} in {} - {}", new Object[] { - finalMetricsName, getClass().getSimpleName(), e.getMessage() }); - } - clearMetricsHeaders(in); - } - - protected abstract void doProcess(Exchange exchange, T endpoint, MetricRegistry registry, String metricsName) throws Exception; - - public String getMetricsName(Message in, String defaultValue) { - return getStringHeader(in, HEADER_METRIC_NAME, defaultValue); - } - - public String getStringHeader(Message in, String header, String defaultValue) { - String headerValue = in.getHeader(header, String.class); - return ObjectHelper.isNotEmpty(headerValue) ? headerValue : defaultValue; - } - - public Long getLongHeader(Message in, String header, Long defaultValue) { - return in.getHeader(header, defaultValue, Long.class); - } - - protected boolean clearMetricsHeaders(Message in) { - return in.removeHeaders(HEADER_PATTERN); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsComponent.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsComponent.java deleted file mode 100644 index b73626a..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsComponent.java +++ /dev/null @@ -1,134 +0,0 @@ -/** - * 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.metrics; - -import java.util.Map; -import java.util.concurrent.TimeUnit; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.Slf4jReporter; -import org.apache.camel.Endpoint; -import org.apache.camel.RuntimeCamelException; -import org.apache.camel.impl.DefaultComponent; -import org.apache.camel.metrics.counter.CounterEndpoint; -import org.apache.camel.metrics.histogram.HistogramEndpoint; -import org.apache.camel.metrics.meter.MeterEndpoint; -import org.apache.camel.metrics.timer.TimerEndpoint; -import org.apache.camel.spi.Registry; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Represents the component that manages {@link MetricsEndpoint}. - */ -public class MetricsComponent extends DefaultComponent { - - public static final String METRIC_REGISTRY_NAME = "metricRegistry"; - public static final MetricsType DEFAULT_METRICS_TYPE = MetricsType.METER; - public static final long DEFAULT_REPORTING_INTERVAL_SECONDS = 60L; - public static final String HEADER_PERFIX = "CamelMetrics"; - public static final String HEADER_METRIC_NAME = HEADER_PERFIX + "Name"; - public static final String HEADER_COUNTER_INCREMENT = HEADER_PERFIX + "CounterIncrement"; - public static final String HEADER_COUNTER_DECREMENT = HEADER_PERFIX + "CounterDecrement"; - public static final String HEADER_HISTOGRAM_VALUE = HEADER_PERFIX + "HistogramValue"; - public static final String HEADER_METER_MARK = HEADER_PERFIX + "MeterMark"; - public static final String HEADER_TIMER_ACTION = HEADER_PERFIX + "TimerAction"; - - private static final Logger LOG = LoggerFactory.getLogger(MetricsComponent.class); - - private MetricRegistry metricRegistry; - - @Override - protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - if (metricRegistry == null) { - Registry camelRegistry = getCamelContext().getRegistry(); - metricRegistry = getOrCreateMetricRegistry(camelRegistry, METRIC_REGISTRY_NAME); - } - String metricsName = getMetricsName(remaining); - MetricsType metricsType = getMetricsType(remaining); - LOG.info("Metrics type: {}; name: {}", metricsType, metricsName); - Endpoint endpoint = createNewEndpoint(metricRegistry, metricsType, metricsName); - setProperties(endpoint, parameters); - return endpoint; - } - - String getMetricsName(String remaining) { - String name = ObjectHelper.after(remaining, ":"); - return name == null ? remaining : name; - } - - Endpoint createNewEndpoint(MetricRegistry registry, MetricsType type, String metricsName) { - Endpoint endpoint; - switch (type) { - case COUNTER: - endpoint = new CounterEndpoint(registry, metricsName); - break; - case METER: - endpoint = new MeterEndpoint(registry, metricsName); - break; - case HISTOGRAM: - endpoint = new HistogramEndpoint(registry, metricsName); - break; - case TIMER: - endpoint = new TimerEndpoint(registry, metricsName); - break; - default: - throw new RuntimeCamelException("Metrics type \"" + type.toString() + "\" not supported"); - } - return endpoint; - } - - MetricsType getMetricsType(String remaining) { - String name = ObjectHelper.before(remaining, ":"); - MetricsType type; - if (name == null) { - type = DEFAULT_METRICS_TYPE; - } else { - type = MetricsType.getByName(name); - } - if (type == null) { - throw new RuntimeCamelException("Unknown metrics type \"" + name + "\""); - } - return type; - } - - MetricRegistry getOrCreateMetricRegistry(Registry camelRegistry, String registryName) { - LOG.debug("Looking up MetricRegistry from Camel Registry for name \"{}\"", registryName); - MetricRegistry result = getMetricRegistryFromCamelRegistry(camelRegistry, registryName); - if (result == null) { - LOG.debug("MetricRegistry not found from Camel Registry for name \"{}\"", registryName); - LOG.info("Creating new default MetricRegistry"); - result = createMetricRegistry(); - } - return result; - } - - MetricRegistry getMetricRegistryFromCamelRegistry(Registry camelRegistry, String registryName) { - return camelRegistry.lookupByNameAndType(registryName, MetricRegistry.class); - } - - MetricRegistry createMetricRegistry() { - MetricRegistry registry = new MetricRegistry(); - final Slf4jReporter reporter = Slf4jReporter.forRegistry(registry) - .outputTo(LOG) - .convertRatesTo(TimeUnit.SECONDS) - .convertDurationsTo(TimeUnit.MILLISECONDS) - .build(); - reporter.start(DEFAULT_REPORTING_INTERVAL_SECONDS, TimeUnit.SECONDS); - return registry; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsType.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsType.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsType.java deleted file mode 100644 index 760c794..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/MetricsType.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * 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.metrics; - -import java.util.EnumSet; -import java.util.HashMap; -import java.util.Map; - -public enum MetricsType { - - GAUGE("gauge"), - COUNTER("counter"), - HISTOGRAM("histogram"), - METER("meter"), - TIMER("timer"); - - private static final Map<String, MetricsType> MAP = new HashMap<String, MetricsType>(); - - private final String name; - - static { - for (MetricsType type : EnumSet.allOf(MetricsType.class)) { - MAP.put(type.name, type); - } - } - - private MetricsType(String name) { - this.name = name; - } - - @Override - public String toString() { - return name; - } - - public static MetricsType getByName(String name) { - return MAP.get(name); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterEndpoint.java deleted file mode 100644 index 406c21d..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterEndpoint.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * 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.metrics.counter; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Producer; -import org.apache.camel.metrics.AbstractMetricsEndpoint; -import org.apache.camel.spi.UriEndpoint; -import org.apache.camel.spi.UriParam; - - -@UriEndpoint(scheme = "metrics") -public class CounterEndpoint extends AbstractMetricsEndpoint { - - public static final String ENDPOINT_URI = "metrics:counter"; - - @UriParam - private Long increment; - - @UriParam - private Long decrement; - - public CounterEndpoint(MetricRegistry registry, String metricsName) { - super(registry, metricsName); - } - - @Override - public Producer createProducer() throws Exception { - return new CounterProducer(this); - } - - public Long getIncrement() { - return increment; - } - - public void setIncrement(Long increment) { - this.increment = increment; - } - - public Long getDecrement() { - return decrement; - } - - public void setDecrement(Long decrement) { - this.decrement = decrement; - } - - @Override - protected String createEndpointUri() { - return ENDPOINT_URI; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java deleted file mode 100644 index bdabba3..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/counter/CounterProducer.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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.metrics.counter; - - -import com.codahale.metrics.Counter; -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.metrics.AbstractMetricsProducer; -import static org.apache.camel.metrics.MetricsComponent.HEADER_COUNTER_DECREMENT; -import static org.apache.camel.metrics.MetricsComponent.HEADER_COUNTER_INCREMENT; - - -public class CounterProducer extends AbstractMetricsProducer<CounterEndpoint> { - - public CounterProducer(CounterEndpoint endpoint) { - super(endpoint); - } - - @Override - protected void doProcess(Exchange exchange, CounterEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { - Message in = exchange.getIn(); - Counter counter = registry.counter(metricsName); - Long increment = endpoint.getIncrement(); - Long decrement = endpoint.getDecrement(); - Long finalIncrement = getLongHeader(in, HEADER_COUNTER_INCREMENT, increment); - Long finalDecrement = getLongHeader(in, HEADER_COUNTER_DECREMENT, decrement); - if (finalIncrement != null) { - counter.inc(finalIncrement); - } else if (finalDecrement != null) { - counter.dec(finalDecrement); - } else { - counter.inc(); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramEndpoint.java deleted file mode 100644 index fad53c0..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramEndpoint.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.metrics.histogram; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Producer; -import org.apache.camel.metrics.AbstractMetricsEndpoint; -import org.apache.camel.spi.UriEndpoint; -import org.apache.camel.spi.UriParam; - - -@UriEndpoint(scheme = "metrics") -public class HistogramEndpoint extends AbstractMetricsEndpoint { - - public static final String ENDPOINT_URI = "metrics:histogram"; - - @UriParam - private Long value; - - public HistogramEndpoint(MetricRegistry registry, String metricsName) { - super(registry, metricsName); - } - - @Override - public Producer createProducer() throws Exception { - return new HistogramProducer(this); - } - - public Long getValue() { - return value; - } - - public void setValue(Long value) { - this.value = value; - } - - @Override - protected String createEndpointUri() { - return ENDPOINT_URI; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java deleted file mode 100644 index b29505a..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * 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.metrics.histogram; - - -import com.codahale.metrics.Histogram; -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.metrics.AbstractMetricsProducer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; - -public class HistogramProducer extends AbstractMetricsProducer<HistogramEndpoint> { - - private static final Logger LOG = LoggerFactory.getLogger(HistogramProducer.class); - - public HistogramProducer(HistogramEndpoint endpoint) { - super(endpoint); - } - - @Override - protected void doProcess(Exchange exchange, HistogramEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { - Message in = exchange.getIn(); - Histogram histogram = registry.histogram(metricsName); - Long value = endpoint.getValue(); - Long finalValue = getLongHeader(in, HEADER_HISTOGRAM_VALUE, value); - if (finalValue != null) { - histogram.update(finalValue); - } else { - LOG.warn("Cannot update histogram \"{}\" with null value", metricsName); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterEndpoint.java deleted file mode 100644 index 84c6316..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterEndpoint.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.metrics.meter; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Producer; -import org.apache.camel.metrics.AbstractMetricsEndpoint; -import org.apache.camel.spi.UriEndpoint; -import org.apache.camel.spi.UriParam; - - -@UriEndpoint(scheme = "metrics") -public class MeterEndpoint extends AbstractMetricsEndpoint { - - public static final String ENDPOINT_URI = "metrics:meter"; - - @UriParam - private Long mark; - - public MeterEndpoint(MetricRegistry registry, String metricsName) { - super(registry, metricsName); - } - - @Override - public Producer createProducer() throws Exception { - return new MeterProducer(this); - } - - public Long getMark() { - return mark; - } - - public void setMark(Long mark) { - this.mark = mark; - } - - @Override - protected String createEndpointUri() { - return ENDPOINT_URI; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java deleted file mode 100644 index 686b198..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/meter/MeterProducer.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 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.metrics.meter; - -import com.codahale.metrics.Meter; -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.metrics.AbstractMetricsProducer; -import static org.apache.camel.metrics.MetricsComponent.HEADER_METER_MARK; - -public class MeterProducer extends AbstractMetricsProducer<MeterEndpoint> { - - public MeterProducer(MeterEndpoint endpoint) { - super(endpoint); - } - - @Override - protected void doProcess(Exchange exchange, MeterEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { - Message in = exchange.getIn(); - Meter meter = registry.meter(metricsName); - Long mark = endpoint.getMark(); - Long finalMark = getLongHeader(in, HEADER_METER_MARK, mark); - if (finalMark == null) { - meter.mark(); - } else { - meter.mark(finalMark); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerEndpoint.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerEndpoint.java deleted file mode 100644 index 10bc25f..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerEndpoint.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * 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.metrics.timer; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Producer; -import org.apache.camel.metrics.AbstractMetricsEndpoint; -import org.apache.camel.spi.UriEndpoint; -import org.apache.camel.spi.UriParam; - -@UriEndpoint(scheme = "metrics") -public class TimerEndpoint extends AbstractMetricsEndpoint { - - public static final String ENDPOINT_URI = "metrics:timer"; - - public enum TimerAction { - start, - stop; - } - - @UriParam - private TimerAction action; - - public TimerEndpoint(MetricRegistry registry, String metricsName) { - super(registry, metricsName); - } - - @Override - public Producer createProducer() throws Exception { - return new TimerProducer(this); - } - - public TimerAction getAction() { - return action; - } - - public void setAction(TimerAction action) { - this.action = action; - } - - @Override - protected String createEndpointUri() { - return ENDPOINT_URI; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java b/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java deleted file mode 100644 index d12726e..0000000 --- a/components/camel-metrics/src/main/java/org/apache/camel/metrics/timer/TimerProducer.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * 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.metrics.timer; - -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.Timer; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.metrics.AbstractMetricsProducer; -import org.apache.camel.metrics.timer.TimerEndpoint.TimerAction; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.apache.camel.metrics.MetricsComponent.HEADER_TIMER_ACTION; -import static org.apache.camel.metrics.timer.TimerEndpoint.ENDPOINT_URI; - -public class TimerProducer extends AbstractMetricsProducer<TimerEndpoint> { - - private static final Logger LOG = LoggerFactory.getLogger(TimerProducer.class); - - public TimerProducer(TimerEndpoint endpoint) { - super(endpoint); - } - - @Override - protected void doProcess(Exchange exchange, TimerEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { - Message in = exchange.getIn(); - TimerAction action = endpoint.getAction(); - TimerAction finalAction = in.getHeader(HEADER_TIMER_ACTION, action, TimerAction.class); - if (finalAction == TimerAction.start) { - handleStart(exchange, registry, metricsName); - } else if (finalAction == TimerAction.stop) { - handleStop(exchange, registry, metricsName); - } else { - LOG.warn("No action provided for timer \"{}\"", metricsName); - } - } - - void handleStart(Exchange exchange, MetricRegistry registry, String metricsName) { - String propertyName = getPropertyName(metricsName); - Timer.Context context = getTimerContextFromExchange(exchange, propertyName); - if (context == null) { - Timer timer = registry.timer(metricsName); - context = timer.time(); - exchange.setProperty(propertyName, context); - } else { - LOG.warn("Timer \"{}\" already running", metricsName); - } - } - - void handleStop(Exchange exchange, MetricRegistry registry, String metricsName) { - String propertyName = getPropertyName(metricsName); - Timer.Context context = getTimerContextFromExchange(exchange, propertyName); - if (context != null) { - context.stop(); - exchange.removeProperty(propertyName); - } else { - LOG.warn("Timer \"{}\" not found", metricsName); - } - } - - String getPropertyName(String metricsName) { - return new StringBuilder(ENDPOINT_URI) - .append(":") - .append(metricsName) - .toString(); - } - - Timer.Context getTimerContextFromExchange(Exchange exchange, String propertyName) { - return exchange.getProperty(propertyName, Timer.Context.class); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/main/resources/META-INF/services/org/apache/camel/component/metrics ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/resources/META-INF/services/org/apache/camel/component/metrics b/components/camel-metrics/src/main/resources/META-INF/services/org/apache/camel/component/metrics index debbe31..6701de5 100644 --- a/components/camel-metrics/src/main/resources/META-INF/services/org/apache/camel/component/metrics +++ b/components/camel-metrics/src/main/resources/META-INF/services/org/apache/camel/component/metrics @@ -1 +1,18 @@ -class=org.apache.camel.metrics.MetricsComponent +# +# 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. +# + +class=org.apache.camel.component.metrics.MetricsComponent