This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 924378e018a7b458754a8f18e9a9484d39cb4007 Author: Pasquale Congiusti <[email protected]> AuthorDate: Tue Apr 13 14:22:32 2021 +0200 doc(example): error handling kamelet binding --- ...ror-handler.yaml => error-handler.kamelet.yaml} | 38 ++++++++------- .../kamelets/error-handler/error-sink.kamelet.yaml | 44 ----------------- ...ler.yaml => incremental-id-source.kamelet.yaml} | 47 +++++++++++------- .../kamelet-binding-error-handler.yaml | 17 ++++--- ...ng-error-handler.yaml => log-sink.kamelet.yaml} | 30 +++++------- examples/kamelets/error-handler/readme.md | 57 ++++++++++++++-------- .../error-handler/timer-source.kamelet.yaml | 54 -------------------- 7 files changed, 107 insertions(+), 180 deletions(-) diff --git a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml b/examples/kamelets/error-handler/error-handler.kamelet.yaml similarity index 56% copy from examples/kamelets/error-handler/kamelet-binding-error-handler.yaml copy to examples/kamelets/error-handler/error-handler.kamelet.yaml index d731d1f..dbfc4bb 100644 --- a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml +++ b/examples/kamelets/error-handler/error-handler.kamelet.yaml @@ -16,23 +16,25 @@ # --------------------------------------------------------------------------- apiVersion: camel.apache.org/v1alpha1 -kind: KameletBinding +kind: Kamelet metadata: - name: http-error-source + name: error-handler spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1alpha1 - name: timer-source - properties: - message: "Hello world!" - sink: - uri: https://log-source/failing-service - errorHandler: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1alpha1 - name: error-sink - properties: - defaultMessage: "ERROR ERROR!" + sources: + - content: | + import org.apache.camel.builder.RouteBuilder; + public class ErrorHandlerSource extends RouteBuilder { + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("log:error?showCaughtException=true&showException=true&showHeaders=true")); + } + } + name: ErrorHandlerSource.java + definition: + title: "Error Handler Log DLC" + description: "Dead letter channel" +# We can use the below once this is fixed https://issues.apache.org/jira/browse/CAMEL-16486 +# flow: +# error-handler: +# dead-letter-channel: +# dead-letter-uri: log:error-sink?showCaughtException=true&showException=true&showHeaders=true diff --git a/examples/kamelets/error-handler/error-sink.kamelet.yaml b/examples/kamelets/error-handler/error-sink.kamelet.yaml deleted file mode 100644 index e35a54d..0000000 --- a/examples/kamelets/error-handler/error-sink.kamelet.yaml +++ /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. -# --------------------------------------------------------------------------- - -apiVersion: camel.apache.org/v1alpha1 -kind: Kamelet -metadata: - name: error-sink - annotations: - camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEwAACxMBAJqcGAAACcBJREFUeJzt3XmsHVUdwPHva21poWGTUpAiVRQSFjWAyi5LcCmKLCEouLwQccMgGsISEkQRXMIfSKMGhQSMCCibkRIgghQEF4RIQYtG4YFsraRlb6G+1j9+94a50/fmvuW+OTN3vp9kkrnnzj3zu+/N786ZmTNnQJIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZIkSZKkfjOQOoAKGQA+DJwAvBfYCpieNKKJexk4E/h56kDUH94O3AOs76NpHfDlXv6R1Ey7ACtIv0FP1XRa7/5Uapo5wL9JvxFP9fSNXv3Bmu [...] - labels: - camel.apache.org/kamelet.type: "sink" -spec: - definition: - title: "Error Log Sink" - description: "Consume events from a channel" - required: - - defaultMessage - properties: - defaultMessage: - title: Message - description: The default message to log - type: string - example: "error while checking the source" - flow: - from: - uri: kamelet:source - steps: - - set-body: - constant: "{{defaultMessage}}" - - to: "log:error-sink" diff --git a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml b/examples/kamelets/error-handler/incremental-id-source.kamelet.yaml similarity index 55% copy from examples/kamelets/error-handler/kamelet-binding-error-handler.yaml copy to examples/kamelets/error-handler/incremental-id-source.kamelet.yaml index d731d1f..5e3181b 100644 --- a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml +++ b/examples/kamelets/error-handler/incremental-id-source.kamelet.yaml @@ -16,23 +16,36 @@ # --------------------------------------------------------------------------- apiVersion: camel.apache.org/v1alpha1 -kind: KameletBinding +kind: Kamelet metadata: - name: http-error-source + name: incremental-id-source + labels: + camel.apache.org/kamelet.type: "source" spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1alpha1 - name: timer-source + definition: + title: "Incremental ID Source" + description: "Produces periodic events with an incremental ID" properties: - message: "Hello world!" - sink: - uri: https://log-source/failing-service - errorHandler: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1alpha1 - name: error-sink - properties: - defaultMessage: "ERROR ERROR!" + period: + title: Period + description: The interval between two events + type: integer + default: 1000 + types: + out: + mediaType: text/plain + flow: + from: + uri: timer:tick + parameters: + period: "{{period}}" + steps: + - set-body: + simple: "Producing message #${exchangeProperty.CamelTimerCounter}" + - choice: + when: + - simple: "${bodyAs(String)} contains 0" + steps: + - set-body: + simple: "${mandatoryBodyAs(Boolean)}" + - to: "kamelet:sink" diff --git a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml b/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml index d731d1f..3602831 100644 --- a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml +++ b/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml @@ -18,21 +18,22 @@ apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: - name: http-error-source + name: kamelet-binding-error-handler spec: source: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 - name: timer-source - properties: - message: "Hello world!" + name: incremental-id-source sink: - uri: https://log-source/failing-service + ref: + kind: Kamelet + apiVersion: camel.apache.org/v1alpha1 + name: log-sink errorHandler: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 - name: error-sink - properties: - defaultMessage: "ERROR ERROR!" + name: error-handler +# uri: kamelet:error-handler + diff --git a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml b/examples/kamelets/error-handler/log-sink.kamelet.yaml similarity index 70% copy from examples/kamelets/error-handler/kamelet-binding-error-handler.yaml copy to examples/kamelets/error-handler/log-sink.kamelet.yaml index d731d1f..aa11481 100644 --- a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml +++ b/examples/kamelets/error-handler/log-sink.kamelet.yaml @@ -16,23 +16,17 @@ # --------------------------------------------------------------------------- apiVersion: camel.apache.org/v1alpha1 -kind: KameletBinding +kind: Kamelet metadata: - name: http-error-source + name: log-sink + labels: + camel.apache.org/kamelet.type: "sink" spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1alpha1 - name: timer-source - properties: - message: "Hello world!" - sink: - uri: https://log-source/failing-service - errorHandler: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1alpha1 - name: error-sink - properties: - defaultMessage: "ERROR ERROR!" + definition: + title: "Regular Log Sink" + description: "Consume events from a channel" + flow: + from: + uri: kamelet:source + steps: + - to: "log:ok" diff --git a/examples/kamelets/error-handler/readme.md b/examples/kamelets/error-handler/readme.md index 5438050..5ff4777 100644 --- a/examples/kamelets/error-handler/readme.md +++ b/examples/kamelets/error-handler/readme.md @@ -1,49 +1,64 @@ # Kamelets Binding Error Handler example -This example shows how to create a simple _timer-source_ `kamelet` bound to a failing sink in a `KameletBinding`. With the support of the `ErrorHandler` we will be able to redirect all errors to a `Dead Letter Channel` _error-sink_ `Kamelet`. +This example shows how to create a simple _source_ `kamelet` bound to a log _sink_ in a `KameletBinding`. With the support of the `ErrorHandler` we will be able to redirect all errors to a `Dead Letter Channel` _error-handler_ `Kamelet`. -## Timer Source Kamelet -First of all, you must install the _timer-source_ Kamelet defined in `timer-source.kamelet.yaml` file: +## Incremental ID Source Kamelet +First of all, you must install the _incremental-id-source_ Kamelet defined in `incremental-id-source.kamelet.yaml` file. This source will emit events every second with an autoincrement counter that will be forced to fail when the number 0 is caught. With this trick, we will simulate possible event faults. ``` -$ kubectl apply -f timer-source.kamelet.yaml +$ kubectl apply -f incremental-id-source.kamelet.yaml ``` You can check the newly created `kamelet` checking the list of kamelets available: ``` $ kubectl get kamelets -NAME PHASE -timer-source Ready +NAME PHASE +incremental-id-source Ready ``` -## Error Sink Kamelet -Now it's the turn of installing the _error-sink_ Kamelet defined in `error-sink.kamelet.yaml` file: +## Log Sink Kamelet +Now it's the turn of installing the log-sink_ Kamelet defined in `log-sink.kamelet.yaml` file: ``` -$ kubectl apply -f error-sink.kamelet.yaml +$ kubectl apply -f log-sink.kamelet.yaml ``` You can check the newly created `kamelet` checking the list of kamelets available: ``` $ kubectl get kamelets -NAME PHASE -error-sink Ready -timer-source Ready +NAME PHASE +log-sink Ready +incremental-id-source Ready +``` +## Error handler Kamelet +We finally install an error handler as a dead letter channel as specified in `error-handler.kamelet.yaml` file. You can specify any kind of error handler as expected by Apache Camel runtime. +``` +$ kubectl apply -f error-handler.kamelet.yaml +``` +You can check the newly created `kamelet` checking the list of kamelets available: +``` +$ kubectl get kamelets + +NAME PHASE +error-handler Ready +log-sink Ready +incremental-id-source Ready ``` ## Error Handler Kamelet Binding -We can create a `KameletBinding` which is triggered by the _timer-source_ `Kamelet` and define a fake sink URI in order to make it fail on purpose. Then we can configure an _errorHandler_ as defined in `kamelet-binding-error-handler.yaml` file: +We can create a `KameletBinding` which is triggered by the _incremental-id-source_ `Kamelet` and log events to _log-sink_ `Kamelet`. As this will sporadically fail, we can configure an _errorHandler_ as defined in `kamelet-binding-error-handler.yaml` file: ``` +... errorHandler: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 - name: log-sink - properties: - defaultMessage: "ERROR ERROR!" + name: error-handler ``` Execute the following command to start the `Integration`: ``` kubectl apply -f kamelet-binding-error-handler.yaml ``` -As soon as the `Integration` starts, it will log a message error for every failure it will print: +As soon as the `Integration` starts, it will log the events on the `ok` log channel and errors on the `error` log channel: +``` +[1] 2021-04-13 10:33:38,375 INFO [ok] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Producing message #8] +[1] 2021-04-13 10:33:39,376 INFO [ok] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Producing message #9] +[1] 2021-04-13 10:33:40,409 INFO [error] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, Headers: {firedTime=Tue Apr 13 10:33:40 GMT 2021}, BodyType: String, Body: Producing message #10, CaughtExceptionType: org.apache.camel.CamelExecutionException, CaughtExceptionMessage: Exception occurred during execution on the exchange: Exchange[]] +[1] 2021-04-13 10:33:41,375 INFO [ok] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Producing message #11] ``` -[1] 2021-03-16 15:11:33,099 INFO [error-sink] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: ERROR ERROR!] -[1] 2021-03-16 15:11:33,988 INFO [error-sink] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: ERROR ERROR!] -[1] 2021-03-16 15:11:34,988 INFO [error-sink] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: ERROR ERROR!] -``` \ No newline at end of file +This example is useful to guide you through the configuration of an error handler. In a production environment you will likely configure the error handler with a `Dead Letter Channel` pointing to a persistent queue. \ No newline at end of file diff --git a/examples/kamelets/error-handler/timer-source.kamelet.yaml b/examples/kamelets/error-handler/timer-source.kamelet.yaml deleted file mode 100644 index f837fb5..0000000 --- a/examples/kamelets/error-handler/timer-source.kamelet.yaml +++ /dev/null @@ -1,54 +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. -# --------------------------------------------------------------------------- - -apiVersion: camel.apache.org/v1alpha1 -kind: Kamelet -metadata: - name: timer-source - annotations: - camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gU3ZnIFZlY3RvciBJY29ucyA6IGh0dHA6Ly93d3cub25saW5ld2ViZm9udHMuY29tL2ljb24gLS0+DQo8IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPg0KPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm [...] - labels: - camel.apache.org/kamelet.type: "source" -spec: - definition: - title: "Timer Source" - description: "Produces periodic events with a custom payload" - required: - - message - properties: - period: - title: Period - description: The interval between two events - type: integer - default: 1000 - message: - title: Message - description: The message to generate - type: string - example: "hello world" - types: - out: - mediaType: text/plain - flow: - from: - uri: timer:tick - parameters: - period: "{{period}}" - steps: - - set-body: - constant: "{{message}}" - - to: "kamelet:sink"
