├── .github └── workflows │ ├── build.yml │ └── release.yml ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── examples ├── balancer │ ├── balancer.png │ ├── start.sh │ └── trigger_incident.sh ├── example1 │ ├── example1.png │ ├── start.sh │ ├── trigger_crash_incident.sh │ └── trigger_resource_incident.sh ├── example2 │ ├── incident.sh │ └── start.sh ├── paymentService │ ├── crash_incident.sh │ ├── errors_incident.sh │ ├── ressource_incident.sh │ └── start.sh └── proxy │ └── start.sh ├── go.mod └── service.go /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # Workflow for building the demo service 2 | 3 | name: Build Service 4 | 5 | # Controls when the action will run. 6 | on: 7 | # Triggers the workflow on push or pull request events but only for the master branch 8 | pull_request: 9 | branches: [ master ] 10 | 11 | # Allows you to run this workflow manually from the Actions tab 12 | workflow_dispatch: 13 | 14 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 15 | jobs: 16 | # This workflow contains a single job called "build" 17 | build: 18 | # The type of runner that the job will run on 19 | runs-on: ubuntu-latest 20 | 21 | # Steps represent a sequence of tasks that will be executed as part of the job 22 | steps: 23 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it 24 | - uses: actions/checkout@v4 25 | 26 | - name: Set up Go 27 | id: go 28 | uses: actions/setup-go@v5 29 | with: 30 | go-version: 1.22 31 | cache: false 32 | 33 | # Fetches all necessary go packages and builds the project 34 | - name: Build 35 | run: | 36 | go build -o demoservice 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Create and publish an OCI image 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | env: 9 | REGISTRY: ghcr.io 10 | IMAGE_NAME: ${{ github.repository }} 11 | 12 | jobs: 13 | build-and-push-image: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - uses: actions/checkout@v4 18 | 19 | - name: Determine next version and push tag 20 | id: semantic-release 21 | uses: codfish/semantic-release-action@v3 22 | with: 23 | tag-format: 'v${version}' 24 | branches: | 25 | [ 'feature/containerize', 'master' ] 26 | plugins: | 27 | ['@semantic-release/commit-analyzer', '@semantic-release/release-notes-generator', '@semantic-release/github'] 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | 31 | - name: Log in to the container registry (GitHub packages) 32 | if: steps.semantic-release.outputs.new-release-published == 'true' 33 | uses: docker/login-action@v3 34 | with: 35 | registry: ${{ env.REGISTRY }} 36 | username: ${{ github.actor }} 37 | password: ${{ secrets.GITHUB_TOKEN }} 38 | 39 | - name: Extract metadata (tags, labels) for Docker 40 | if: steps.semantic-release.outputs.new-release-published == 'true' 41 | id: meta 42 | uses: docker/metadata-action@v5 43 | with: 44 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} 45 | tags: | 46 | # Set version tag based on semantic release output 47 | type=semver,pattern=v{{version}},value=${{ steps.semantic-release.outputs.release-version }} 48 | # Set latest tag 49 | type=raw,value=latest 50 | 51 | - name: Build and push Docker image 52 | if: steps.semantic-release.outputs.new-release-published == 'true' 53 | id: push 54 | uses: docker/build-push-action@v6 55 | with: 56 | context: . 57 | push: true 58 | tags: ${{ steps.meta.outputs.tags }} 59 | labels: ${{ steps.meta.outputs.labels }} 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | demoservice 9 | 10 | # Test binary, build with `go test -c` 11 | *.test 12 | 13 | # Output of the go coverage tool, specifically when used with LiteIDE 14 | *.out 15 | 16 | # IntelliJ Idea 17 | .idea -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # syntax=docker/dockerfile:1 2 | FROM golang:1.22 3 | 4 | WORKDIR /app 5 | 6 | COPY go.mod *.go ./ 7 | RUN CGO_ENABLED=0 GOOS=linux go build -o /demoservice 8 | 9 | EXPOSE 8080 10 | 11 | CMD ["/demoservice"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Generic demo service 2 | 3 | Purpose of this service is to simulate specific anomaly situations, such as: 4 | 5 | - Slowdowns 6 | - Failures 7 | - Increase in resource consumption 8 | - Process crashes 9 | - Calls to one or more other services 10 | 11 | Therefore, this generic service can be used to build all kinds of variable demo 12 | situations with either flat or deep call trees. 13 | 14 | The service listenes on following HTTP resource pathes: 15 | - GET '/' normal page return 16 | - POST '/config' service receives anomaly config as a HTTP POST message with a JSON config payload that is defined below. 17 | 18 | ## Usage 19 | 20 | Start service by specifying a listening port: 21 | ./service.exe 8090 22 | Start the service multiple times and let the services call each other 23 | ./service.exe 8090 24 | ./service.exe 8091 25 | 26 | ## Dynamically reconfigure the service 27 | 28 | Push a http POST request to /config on your started service. 29 | 30 | ## Config JSON body 31 | 32 | Count always represents the number of service requests that suffer from that anomaly, e.g.: a count of 5 means the next 5 service requests are affected. 33 | A crash anomaly kills the service process with the given exit code. The resource anomaly allocates a matrix of 100x100 elements multiplied by the given severity. 34 | Callees let you specify the callees this service calls with each service request. Specifying callees allows you to build dynamic multi-level service call trees. 35 | In case the attribute 'Balanced' is set to 'true', the callees are equally iterated with each request. 36 | 37 | ```json 38 | { 39 | "ErrorConfig" : { 40 | ResponseCode 500 41 | Count 5 42 | }, 43 | "SlowdownConfig" : { 44 | SlowdownMillis 500 45 | Count 1 46 | }, 47 | "CrashConfig" : { 48 | Code 3 49 | }, 50 | "ResourceConfig" : { 51 | Severity 5 52 | Count 2 53 | }, 54 | "Callees" : [ 55 | { "Adr" : "http://www.example.com", "Count" : 10 }, 56 | { "Adr" : "http://www.orf.at", "Count" : 3 }, 57 | { "Adr" : "http://localhost:8090", "Count" : 3 } 58 | ], 59 | "Balanced" : true 60 | } 61 | ``` 62 | 63 | ## Example topology 64 | 65 | The **example1/start.sh** shell script copies the generic demo service into 6 individual services. Starts those 6 services on the same machine on 6 different ports and configures them to call them each other in the topology shown by Dynatrace below: 66 | 67 | ![](examples/example1/example1.png) 68 | 69 | ## Balanced example 70 | 71 | The **balancer/start.sh** shell script copies the generic demo service into 6 individual services. Starts those 6 services on the same machine on 6 different ports and configures them to form a balancer and worker pool topology, as it is shown by Dynatrace below: 72 | 73 | ![](examples/balancer/balancer.png) 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /examples/balancer/balancer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wolfgangB33r/demoservice/2961d86f3cd0bdcd64e11bb656ccd883fa82d7e9/examples/balancer/balancer.png -------------------------------------------------------------------------------- /examples/balancer/start.sh: -------------------------------------------------------------------------------- 1 | pkill balancer 2 | pkill worker 3 | 4 | sleep 5 5 | 6 | cp ../../demoservice balancer 7 | cp ../../demoservice worker 8 | 9 | ./balancer 9000& 10 | DT_IGNOREDYNAMICPORT=true DT_NODE_ID=1 ./worker 9100& 11 | DT_IGNOREDYNAMICPORT=true DT_NODE_ID=2 ./worker 9200& 12 | DT_IGNOREDYNAMICPORT=true DT_NODE_ID=3 ./worker 9300& 13 | DT_IGNOREDYNAMICPORT=true DT_NODE_ID=4 ./worker 9400& 14 | DT_IGNOREDYNAMICPORT=true DT_NODE_ID=5 ./worker 9500& 15 | 16 | sleep 5 17 | 18 | curl -i -X POST \ 19 | -H "Content-Type:application/json" \ 20 | -d \ 21 | '{ 22 | "Callees" : [ 23 | { "Adr" : "http://localhost:9100", "Count" : 1 }, 24 | { "Adr" : "http://localhost:9200", "Count" : 1 }, 25 | { "Adr" : "http://localhost:9300", "Count" : 1 }, 26 | { "Adr" : "http://localhost:9400", "Count" : 1 }, 27 | { "Adr" : "http://localhost:9500", "Count" : 1 } 28 | ], 29 | "Balanced" : true 30 | }' \ 31 | 'http://localhost:9000/config' 32 | -------------------------------------------------------------------------------- /examples/balancer/trigger_incident.sh: -------------------------------------------------------------------------------- 1 | # first, configure the balancer to only send to 2 workers instead of previously 5 2 | 3 | curl -i -X POST \ 4 | -H "Content-Type:application/json" \ 5 | -d \ 6 | '{ 7 | "Callees" : [ 8 | { "Adr" : "http://localhost:9400", "Count" : 3 }, 9 | { "Adr" : "http://localhost:9500", "Count" : 3 } 10 | ], 11 | "Balanced" : true 12 | }' \ 13 | 'http://localhost:9000/config' 14 | 15 | # then, slowdown the remaining 2 workers because of the load shift 16 | 17 | sleep 80 18 | 19 | curl -i -X POST \ 20 | -H "Content-Type:application/json" \ 21 | -d \ 22 | '{ 23 | "SlowdownConfig" : { 24 | "SlowdownMillis" : 500, 25 | "Count" : 3000 26 | }, 27 | "Callees" : [ 28 | ] 29 | }' \ 30 | 'http://localhost:9400/config' 31 | 32 | curl -i -X POST \ 33 | -H "Content-Type:application/json" \ 34 | -d \ 35 | '{ 36 | "SlowdownConfig" : { 37 | "SlowdownMillis" : 500, 38 | "Count" : 3000 39 | }, 40 | "Callees" : [ 41 | ] 42 | }' \ 43 | 'http://localhost:9500/config' 44 | -------------------------------------------------------------------------------- /examples/example1/example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wolfgangB33r/demoservice/2961d86f3cd0bdcd64e11bb656ccd883fa82d7e9/examples/example1/example1.png -------------------------------------------------------------------------------- /examples/example1/start.sh: -------------------------------------------------------------------------------- 1 | pkill customerAPI 2 | pkill sideService 3 | pkill calcService 4 | pkill restService 5 | pkill recomService 6 | pkill modelService 7 | 8 | sleep 5 9 | 10 | cp ../../demoservice customerAPI 11 | cp ../../demoservice sideService 12 | cp ../../demoservice calcService 13 | cp ../../demoservice restService 14 | cp ../../demoservice recomService 15 | cp ../../demoservice modelService 16 | 17 | ./customerAPI 8080 > customerapi.log 2>&1 & 18 | ./sideService 8081 > sideservice.log 2>&1 & 19 | ./calcService 8082 > calcservice.log 2>&1 & 20 | ./restService 8083 > restservice.log 2>&1 & 21 | ./recomService 8084 > recomservice.log 2>&1 & 22 | ./modelService 8085 > modelservice.log 2>&1 & 23 | 24 | sleep 5 25 | 26 | curl -i -X POST \ 27 | -H "Content-Type:application/json" \ 28 | -d \ 29 | '{ 30 | "Callees" : [ 31 | { "Adr" : "http://localhost:8081", "Count" : 2 }, 32 | { "Adr" : "http://localhost:8082", "Count" : 3 }, 33 | { "Adr" : "http://www.example.com", "Count" : 1 } 34 | ] 35 | }' \ 36 | 'http://localhost:8080/config' 37 | 38 | curl -i -X POST \ 39 | -H "Content-Type:application/json" \ 40 | -d \ 41 | '{ 42 | "Callees" : [ 43 | { "Adr" : "http://localhost:8083", "Count" : 2 }, 44 | { "Adr" : "http://localhost:8084", "Count" : 2 }, 45 | { "Adr" : "http://www.example.com", "Count" : 1 } 46 | ] 47 | }' \ 48 | 'http://localhost:8081/config' 49 | 50 | curl -i -X POST \ 51 | -H "Content-Type:application/json" \ 52 | -d \ 53 | '{ 54 | "Callees" : [ 55 | { "Adr" : "http://localhost:8085", "Count" : 2 }, 56 | { "Adr" : "http://www.example.com", "Count" : 1 } 57 | ] 58 | }' \ 59 | 'http://localhost:8084/config' 60 | -------------------------------------------------------------------------------- /examples/example1/trigger_crash_incident.sh: -------------------------------------------------------------------------------- 1 | curl -i -X POST \ 2 | -H "Content-Type:application/json" \ 3 | -d \ 4 | '{ 5 | "CrashConfig" : { 6 | "Code" : 9 7 | }, 8 | "Callees" : [ 9 | ] 10 | }' \ 11 | 'http://localhost:8002/config' 12 | -------------------------------------------------------------------------------- /examples/example1/trigger_resource_incident.sh: -------------------------------------------------------------------------------- 1 | curl -i -X POST \ 2 | -H "Content-Type:application/json" \ 3 | -d \ 4 | '{ 5 | "ResourceConfig" : { 6 | "Severity" : 100, 7 | "Count" : 30000 8 | }, 9 | "Callees" : [ 10 | ] 11 | }' \ 12 | 'http://localhost:8006/config' 13 | -------------------------------------------------------------------------------- /examples/example2/incident.sh: -------------------------------------------------------------------------------- 1 | echo Triggering a slowdown of the database service by 50ms 2 | curl -i -X POST \ 3 | -H "Content-Type:application/json" \ 4 | -d \ 5 | '{ 6 | "SlowdownConfig" : { 7 | "SlowdownMillis" : 150, 8 | "Count" : 10000 9 | }, 10 | "Callees" : [ 11 | ] 12 | }' \ 13 | 'http://localhost:9305/config' -------------------------------------------------------------------------------- /examples/example2/start.sh: -------------------------------------------------------------------------------- 1 | echo Killing all the running example processes 2 | pkill stockweb 3 | pkill stockticker 4 | pkill loginservice 5 | pkill contentprovider 6 | pkill database 7 | echo Wait 5 seconds before starting processes 8 | 9 | sleep 5 10 | 11 | cp ../../demoservice stockweb 12 | cp ../../demoservice stockticker 13 | cp ../../demoservice loginservice 14 | cp ../../demoservice contentprovider 15 | cp ../../demoservice database 16 | echo copied all demo processes 17 | 18 | ./stockweb 9301 > stockweb.log 2>&1 & 19 | ./stockticker 9302 > stockticker.log 2>&1 & 20 | ./loginservice 9303 > loginservice.log 2>&1 & 21 | ./contentprovider 9304 > contentprovider.log 2>&1 & 22 | ./database 9305 > database.log 2>&1 & 23 | echo started all demo services 24 | 25 | sleep 5 26 | 27 | curl -i -X POST \ 28 | -H "Content-Type:application/json" \ 29 | -d \ 30 | '{ 31 | "Callees" : [ 32 | { "Adr" : "http://localhost:9303", "Count" : 1 }, 33 | { "Adr" : "http://localhost:9302", "Count" : 2 }, 34 | { "Adr" : "http://www.example.com", "Count" : 1 } 35 | ] 36 | }' \ 37 | 'http://localhost:9301/config' 38 | 39 | echo configured stockweb to call login service and stockticker 40 | 41 | curl -i -X POST \ 42 | -H "Content-Type:application/json" \ 43 | -d \ 44 | '{ 45 | "Callees" : [ 46 | { "Adr" : "http://localhost:9303", "Count" : 2 }, 47 | { "Adr" : "http://localhost:9304", "Count" : 2 } 48 | ] 49 | }' \ 50 | 'http://localhost:9302/config' 51 | 52 | echo configured stockticker to call authetication service and content service 53 | 54 | curl -i -X POST \ 55 | -H "Content-Type:application/json" \ 56 | -d \ 57 | '{ 58 | "Callees" : [ 59 | { "Adr" : "http://localhost:9305", "Count" : 5 } 60 | ] 61 | }' \ 62 | 'http://localhost:9303/config' 63 | 64 | echo configured login service to call the database 65 | 66 | curl -i -X POST \ 67 | -H "Content-Type:application/json" \ 68 | -d \ 69 | '{ 70 | "Callees" : [ 71 | { "Adr" : "http://localhost:9305", "Count" : 5 } 72 | ] 73 | }' \ 74 | 'http://localhost:9304/config' 75 | 76 | echo configured the content service to call the database -------------------------------------------------------------------------------- /examples/paymentService/crash_incident.sh: -------------------------------------------------------------------------------- 1 | curl -i -X POST \ 2 | -H "Content-Type:application/json" \ 3 | -d \ 4 | '{ 5 | "CrashConfig" : { 6 | "Code" : 9 7 | }, 8 | "Callees" : [ 9 | ] 10 | }' \ 11 | 'http://localhost:8083/config' 12 | -------------------------------------------------------------------------------- /examples/paymentService/errors_incident.sh: -------------------------------------------------------------------------------- 1 | curl -i -X POST \ 2 | -H "Content-Type:application/json" \ 3 | -d \ 4 | '{ 5 | "ErrorConfig" : { 6 | "ResponseCode" : 500, 7 | "Count": 100 8 | }, 9 | "Callees" : [ 10 | ] 11 | }' \ 12 | 'http://localhost:8083/config' -------------------------------------------------------------------------------- /examples/paymentService/ressource_incident.sh: -------------------------------------------------------------------------------- 1 | curl -i -X POST \ 2 | -H "Content-Type:application/json" \ 3 | -d \ 4 | '{ 5 | "ResourceConfig" : { 6 | "Severity" : 100, 7 | "Count" : 30000 8 | }, 9 | "Callees" : [ 10 | ] 11 | }' \ 12 | 'http://localhost:8083/config' 13 | -------------------------------------------------------------------------------- /examples/paymentService/start.sh: -------------------------------------------------------------------------------- 1 | pkill paymentService 2 | pkill authenticationService 3 | pkill persistanceService 4 | pkill riskAssessmentService 5 | 6 | sleep 5 7 | 8 | cp ../../demoservice paymentService 9 | cp ../../demoservice authenticationService 10 | cp ../../demoservice persistanceService 11 | cp ../../demoservice riskAssessmentService 12 | 13 | ./paymentService 8080 > payment.log 2>&1 & 14 | ./authenticationService 8081 > payment.log 2>&1 & 15 | ./persistanceService 8082 > payment.log 2>&1 & 16 | ./riskAssessmentService 8083 > payment.log 2>&1 & 17 | 18 | sleep 5 19 | 20 | curl -i -X POST \ 21 | -H "Content-Type:application/json" \ 22 | -d \ 23 | '{ 24 | "Callees" : [ 25 | { "Adr" : "http://localhost:8081", "Count" : 1 }, 26 | { "Adr" : "http://localhost:8082", "Count" : 5 }, 27 | { "Adr" : "http://localhost:8083", "Count" : 1 } 28 | ] 29 | }' \ 30 | 'http://localhost:8080/config' 31 | 32 | -------------------------------------------------------------------------------- /examples/proxy/start.sh: -------------------------------------------------------------------------------- 1 | echo Killing all the running example processes 2 | pkill caller 3 | pkill proxy 4 | pkill destination 5 | echo Wait 5 seconds before starting processes 6 | 7 | sleep 5 8 | 9 | cp ../../demoservice caller 10 | cp ../../demoservice proxy 11 | cp ../../demoservice destination 12 | echo copied all demo processes 13 | 14 | ./caller 8497 & 15 | ./proxy 8498 > proxy.log 2>&1 & 16 | ./destination 8499 & 17 | echo started all demo services 18 | 19 | sleep 5 20 | 21 | curl -i -X POST \ 22 | -H "Content-Type:application/json" \ 23 | -d \ 24 | '{ 25 | "Callees" : [ 26 | { "Adr" : "http://localhost:8498", "Count" : 1 } 27 | ] 28 | }' \ 29 | 'http://localhost:8497/config' 30 | 31 | echo configured caller to call the proxy 32 | 33 | curl -i -X POST \ 34 | -H "Content-Type:application/json" \ 35 | -d \ 36 | '{ 37 | "Callees" : [ 38 | { "Adr" : "http://localhost:8499", "Count" : 1 } 39 | ], 40 | "Proxy" : true 41 | }' \ 42 | 'http://localhost:8498/config' 43 | 44 | echo configured the proxy to call the destination service 45 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module demoservice 2 | 3 | go 1.22.5 4 | -------------------------------------------------------------------------------- /service.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io/ioutil" 8 | "log" 9 | "math/rand" 10 | "net/http" 11 | "os" 12 | "strconv" 13 | "strings" 14 | "time" 15 | ) 16 | 17 | type errorAnomalyConfig struct { 18 | ResponseCode int 19 | Count int 20 | } 21 | 22 | type slowdownAnomalyConfig struct { 23 | SlowdownMillis int 24 | Count int 25 | } 26 | 27 | type crashAnomalyConfig struct { 28 | Code int 29 | } 30 | 31 | type resourceAnomalyConfig struct { 32 | Severity int 33 | Count int 34 | } 35 | 36 | type callee struct { 37 | Adr string // URL address to call 38 | Count int // number of calls per minute 39 | } 40 | 41 | type config struct { 42 | ErrorConfig errorAnomalyConfig 43 | SlowdownConfig slowdownAnomalyConfig 44 | CrashConfig crashAnomalyConfig 45 | ResourceConfig resourceAnomalyConfig 46 | Callees []callee 47 | Balanced bool 48 | Proxy bool 49 | } 50 | 51 | var conf config 52 | var reqcount int 53 | 54 | func receiveConfig(w http.ResponseWriter, r *http.Request) { 55 | switch r.Method { 56 | case "POST": 57 | w.WriteHeader(http.StatusNoContent) 58 | body, err := ioutil.ReadAll(r.Body) 59 | if err != nil { 60 | panic(err) 61 | } 62 | //fmt.Printf(string(body)) 63 | err = json.Unmarshal(body, &conf) 64 | if err != nil { 65 | fmt.Printf("config payload wrong") 66 | log.Printf("%s config payload is wrong", os.Args[0]) 67 | panic(err) 68 | } 69 | log.Printf("%s received a new service config deployment", os.Args[0]) 70 | default: 71 | fmt.Fprintf(w, "sorry, only POST method is supported.") 72 | } 73 | defer r.Body.Close() 74 | } 75 | 76 | func handleIcon(w http.ResponseWriter, r *http.Request) { 77 | defer r.Body.Close() 78 | } 79 | 80 | func sayHello(w http.ResponseWriter, r *http.Request) { 81 | var buf bytes.Buffer 82 | reqcount++ 83 | fmt.Fprintf(&buf, "it's the %d call\n", reqcount) 84 | fmt.Fprintf(&buf, "what I did:\n") 85 | // first call all callees we have in the config with the multiplicity given 86 | failures := false 87 | 88 | for ci, element := range conf.Callees { 89 | if !conf.Balanced || reqcount%len(conf.Callees) == ci { 90 | for i := 0; i < element.Count; i++ { 91 | req, err := http.NewRequest("GET", element.Adr, nil) 92 | if err != nil { 93 | // os.Args[0] to get the current exe name 94 | log.Printf("%s error reading request.", os.Args[0]) 95 | os.Exit(1) 96 | } 97 | if conf.Proxy { 98 | log.Printf("%s dt header: %s ", os.Args[0], r.Header.Get("X-Dynatrace")) 99 | log.Printf("%s RemoteAddr: %s ", os.Args[0], r.RemoteAddr) 100 | req.Header.Set("X-Dynatrace", r.Header.Get("X-Dynatrace")) 101 | req.Header.Set("x-forwarded-for", r.RemoteAddr) 102 | req.Header.Set("forwarded", r.RemoteAddr) 103 | } 104 | req.Header.Set("Cache-Control", "no-cache") 105 | 106 | client := &http.Client{Timeout: time.Second * 10} 107 | 108 | resp, err := client.Do(req) 109 | if err != nil { 110 | log.Printf("%s error reading response.", os.Args[0]) 111 | os.Exit(1) 112 | } else { 113 | if resp.StatusCode != 200 { 114 | log.Printf("%s got a bad return", os.Args[0]) 115 | failures = true 116 | } 117 | } 118 | defer resp.Body.Close() 119 | } 120 | fmt.Fprintf(&buf, "called %s %d times\n", element.Adr, element.Count) 121 | } 122 | } 123 | // then check if we should crash the process 124 | if conf.CrashConfig.Code != 0 { 125 | log.Printf("%s cashed.", os.Args[0]) 126 | panic("a problem") 127 | } 128 | // then check if we should add a delay 129 | if conf.SlowdownConfig.SlowdownMillis != 0 && conf.SlowdownConfig.Count > 0 { 130 | time.Sleep(time.Duration(conf.SlowdownConfig.SlowdownMillis) * time.Millisecond) 131 | conf.SlowdownConfig.Count = conf.SlowdownConfig.Count - 1 132 | fmt.Fprintf(&buf, "sleeped for %d millis\n", conf.SlowdownConfig.SlowdownMillis) 133 | } 134 | // then check if we should increase resource consumption 135 | if conf.ResourceConfig.Severity != 0 && conf.ResourceConfig.Count > 0 { 136 | for c := 0; c <= conf.ResourceConfig.Severity; c++ { 137 | m1 := [100][100]int{} 138 | for i := 0; i < 100; i++ { 139 | for j := 0; j < 100; j++ { 140 | m1[i][j] = rand.Int() 141 | } 142 | } 143 | } 144 | fmt.Fprintf(&buf, "allocated %d 100x100 matrices with random values\n", conf.ResourceConfig.Severity) 145 | conf.ResourceConfig.Count = conf.ResourceConfig.Count - 1 146 | log.Printf("%s high resource consumption service call", os.Args[0]) 147 | } 148 | // then check if the should return an error response code 149 | if failures || (conf.ErrorConfig.ResponseCode != 0 && conf.ErrorConfig.Count > 0) { 150 | if conf.ErrorConfig.ResponseCode == 400 { 151 | w.WriteHeader(http.StatusForbidden) 152 | } else { 153 | w.WriteHeader(http.StatusInternalServerError) 154 | } 155 | conf.ErrorConfig.Count = conf.ErrorConfig.Count - 1 156 | } else { 157 | message := r.URL.Path 158 | message = strings.TrimPrefix(message, "/") 159 | message = "finally returned " + message 160 | w.Write([]byte(message)) 161 | } 162 | defer r.Body.Close() 163 | } 164 | 165 | func main() { 166 | port := 8080 167 | if len(os.Args) > 1 { 168 | arg := os.Args[1] 169 | fmt.Printf("Start demo service at port: %s\n", arg) 170 | i1, err := strconv.Atoi(arg) 171 | if err == nil { 172 | port = i1 173 | } 174 | } else { 175 | fmt.Printf("Start demo service at default port: %d\n", port) 176 | } 177 | 178 | http.HandleFunc("/", sayHello) 179 | http.HandleFunc("/favicon.ico", handleIcon) 180 | http.HandleFunc("/config", receiveConfig) 181 | if err := http.ListenAndServe(":"+strconv.Itoa(port), nil); err != nil { 182 | panic(err) 183 | } 184 | } 185 | --------------------------------------------------------------------------------