├── .gitignore ├── LICENSE ├── README.md ├── auth ├── README.md ├── auth.d │ └── main.go ├── endpoints.go ├── logging.go ├── register.go ├── security.go ├── service.go └── transport.go ├── docker ├── docker-compose-filebeat.yml ├── docker-compose-prometheus-grafana-consul.yml └── docker-compose-prometheus-grafana.yml ├── lorem-consul ├── README.md ├── discover.d │ └── main.go ├── endpoints.go ├── instrument.go ├── logging.go ├── lorem-consul.d │ └── main.go ├── prometheus │ └── prometheus.yml ├── register.go ├── service.go └── transport.go ├── lorem-grpc ├── README.md ├── client │ ├── client.go │ └── cmd │ │ └── client_grpc_main.go ├── endpoints.go ├── model.go ├── pb │ ├── lorem.pb.go │ └── lorem.proto ├── server │ └── server_grpc_main.go ├── service.go └── transport.go ├── lorem-hystrix ├── README.md ├── circuitbreaker.go ├── discover.d │ └── main.go ├── endpoints.go ├── instrument.go ├── logging.go ├── lorem-hystrix.d │ └── main.go ├── prometheus │ └── prometheus.yml ├── register.go ├── service.go └── transport.go ├── lorem-logging ├── README.md ├── endpoints.go ├── filebeat │ └── filebeat.yml ├── logging.go ├── lorem-logging.d │ └── main.go ├── service.go └── transport.go ├── lorem-metrics ├── README.md ├── endpoints.go ├── instrument.go ├── logging.go ├── lorem-metrics.d │ └── main.go ├── prometheus │ └── prometheus.yml ├── service.go └── transport.go ├── lorem-rate-limit ├── README.md ├── endpoints.go ├── instrument.go ├── logging.go ├── lorem-rate-limit.d │ └── main.go ├── prometheus │ └── prometheus.yml ├── service.go └── transport.go └── lorem ├── README.md ├── endpoints.go ├── lorem.d └── main.go ├── service.go └── transport.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | /log/ 26 | /extra/* 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gokit-playground 2 | Repository for gokit tutorial 3 | 4 | ## Docker Compose 5 | The docker compose configurations are located under `docker` folder 6 | 7 | ## Simple REST service: 8 | See `lorem` module 9 | 10 | ## gRPC protocol: 11 | See `lorem-grpc` module 12 | 13 | ## Application Logging 14 | See `lorem-logging` module 15 | 16 | ## Rate Limiter 17 | See `lorem-rate-limit` module 18 | 19 | ## Metrics 20 | See `lorem-metrics` module 21 | 22 | ## Service Discovery: Consul 23 | See `lorem-consul` module 24 | 25 | ## Circuit Breaker Pattern 26 | See `lorem-hystrix` module -------------------------------------------------------------------------------- /auth/README.md: -------------------------------------------------------------------------------- 1 | # Auth 2 | This is a sample for authentication using JWT 3 | 4 | ### service.go 5 | Business logic will be put here 6 | 7 | ### endpoint.go 8 | Endpoint will be created here 9 | 10 | ### transport.go 11 | Handling about encode and decode json 12 | 13 | ### register.go 14 | Register service to consul 15 | 16 | ### security.go 17 | Handling authentication, creating JWT then stored into Consul KV 18 | 19 | ### logging.go 20 | Logging function is under this file 21 | 22 | ### Running Consul 23 | 24 | docker run --rm -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap -ui-dir /ui 25 | 26 | ### execute 27 | 28 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 29 | go run auth/auth.d/main.go -consul.addr localhost -consul.port 8500 -advertise.addr 192.168.1.103 -advertise.port 7002 30 | 31 | ### Test it 32 | 33 | curl -X POST http://192.168.1.103:7002/auth/login -d'{"username":"admin","password":"password"}' -v 34 | 35 | #see response header X-Token-Gen 36 | -------------------------------------------------------------------------------- /auth/auth.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "os/signal" 6 | "syscall" 7 | "fmt" 8 | ilog "log" 9 | "flag" 10 | "context" 11 | "github.com/go-kit/kit/log" 12 | //kitjwt "github.com/go-kit/kit/auth/jwt" 13 | "net/http" 14 | "github.com/ru-rocker/gokit-playground/auth" 15 | //"github.com/dgrijalva/jwt-go" 16 | ) 17 | 18 | func main() { 19 | // parse variable from input command 20 | var ( 21 | consulAddr = flag.String("consul.addr", "", "consul address") 22 | consulPort = flag.String("consul.port", "", "consul port") 23 | advertiseAddr = flag.String("advertise.addr", "", "advertise address") 24 | advertisePort = flag.String("advertise.port", "", "advertise port") 25 | ) 26 | flag.Parse() 27 | 28 | ctx := context.Background() 29 | errChan := make(chan error) 30 | 31 | // Logging domain. 32 | var logger log.Logger 33 | { 34 | logger = log.NewLogfmtLogger(os.Stdout) 35 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 36 | logger = log.With(logger, "caller", log.DefaultCaller) 37 | } 38 | 39 | var svc auth.Service 40 | svc = auth.AuthService{} 41 | svc = auth.LoggingMiddleware(logger)(svc) 42 | 43 | e := auth.MakeAuthEndpoint(svc) 44 | e = auth.JwtEndpoint(*consulAddr, *consulPort, logger)(e) 45 | 46 | endpoint := auth.Endpoints{ 47 | AuthEndpoint: e, 48 | HealthEndpoint: auth.MakeHealthEndpoint(svc), 49 | } 50 | 51 | r := auth.MakeHttpHandler(ctx, endpoint, logger) 52 | 53 | // Register Service to Consul 54 | registar := auth.Register(*consulAddr, 55 | *consulPort, 56 | *advertiseAddr, 57 | *advertisePort) 58 | 59 | // HTTP transport 60 | go func() { 61 | ilog.Println("Starting server at port", *advertisePort) 62 | // register service 63 | registar.Register() 64 | handler := r 65 | errChan <- http.ListenAndServe( ":" + *advertisePort, handler) 66 | }() 67 | 68 | 69 | go func() { 70 | c := make(chan os.Signal, 1) 71 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 72 | errChan <- fmt.Errorf("%s", <-c) 73 | }() 74 | error := <- errChan 75 | 76 | // deregister service 77 | registar.Deregister() 78 | ilog.Fatalln(error) 79 | } -------------------------------------------------------------------------------- /auth/endpoints.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "context" 6 | "strings" 7 | "errors" 8 | "github.com/kr/pretty" 9 | ) 10 | 11 | var ErrRequestTypeNotFound = errors.New("Request type only valid for login and logout") 12 | 13 | type CommonReqResp struct{ 14 | 15 | TokenString string `json:"-"` 16 | } 17 | 18 | //request 19 | type AuthRequest struct { 20 | CommonReqResp 21 | Username string `json:"username"` 22 | Password string `json:"password"` 23 | Type string `json:"-"` 24 | } 25 | 26 | //response 27 | type AuthResponse struct { 28 | CommonReqResp 29 | Roles []string `json:"roles,omitempty"` 30 | Mesg string `json:"mesg"` 31 | Err error `json:"err,omitempty"` 32 | } 33 | 34 | //Health Request 35 | type HealthRequest struct { 36 | 37 | } 38 | 39 | //Health Response 40 | type HealthResponse struct { 41 | Status bool `json:"status"` 42 | } 43 | 44 | // endpoints wrapper 45 | type Endpoints struct { 46 | AuthEndpoint endpoint.Endpoint 47 | HealthEndpoint endpoint.Endpoint 48 | } 49 | 50 | // creating Auth Endpoint 51 | func MakeAuthEndpoint(svc Service) endpoint.Endpoint { 52 | return func(ctx context.Context, request interface{}) (interface{}, error) { 53 | var ( 54 | roles []string 55 | mesg string 56 | err error 57 | ) 58 | 59 | req := request.(AuthRequest) 60 | pretty.Print("ctx") 61 | if strings.EqualFold(req.Type, "login") { 62 | mesg, roles, err = svc.Login(req.Username, req.Password) 63 | } else if strings.EqualFold(req.Type, "logout") { 64 | mesg = svc.Logout() 65 | err = nil 66 | } else { 67 | return nil, ErrRequestTypeNotFound 68 | } 69 | 70 | // check if err is not null 71 | if err != nil { 72 | return nil, err 73 | } 74 | return AuthResponse{Mesg:mesg, Roles: roles, Err: err}, nil 75 | } 76 | } 77 | 78 | // creating health endpoint 79 | func MakeHealthEndpoint(svc Service) endpoint.Endpoint { 80 | return func(ctx context.Context, request interface{}) (interface{}, error) { 81 | status := svc.HealthCheck() 82 | return HealthResponse{Status: status }, nil 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /auth/logging.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "github.com/go-kit/kit/log" 5 | "time" 6 | "strings" 7 | ) 8 | 9 | // implement function to return ServiceMiddleware 10 | func LoggingMiddleware(logger log.Logger) ServiceMiddleware { 11 | return func(next Service) Service { 12 | return loggingMiddleware{next, logger} 13 | } 14 | } 15 | 16 | // Make a new type and wrap into Service interface 17 | // Add logger property to this type 18 | type loggingMiddleware struct { 19 | Service 20 | logger log.Logger 21 | } 22 | 23 | // Implement Service Interface for LoggingMiddleware 24 | func (mw loggingMiddleware) Login(username string, password string) (mesg string, roles []string, err error) { 25 | defer func(begin time.Time){ 26 | mw.logger.Log( 27 | "function","Login", 28 | "mesg", mesg, 29 | "roles", strings.Join(roles, ","), 30 | "took", time.Since(begin), 31 | ) 32 | }(time.Now()) 33 | mesg, roles, err = mw.Service.Login(username, password) 34 | return 35 | } 36 | 37 | func (mw loggingMiddleware) Logout() (mesg string) { 38 | defer func(begin time.Time){ 39 | mw.logger.Log( 40 | "function","Logout", 41 | "result", mesg, 42 | "took", time.Since(begin), 43 | ) 44 | }(time.Now()) 45 | mesg = mw.Service.Logout() 46 | return 47 | } 48 | -------------------------------------------------------------------------------- /auth/register.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | consulsd "github.com/go-kit/kit/sd/consul" 5 | "github.com/go-kit/kit/log" 6 | "os" 7 | "github.com/hashicorp/consul/api" 8 | "github.com/go-kit/kit/sd" 9 | "math/rand" 10 | "strconv" 11 | "time" 12 | "net" 13 | ) 14 | 15 | func Register(consulAddress string, 16 | consulPort string, 17 | advertiseAddress string, 18 | advertisePort string) (registar sd.Registrar) { 19 | 20 | // Logging domain. 21 | var logger log.Logger 22 | { 23 | logger = log.NewLogfmtLogger(os.Stderr) 24 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 25 | logger = log.With(logger, "caller", log.DefaultCaller) 26 | } 27 | 28 | client := consulsd.NewClient(ConsulClient(consulAddress, consulPort, logger)) 29 | rand.Seed(time.Now().UTC().UnixNano()) 30 | check := api.AgentServiceCheck{ 31 | HTTP: "http://" + advertiseAddress + ":" + advertisePort + "/health", 32 | Interval: "10s", 33 | Timeout: "1s", 34 | Notes: "Basic health checks", 35 | } 36 | 37 | port, _ := strconv.Atoi(advertisePort) 38 | num := rand.Intn(100) // to make service ID unique 39 | asr := api.AgentServiceRegistration{ 40 | ID: "auth" + strconv.Itoa(num), //unique service ID 41 | Name: "auth", 42 | Address: advertiseAddress, 43 | Port: port, 44 | Tags: []string{"auth", "ru-rocker"}, 45 | Check: &check, 46 | } 47 | registar = consulsd.NewRegistrar(client, &asr, logger) 48 | return 49 | } 50 | 51 | //retrieve consul api client for make consulsd client or KV 52 | func ConsulClient(consulAddress string, consulPort string, logger log.Logger) *api.Client { 53 | // Service discovery domain. In this example we use Consul. 54 | consulConfig := api.DefaultConfig() 55 | consulConfig.Address = net.JoinHostPort(consulAddress, consulPort) 56 | consulClient, err := api.NewClient(consulConfig) 57 | if err != nil { 58 | logger.Log("err", err) 59 | os.Exit(1) 60 | } 61 | return consulClient 62 | } 63 | -------------------------------------------------------------------------------- /auth/security.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "github.com/leonelquinteros/gorand" 6 | "context" 7 | "github.com/go-kit/kit/log" 8 | "github.com/hashicorp/consul/api" 9 | "strings" 10 | "github.com/SermoDigital/jose/jws" 11 | "github.com/SermoDigital/jose/jwt" 12 | "github.com/SermoDigital/jose/crypto" 13 | "time" 14 | "encoding/json" 15 | ) 16 | 17 | var ( 18 | key = []byte("ru-rocker") 19 | method = crypto.SigningMethodHS256 20 | ) 21 | 22 | func JwtEndpoint(consulAddress string, consulPort string, log log.Logger) endpoint.Middleware { 23 | 24 | return func(next endpoint.Endpoint) endpoint.Endpoint { 25 | return func(ctx context.Context, request interface{}) (response interface{}, err error) { 26 | req := request.(AuthRequest) 27 | response, err = next(ctx, request) 28 | 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | resp := response.(AuthResponse) 34 | if strings.EqualFold("login", req.Type) { 35 | err = loginHandler(consulAddress, consulPort, 36 | req.Username, &resp, log) 37 | } else if strings.EqualFold("logout", req.Type) { 38 | println("logout") 39 | err = logoutHandler(consulAddress, consulPort, 40 | req, &resp, log) 41 | } 42 | 43 | return resp, err 44 | } 45 | } 46 | } 47 | //create jwt keyFunc to retrieve kid 48 | //func keyFunc(token *jwt.Token) (interface{}, error) { 49 | // return key, nil 50 | //} 51 | // handling login 52 | func loginHandler(consulAddress string, consulPort string, 53 | username string, resp *AuthResponse, log log.Logger) error { 54 | 55 | var ( 56 | cid string 57 | tokenString string 58 | ) 59 | 60 | defer func(){ 61 | log.Log( 62 | "username", username, 63 | "jwtid", cid, 64 | "token", tokenString, 65 | ) 66 | 67 | }() 68 | 69 | uuid, err := gorand.UUID() 70 | if err != nil { 71 | panic(err.Error()) 72 | } 73 | cid = uuid 74 | 75 | claims := jws.Claims{} 76 | 77 | m := map[string]interface{} { 78 | "username": username, 79 | "roles": resp.Roles, 80 | } 81 | val, _ := json.Marshal(m) 82 | 83 | claims.SetIssuer("ru-rocker.com") 84 | claims.SetIssuedAt(time.Now()) 85 | claims.SetExpiration(time.Now().Add(time.Duration(5) * time.Second)) 86 | claims.SetJWTID(cid) 87 | 88 | j := jws.NewJWT(claims, method) 89 | 90 | b, err := j.Serialize(key) 91 | if err != nil { 92 | return err 93 | } 94 | tokenString = string(b[:]) 95 | resp.TokenString = tokenString 96 | 97 | errChan := make(chan error) 98 | //register UUID on Consul KV 99 | go func() { 100 | client := ConsulClient(consulAddress, consulPort, log) 101 | kv := client.KV() 102 | 103 | key := "session/" + uuid 104 | p := &api.KVPair{Key: key, Value: []byte(val)} 105 | _, e := kv.Put(p, nil) 106 | if e != nil { 107 | errChan <- e 108 | } else { 109 | errChan <- nil 110 | } 111 | }() 112 | 113 | if err = <- errChan; err != nil { 114 | return err 115 | } 116 | return nil 117 | } 118 | 119 | // handling logout 120 | func logoutHandler(consulAddress string, consulPort string, 121 | req AuthRequest, resp *AuthResponse, log log.Logger) error { 122 | 123 | var ( 124 | username string 125 | cid string 126 | tokenString string 127 | ) 128 | 129 | defer func(){ 130 | log.Log( 131 | "username", username, 132 | "jwtid", cid, 133 | "token", tokenString, 134 | ) 135 | 136 | }() 137 | 138 | leeway := 10 * time.Second 139 | tokenString = req.TokenString 140 | username = req.Username 141 | w, err := jws.ParseJWT([]byte(tokenString)) 142 | if err != nil { 143 | return err 144 | } 145 | 146 | claims := w.Claims() 147 | 148 | if jwtid, ok := claims.JWTID(); ok { 149 | cid = jwtid 150 | } 151 | 152 | err = claims.Validate(time.Now(), leeway, leeway); 153 | 154 | if err == nil || err == jwt.ErrTokenIsExpired { 155 | 156 | errChan := make(chan error) 157 | //remove UUID on Consul KV 158 | go func(){ 159 | client := ConsulClient(consulAddress, consulPort, log) 160 | kv := client.KV() 161 | key := "session/" + cid 162 | _, e := kv.Delete (key, nil) 163 | resp.TokenString = "" 164 | if err != nil { 165 | errChan <- err 166 | } else if e != nil { 167 | errChan <- e 168 | } else { 169 | errChan <- nil 170 | } 171 | }() 172 | 173 | if err = <- errChan; err != nil { 174 | return err 175 | } else if err == jwt.ErrTokenIsExpired{ 176 | return err 177 | } 178 | } 179 | 180 | return nil 181 | } -------------------------------------------------------------------------------- /auth/service.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "strings" 5 | "errors" 6 | ) 7 | 8 | type Service interface { 9 | 10 | Login(username string, password string) (mesg string, roles []string, err error) 11 | 12 | Logout() string 13 | 14 | // health check 15 | HealthCheck() bool 16 | 17 | } 18 | 19 | type AuthService struct { 20 | 21 | } 22 | 23 | // create type that return function. 24 | // this will be needed in main.go 25 | type ServiceMiddleware func(Service) Service 26 | 27 | var InvalidLoginErr = errors.New("Username or Password does not match. Authentication failed.") 28 | 29 | func (AuthService) Login(username string, password string) (mesg string, roles []string, err error) { 30 | if strings.EqualFold("admin", username) && 31 | strings.EqualFold("password", password) { 32 | mesg, roles, err = "Login succeed", []string{"Admin", "User"}, nil 33 | } else { 34 | mesg, roles, err = "", nil, InvalidLoginErr 35 | } 36 | return 37 | } 38 | 39 | func (AuthService) Logout() string { 40 | return "Logout Succeed." 41 | } 42 | 43 | func (AuthService) HealthCheck() bool { 44 | //to make the health check always return true is a bad idea 45 | //however, I did this on purpose to make the sample run easier. 46 | //Ideally, it should check things such as amount of free disk space or free memory 47 | return true 48 | } -------------------------------------------------------------------------------- /auth/transport.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "github.com/gorilla/mux" 5 | httptransport "github.com/go-kit/kit/transport/http" 6 | "github.com/go-kit/kit/log" 7 | "context" 8 | "encoding/json" 9 | "net/http" 10 | "errors" 11 | "strings" 12 | ) 13 | 14 | var ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 15 | 16 | // Make Http Handler 17 | func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 18 | r := mux.NewRouter() 19 | options := []httptransport.ServerOption{ 20 | httptransport.ServerErrorLogger(logger), 21 | httptransport.ServerErrorEncoder(encodeError), 22 | } 23 | 24 | //POST /auth/{type} 25 | //type can be login or logout 26 | r.Methods("POST").Path("/auth/{type}").Handler(httptransport.NewServer( 27 | endpoint.AuthEndpoint, 28 | decodeAuthRequest, 29 | encodeResponse, 30 | options..., 31 | )) 32 | 33 | //GET /health 34 | r.Methods("GET").Path("/health").Handler(httptransport.NewServer( 35 | endpoint.HealthEndpoint, 36 | decodeHealthRequest, 37 | encodeResponse, 38 | options..., 39 | )) 40 | 41 | return r 42 | 43 | } 44 | 45 | // encode error 46 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 47 | if err == nil { 48 | panic("encodeError with nil error") 49 | } 50 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 51 | 52 | if err == InvalidLoginErr { 53 | w.WriteHeader(http.StatusUnauthorized) 54 | } else { 55 | w.WriteHeader(http.StatusInternalServerError) 56 | } 57 | 58 | json.NewEncoder(w).Encode(map[string]interface{}{ 59 | "error": err.Error(), 60 | }) 61 | } 62 | 63 | // errorer is implemented by all concrete response types that may contain 64 | // errors. It allows us to change the HTTP response code without needing to 65 | // trigger an endpoint (transport-level) error. 66 | type errorer interface { 67 | error() error 68 | } 69 | 70 | // decode auth request 71 | func decodeAuthRequest(ctx context.Context, r *http.Request) (interface{}, error) { 72 | 73 | vars := mux.Vars(r) 74 | requestType, ok := vars["type"] 75 | if !ok { 76 | return nil, ErrBadRouting 77 | } 78 | 79 | var request AuthRequest 80 | if strings.EqualFold("login", requestType) { 81 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { 82 | return nil, err 83 | } 84 | } 85 | request.Type = requestType 86 | 87 | //get token from header 88 | val := r.Header.Get("Authorization") 89 | authHeaderParts := strings.Split(val, " ") 90 | if len(authHeaderParts) == 2 && strings.ToLower(authHeaderParts[0]) == "bearer" { 91 | request.TokenString = authHeaderParts[1] 92 | } 93 | 94 | return request, nil 95 | } 96 | 97 | // encodeResponse is the common method to encode all response types to the 98 | // client. 99 | func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 100 | 101 | if e, ok := response.(errorer); ok && e.error() != nil { 102 | // Not a Go kit transport error, but a business-logic error. 103 | // Provide those as HTTP errors. 104 | encodeError(ctx, e.error(), w) 105 | return nil 106 | } 107 | 108 | if authResp, ok := response.(AuthResponse); ok { 109 | w.Header().Set("X-TOKEN-GEN", authResp.TokenString) 110 | } 111 | 112 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 113 | return json.NewEncoder(w).Encode(response) 114 | } 115 | 116 | // decode health check 117 | func decodeHealthRequest(_ context.Context, _ *http.Request) (interface{}, error) { 118 | return HealthRequest{}, nil 119 | } 120 | -------------------------------------------------------------------------------- /docker/docker-compose-filebeat.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | filebeat: 5 | image: fiunchinho/docker-filebeat 6 | environment: 7 | LOGSTASH_HOST: 172.20.20.10 8 | LOGSTASH_PORT: 5044 9 | INDEX: logstash 10 | volumes: 11 | - $PWD/lorem-logging/filebeat/filebeat.yml:/filebeat.yml 12 | - $PWD/log/lorem/golorem.log:/golorem.log -------------------------------------------------------------------------------- /docker/docker-compose-prometheus-grafana-consul.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | prometheus: 5 | image: prom/prometheus 6 | ports: 7 | - 9090:9090 8 | volumes: 9 | - $PWD/lorem-consul/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 10 | grafana: 11 | image: grafana/grafana 12 | ports: 13 | - 3000:3000 14 | environment: 15 | - GF_SECURITY_ADMIN_PASSWORD=password 16 | volumes: 17 | - $PWD/extra/grafana_db:/var/lib/grafana grafana/grafana -------------------------------------------------------------------------------- /docker/docker-compose-prometheus-grafana.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | prometheus: 5 | image: prom/prometheus 6 | ports: 7 | - 9090:9090 8 | volumes: 9 | - $PWD/lorem-rate-limit/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 10 | grafana: 11 | image: grafana/grafana 12 | ports: 13 | - 3000:3000 14 | environment: 15 | - GF_SECURITY_ADMIN_PASSWORD=password 16 | volumes: 17 | - $PWD/extra/grafana_db:/var/lib/grafana grafana/grafana -------------------------------------------------------------------------------- /lorem-consul/README.md: -------------------------------------------------------------------------------- 1 | # lorem-consul 2 | This is simple service module. Only for showing the micro service with HTTP and return json. 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | In this part I will demonstrate how to discover service by using consul. I copied from `lorem-metrics` 6 | 7 | I am fully using all three functions from the golorem library. 8 | 9 | ## Required libraries 10 | 11 | go get github.com/go-kit/kit 12 | go get github.com/drhodes/golorem 13 | go get github.com/gorilla/mux 14 | go get github.com/juju/ratelimit 15 | go get github.com/prometheus/client_golang/prometheus 16 | go get github.com/go-kit/kit/sd/consul 17 | 18 | ### service.go 19 | Business logic will be put here 20 | 21 | ### endpoint.go 22 | Endpoint will be created here 23 | 24 | ### transport.go 25 | Handling about encode and decode json 26 | 27 | ### logging.go 28 | Logging function is under this file 29 | 30 | ### instrument.go 31 | Middleware function. 32 | For this sample, this function for rate limiting and metrics. 33 | 34 | ### discovery.go 35 | Consul service registration utility 36 | 37 | #### lorem-consul.d 38 | Go main function to build service and register to consul 39 | 40 | #### discover.d 41 | Go main function for discover service 42 | 43 | ### Running Consul 44 | 45 | docker run --rm -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap -ui-dir /ui 46 | 47 | ### execute 48 | 49 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 50 | go run lorem-consul/lorem-consul.d/main.go -consul.addr localhost -consul.port 8500 -advertise.addr 192.168.1.103 -advertise.port 7002 51 | go run lorem-consul/discover.d/main.go -consul.addr localhost -consul.port 8500 52 | 53 | ### Running Prometheus and Grafana 54 | To execute type 55 | 56 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 57 | docker-compose -f docker/docker-compose-prometheus-grafana-consul.yml up -d 58 | -------------------------------------------------------------------------------- /lorem-consul/discover.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/go-kit/kit/sd" 5 | "github.com/go-kit/kit/endpoint" 6 | "io" 7 | "strings" 8 | "net/url" 9 | "net/http" 10 | "context" 11 | ht "github.com/go-kit/kit/transport/http" 12 | consulsd "github.com/go-kit/kit/sd/consul" 13 | "os" 14 | "github.com/go-kit/kit/log" 15 | "github.com/hashicorp/consul/api" 16 | "github.com/go-kit/kit/sd/lb" 17 | "time" 18 | "github.com/gorilla/mux" 19 | "os/signal" 20 | "syscall" 21 | "fmt" 22 | "flag" 23 | "github.com/ru-rocker/gokit-playground/lorem-consul" 24 | "encoding/json" 25 | "strconv" 26 | "errors" 27 | ) 28 | 29 | //to execute: go run src/github.com/ru-rocker/gokit-playground/lorem-consul/discover.d/main.go -consul.addr localhost -consul.port 8500 30 | // curl -XPOST -d'{"requestType":"word", "min":10, "max":10}' http://localhost:8080/sd-lorem 31 | func main() { 32 | 33 | var ( 34 | consulAddr = flag.String("consul.addr", "", "consul address") 35 | consulPort = flag.String("consul.port", "", "consul port") 36 | ) 37 | flag.Parse() 38 | 39 | // Logging domain. 40 | var logger log.Logger 41 | { 42 | logger = log.NewLogfmtLogger(os.Stderr) 43 | logger = log.With(logger,"ts", log.DefaultTimestampUTC) 44 | logger = log.With(logger,"caller", log.DefaultCaller) 45 | } 46 | 47 | 48 | // Service discovery domain. In this example we use Consul. 49 | var client consulsd.Client 50 | { 51 | consulConfig := api.DefaultConfig() 52 | 53 | consulConfig.Address = "http://" + *consulAddr + ":" + *consulPort 54 | consulClient, err := api.NewClient(consulConfig) 55 | if err != nil { 56 | logger.Log("err", err) 57 | os.Exit(1) 58 | } 59 | client = consulsd.NewClient(consulClient) 60 | } 61 | 62 | tags := []string{"lorem", "ru-rocker"} 63 | passingOnly := true 64 | duration := 500 * time.Millisecond 65 | var loremEndpoint endpoint.Endpoint 66 | 67 | ctx := context.Background() 68 | r := mux.NewRouter() 69 | 70 | factory := loremFactory(ctx, "POST", "/lorem") 71 | serviceName := "lorem" 72 | instancer := consulsd.NewInstancer(client, logger, serviceName, tags, passingOnly) 73 | endpointer := sd.NewEndpointer(instancer, factory, logger) 74 | balancer := lb.NewRoundRobin(endpointer) 75 | retry := lb.Retry(1, duration, balancer) 76 | loremEndpoint = retry 77 | 78 | // POST /sd-lorem 79 | // Payload: {"requestType":"word", "min":10, "max":10} 80 | r.Methods("POST").Path("/sd-lorem").Handler(ht.NewServer( 81 | loremEndpoint, 82 | decodeConsulLoremRequest, 83 | lorem_consul.EncodeResponse, // use existing encode response since I did not change the logic on response 84 | )) 85 | 86 | // Interrupt handler. 87 | errc := make(chan error) 88 | go func() { 89 | c := make(chan os.Signal) 90 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 91 | errc <- fmt.Errorf("%s", <-c) 92 | }() 93 | 94 | // HTTP transport. 95 | go func() { 96 | logger.Log("transport", "HTTP", "addr", "8080") 97 | errc <- http.ListenAndServe(":8080", r) 98 | }() 99 | 100 | // Run! 101 | logger.Log("exit", <-errc) 102 | } 103 | 104 | // factory function to parse URL from Consul to Endpoint 105 | func loremFactory(_ context.Context, method, path string) sd.Factory { 106 | return func(instance string) (endpoint.Endpoint, io.Closer, error) { 107 | if !strings.HasPrefix(instance, "http") { 108 | instance = "http://" + instance 109 | } 110 | 111 | tgt, err := url.Parse(instance) 112 | if err != nil { 113 | return nil, nil, err 114 | } 115 | tgt.Path = path 116 | 117 | var ( 118 | enc ht.EncodeRequestFunc 119 | dec ht.DecodeResponseFunc 120 | ) 121 | enc, dec = encodeLoremRequest, decodeLoremResponse 122 | 123 | return ht.NewClient(method, tgt, enc, dec).Endpoint(), nil, nil 124 | } 125 | } 126 | 127 | // decode request from discovery service 128 | // parsing JSON into LoremRequest 129 | func decodeConsulLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 130 | var request lorem_consul.LoremRequest 131 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { 132 | return nil, err 133 | } 134 | return request, nil 135 | } 136 | 137 | // Encode request form LoremRequest into existing Lorem Service 138 | // The encode will translate the LoremRequest into /lorem/{requestType}/{min}/{max} 139 | func encodeLoremRequest(_ context.Context, req *http.Request, request interface{}) error { 140 | lr := request.(lorem_consul.LoremRequest) 141 | p := "/" + lr.RequestType + "/" + strconv.Itoa(lr.Min) + "/" + strconv.Itoa(lr.Max) 142 | req.URL.Path += p 143 | return nil 144 | } 145 | 146 | // decode response from Lorem Service 147 | func decodeLoremResponse(_ context.Context, resp *http.Response) (interface{}, error) { 148 | var response lorem_consul.LoremResponse 149 | var s map[string]interface{} 150 | 151 | if respCode := resp.StatusCode; respCode >= 400 { 152 | if err := json.NewDecoder(resp.Body).Decode(&s); err != nil{ 153 | return nil, err 154 | } 155 | return nil, errors.New(s["error"].(string) + "\n") 156 | } 157 | 158 | if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { 159 | return nil, err 160 | } 161 | 162 | return response, nil 163 | } -------------------------------------------------------------------------------- /lorem-consul/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem_consul 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "strings" 6 | "errors" 7 | "context" 8 | ) 9 | 10 | var ( 11 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 12 | ) 13 | 14 | //Lorem Request 15 | type LoremRequest struct { 16 | RequestType string `json:"requestType"` 17 | Min int `json:"min"` 18 | Max int `json:"max"` 19 | } 20 | 21 | //Lorem Response 22 | type LoremResponse struct { 23 | Message string `json:"message"` 24 | Err error `json:"err,omitempty"` 25 | } 26 | 27 | //Health Request 28 | type HealthRequest struct { 29 | 30 | } 31 | 32 | //Health Response 33 | type HealthResponse struct { 34 | Status bool `json:"status"` 35 | } 36 | 37 | // endpoints wrapper 38 | type Endpoints struct { 39 | LoremEndpoint endpoint.Endpoint 40 | HealthEndpoint endpoint.Endpoint 41 | } 42 | 43 | // creating Lorem Ipsum Endpoint 44 | func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint { 45 | return func(ctx context.Context, request interface{}) (interface{}, error) { 46 | req := request.(LoremRequest) 47 | 48 | var ( 49 | txt string 50 | min, max int 51 | ) 52 | 53 | min = req.Min 54 | max = req.Max 55 | 56 | if strings.EqualFold(req.RequestType, "Word") { 57 | txt = svc.Word(min, max) 58 | } else if strings.EqualFold(req.RequestType, "Sentence"){ 59 | txt = svc.Sentence(min, max) 60 | } else if strings.EqualFold(req.RequestType, "Paragraph") { 61 | txt = svc.Paragraph(min, max) 62 | } else { 63 | return nil, ErrRequestTypeNotFound 64 | } 65 | return LoremResponse{Message: txt}, nil 66 | } 67 | 68 | } 69 | 70 | // creating health endpoint 71 | func MakeHealthEndpoint(svc Service) endpoint.Endpoint { 72 | return func(ctx context.Context, request interface{}) (interface{}, error) { 73 | status := svc.HealthCheck() 74 | return HealthResponse{Status: status }, nil 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /lorem-consul/instrument.go: -------------------------------------------------------------------------------- 1 | package lorem_consul 2 | 3 | import ( 4 | "github.com/go-kit/kit/metrics" 5 | "time" 6 | ) 7 | 8 | func Metrics(requestCount metrics.Counter, 9 | requestLatency metrics.Histogram) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return metricsMiddleware{ 12 | next, 13 | requestCount, 14 | requestLatency, 15 | } 16 | } 17 | } 18 | 19 | // Make a new type and wrap into Service interface 20 | // Add expected metrics property to this type 21 | type metricsMiddleware struct { 22 | Service 23 | requestCount metrics.Counter 24 | requestLatency metrics.Histogram 25 | } 26 | 27 | // Implement service functions and add label method for our metrics 28 | func (mw metricsMiddleware) Word(min, max int) (output string) { 29 | defer func(begin time.Time) { 30 | lvs := []string{"method", "Word"} 31 | mw.requestCount.With(lvs...).Add(1) 32 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 33 | }(time.Now()) 34 | output = mw.Service.Word(min, max) 35 | return 36 | } 37 | 38 | func (mw metricsMiddleware) Sentence(min, max int) (output string) { 39 | defer func(begin time.Time) { 40 | lvs := []string{"method", "Sentence"} 41 | mw.requestCount.With(lvs...).Add(1) 42 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 43 | }(time.Now()) 44 | output = mw.Service.Sentence(min, max) 45 | return 46 | } 47 | 48 | func (mw metricsMiddleware) Paragraph(min, max int) (output string) { 49 | defer func(begin time.Time) { 50 | lvs := []string{"method", "Paragraph"} 51 | mw.requestCount.With(lvs...).Add(1) 52 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 53 | }(time.Now()) 54 | output = mw.Service.Paragraph(min, max) 55 | return 56 | } 57 | 58 | func (mw metricsMiddleware) HealthCheck() (output bool) { 59 | defer func(begin time.Time) { 60 | lvs := []string{"method", "HealthCheck"} 61 | mw.requestCount.With(lvs...).Add(1) 62 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 63 | }(time.Now()) 64 | output = mw.Service.HealthCheck() 65 | return 66 | } -------------------------------------------------------------------------------- /lorem-consul/logging.go: -------------------------------------------------------------------------------- 1 | package lorem_consul 2 | 3 | import ( 4 | "github.com/go-kit/kit/log" 5 | "time" 6 | ) 7 | 8 | // implement function to return ServiceMiddleware 9 | func LoggingMiddleware(logger log.Logger) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return loggingMiddleware{next, logger} 12 | } 13 | } 14 | 15 | // Make a new type and wrap into Service interface 16 | // Add logger property to this type 17 | type loggingMiddleware struct { 18 | Service 19 | logger log.Logger 20 | } 21 | 22 | // Implement Service Interface for LoggingMiddleware 23 | func (mw loggingMiddleware) Word(min, max int) (output string) { 24 | defer func(begin time.Time){ 25 | mw.logger.Log( 26 | "function","Word", 27 | "min", min, 28 | "max", max, 29 | "result", output, 30 | "took", time.Since(begin), 31 | ) 32 | }(time.Now()) 33 | output = mw.Service.Word(min,max) 34 | return 35 | } 36 | 37 | func (mw loggingMiddleware) Sentence(min, max int) (output string) { 38 | defer func(begin time.Time){ 39 | mw.logger.Log( 40 | "function","Sentence", 41 | "min", min, 42 | "max", max, 43 | "result", output, 44 | "took", time.Since(begin), 45 | ) 46 | }(time.Now()) 47 | output = mw.Service.Sentence(min,max) 48 | return 49 | } 50 | 51 | func (mw loggingMiddleware) Paragraph(min, max int) (output string) { 52 | defer func(begin time.Time){ 53 | mw.logger.Log( 54 | "function","Paragraph", 55 | "min", min, 56 | "max", max, 57 | "result", output, 58 | "took", time.Since(begin), 59 | ) 60 | }(time.Now()) 61 | output = mw.Service.Paragraph(min,max) 62 | return 63 | } 64 | 65 | func (mw loggingMiddleware) HealthCheck() (output bool) { 66 | defer func(begin time.Time){ 67 | mw.logger.Log( 68 | "function","HealthCheck", 69 | "result", output, 70 | "took", time.Since(begin), 71 | ) 72 | }(time.Now()) 73 | output = mw.Service.HealthCheck() 74 | return 75 | } -------------------------------------------------------------------------------- /lorem-consul/lorem-consul.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | "os" 6 | "os/signal" 7 | "syscall" 8 | "net/http" 9 | "fmt" 10 | "github.com/go-kit/kit/log" 11 | ratelimitkit "github.com/go-kit/kit/ratelimit" 12 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" 13 | stdprometheus "github.com/prometheus/client_golang/prometheus" 14 | "time" 15 | "github.com/juju/ratelimit" 16 | "github.com/ru-rocker/gokit-playground/lorem-consul" 17 | "flag" 18 | ilog "log" 19 | ) 20 | 21 | func main() { 22 | 23 | // parse variable from input command 24 | var ( 25 | consulAddr = flag.String("consul.addr", "", "consul address") 26 | consulPort = flag.String("consul.port", "", "consul port") 27 | advertiseAddr = flag.String("advertise.addr", "", "advertise address") 28 | advertisePort = flag.String("advertise.port", "", "advertise port") 29 | ) 30 | flag.Parse() 31 | 32 | ctx := context.Background() 33 | errChan := make(chan error) 34 | 35 | // Logging domain. 36 | var logger log.Logger 37 | { 38 | logger = log.NewLogfmtLogger(os.Stdout) 39 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 40 | logger = log.With(logger, "caller", log.DefaultCaller) 41 | } 42 | 43 | //declare metrics 44 | fieldKeys := []string{"method"} 45 | requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ 46 | Namespace: "ru_rocker", 47 | Subsystem: "lorem_service", 48 | Name: "request_count", 49 | Help: "Number of requests received.", 50 | }, fieldKeys) 51 | requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ 52 | Namespace: "ru_rocker", 53 | Subsystem: "lorem_service", 54 | Name: "request_latency_microseconds", 55 | Help: "Total duration of requests in microseconds.", 56 | }, fieldKeys) 57 | 58 | var svc lorem_consul.Service 59 | svc = lorem_consul.LoremService{} 60 | svc = lorem_consul.LoggingMiddleware(logger)(svc) 61 | svc = lorem_consul.Metrics(requestCount, requestLatency)(svc) 62 | 63 | rlbucket := ratelimit.NewBucket(1*time.Second, 5) 64 | e := lorem_consul.MakeLoremLoggingEndpoint(svc) 65 | e = ratelimitkit.NewTokenBucketThrottler(rlbucket, time.Sleep)(e) 66 | 67 | // Make health endpoint 68 | healthEndpoint := lorem_consul.MakeHealthEndpoint(svc) 69 | endpoint := lorem_consul.Endpoints{ 70 | LoremEndpoint: e, 71 | HealthEndpoint: healthEndpoint, 72 | } 73 | 74 | r := lorem_consul.MakeHttpHandler(ctx, endpoint, logger) 75 | 76 | // Register Service to Consul 77 | registar := lorem_consul.Register(*consulAddr, 78 | *consulPort, 79 | *advertiseAddr, 80 | *advertisePort) 81 | 82 | // HTTP transport 83 | go func() { 84 | ilog.Println("Starting server at port", *advertisePort) 85 | // register service 86 | registar.Register() 87 | handler := r 88 | errChan <- http.ListenAndServe(":" + *advertisePort, handler) 89 | }() 90 | 91 | 92 | go func() { 93 | c := make(chan os.Signal, 1) 94 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 95 | errChan <- fmt.Errorf("%s", <-c) 96 | }() 97 | error := <- errChan 98 | // deregister service 99 | registar.Deregister() 100 | ilog.Fatalln(error) 101 | } 102 | -------------------------------------------------------------------------------- /lorem-consul/prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s # By default, scrape targets every 15 seconds. 3 | 4 | # Attach these labels to any time series or alerts when communicating with 5 | # external systems (federation, remote storage, Alertmanager). 6 | external_labels: 7 | monitor: 'rurocker-monitor' 8 | 9 | # A scrape configuration containing exactly one endpoint to scrape: 10 | # Here it's Prometheus itself. 11 | scrape_configs: 12 | # The job name is added as a label `job=` to any timeseries scraped from this config. 13 | - job_name: 'prometheus' 14 | 15 | # Override the global default and scrape targets from this job every 5 seconds. 16 | scrape_interval: 5s 17 | 18 | static_configs: 19 | - targets: ['localhost:9090'] 20 | labels: 21 | group: 'local' 22 | 23 | - job_name: 'ru-rocker' 24 | 25 | scrape_interval: 5s 26 | 27 | consul_sd_configs: 28 | - server: '192.168.1.103:8500' 29 | 30 | relabel_configs: 31 | - source_labels: [__meta_consul_tags] 32 | regex: .*,lorem,.* 33 | action: keep 34 | - source_labels: [__meta_consul_service] 35 | target_label: job 36 | -------------------------------------------------------------------------------- /lorem-consul/register.go: -------------------------------------------------------------------------------- 1 | package lorem_consul 2 | 3 | import ( 4 | consulsd "github.com/go-kit/kit/sd/consul" 5 | "github.com/go-kit/kit/log" 6 | "os" 7 | "github.com/hashicorp/consul/api" 8 | "github.com/go-kit/kit/sd" 9 | "math/rand" 10 | "strconv" 11 | "time" 12 | ) 13 | 14 | func Register(consulAddress string, 15 | consulPort string, 16 | advertiseAddress string, 17 | advertisePort string) (registar sd.Registrar) { 18 | 19 | // Logging domain. 20 | var logger log.Logger 21 | { 22 | logger = log.NewLogfmtLogger(os.Stderr) 23 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 24 | logger = log.With(logger, "caller", log.DefaultCaller) 25 | } 26 | 27 | rand.Seed(time.Now().UTC().UnixNano()) 28 | 29 | 30 | // Service discovery domain. In this example we use Consul. 31 | var client consulsd.Client 32 | { 33 | consulConfig := api.DefaultConfig() 34 | consulConfig.Address = consulAddress + ":" + consulPort 35 | consulClient, err := api.NewClient(consulConfig) 36 | if err != nil { 37 | logger.Log("err", err) 38 | os.Exit(1) 39 | } 40 | client = consulsd.NewClient(consulClient) 41 | } 42 | 43 | check := api.AgentServiceCheck{ 44 | HTTP: "http://" + advertiseAddress + ":" + advertisePort + "/health", 45 | Interval: "10s", 46 | Timeout: "1s", 47 | Notes: "Basic health checks", 48 | } 49 | 50 | port, _ := strconv.Atoi(advertisePort) 51 | num := rand.Intn(100) // to make service ID unique 52 | asr := api.AgentServiceRegistration{ 53 | ID: "lorem" + strconv.Itoa(num), //unique service ID 54 | Name: "lorem", 55 | Address: advertiseAddress, 56 | Port: port, 57 | Tags: []string{"lorem", "ru-rocker"}, 58 | Check: &check, 59 | } 60 | registar = consulsd.NewRegistrar(client, &asr, logger) 61 | return 62 | } 63 | -------------------------------------------------------------------------------- /lorem-consul/service.go: -------------------------------------------------------------------------------- 1 | package lorem_consul 2 | 3 | import ( 4 | golorem "github.com/drhodes/golorem" 5 | ) 6 | 7 | // Define service interface 8 | type Service interface { 9 | // generate a word with at least min letters and at most max letters. 10 | Word(min, max int) string 11 | 12 | // generate a sentence with at least min words and at most max words. 13 | Sentence(min, max int) string 14 | 15 | // generate a paragraph with at least min sentences and at most max sentences. 16 | Paragraph(min, max int) string 17 | 18 | // health check 19 | HealthCheck() bool 20 | } 21 | 22 | // Implement service with empty struct 23 | type LoremService struct { 24 | 25 | } 26 | 27 | // create type that return function. 28 | // this will be needed in main.go 29 | type ServiceMiddleware func(Service) Service 30 | 31 | // Implement service functions 32 | func (LoremService) Word(min, max int) string { 33 | return golorem.Word(min, max) 34 | } 35 | 36 | func (LoremService) Sentence(min, max int) string { 37 | return golorem.Sentence(min, max) 38 | } 39 | 40 | func (LoremService) Paragraph(min, max int) string { 41 | return golorem.Paragraph(min, max) 42 | } 43 | 44 | func (LoremService) HealthCheck() bool { 45 | //to make the health check always return true is a bad idea 46 | //however, I did this on purpose to make the sample run easier. 47 | //Ideally, it should check things such as amount of free disk space or free memory 48 | return true 49 | } -------------------------------------------------------------------------------- /lorem-consul/transport.go: -------------------------------------------------------------------------------- 1 | package lorem_consul 2 | 3 | import ( 4 | "net/http" 5 | "errors" 6 | 7 | "github.com/gorilla/mux" 8 | "github.com/go-kit/kit/log" 9 | httptransport "github.com/go-kit/kit/transport/http" 10 | "encoding/json" 11 | "strconv" 12 | stdprometheus "github.com/prometheus/client_golang/prometheus/promhttp" 13 | "context" 14 | ) 15 | 16 | var ( 17 | // ErrBadRouting is returned when an expected path variable is missing. 18 | ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 19 | ) 20 | 21 | // Make Http Handler 22 | func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 23 | r := mux.NewRouter() 24 | options := []httptransport.ServerOption{ 25 | httptransport.ServerErrorLogger(logger), 26 | httptransport.ServerErrorEncoder(encodeError), 27 | } 28 | 29 | //POST /lorem/{type}/{min}/{max} 30 | r.Methods("POST").Path("/lorem/{type}/{min}/{max}").Handler(httptransport.NewServer( 31 | endpoint.LoremEndpoint, 32 | DecodeLoremRequest, 33 | EncodeResponse, 34 | options..., 35 | )) 36 | 37 | //GET /health 38 | r.Methods("GET").Path("/health").Handler(httptransport.NewServer( 39 | endpoint.HealthEndpoint, 40 | decodeHealthRequest, 41 | EncodeResponse, 42 | options..., 43 | )) 44 | 45 | // GET /metrics 46 | r.Path("/metrics").Handler(stdprometheus.Handler()) 47 | 48 | return r 49 | 50 | } 51 | 52 | // decode url path variables into request 53 | func DecodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 54 | vars := mux.Vars(r) 55 | requestType, ok := vars["type"] 56 | if !ok { 57 | return nil, ErrBadRouting 58 | } 59 | 60 | vmin, ok := vars["min"] 61 | if !ok { 62 | return nil, ErrBadRouting 63 | } 64 | 65 | vmax, ok := vars["max"] 66 | if !ok { 67 | return nil, ErrBadRouting 68 | } 69 | 70 | min, _ := strconv.Atoi(vmin) 71 | max, _ := strconv.Atoi(vmax) 72 | 73 | request := LoremRequest{ 74 | RequestType: requestType, 75 | Min: min, 76 | Max: max, 77 | } 78 | return request, nil 79 | } 80 | 81 | // decode health check 82 | func decodeHealthRequest(_ context.Context, _ *http.Request) (interface{}, error) { 83 | return HealthRequest{}, nil 84 | } 85 | 86 | 87 | // errorer is implemented by all concrete response types that may contain 88 | // errors. It allows us to change the HTTP response code without needing to 89 | // trigger an endpoint (transport-level) error. 90 | type errorer interface { 91 | error() error 92 | } 93 | 94 | // encodeResponse is the common method to encode all response types to the 95 | // client. 96 | func EncodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 97 | if e, ok := response.(errorer); ok && e.error() != nil { 98 | // Not a Go kit transport error, but a business-logic error. 99 | // Provide those as HTTP errors. 100 | encodeError(ctx, e.error(), w) 101 | return nil 102 | } 103 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 104 | return json.NewEncoder(w).Encode(response) 105 | } 106 | 107 | // encode error 108 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 109 | if err == nil { 110 | panic("encodeError with nil error") 111 | } 112 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 113 | w.WriteHeader(http.StatusInternalServerError) 114 | json.NewEncoder(w).Encode(map[string]interface{}{ 115 | "error": err.Error(), 116 | }) 117 | } -------------------------------------------------------------------------------- /lorem-grpc/README.md: -------------------------------------------------------------------------------- 1 | # lorem-grpc 2 | This is simple service module. Only for showing the micro service with gRPC protocol 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | I am fully using all three functions from the golorem library. 6 | 7 | ## Required libraries 8 | 9 | go get github.com/go-kit/kit 10 | go get github.com/drhodes/golorem 11 | go get github.com/gorilla/mux 12 | 13 | # pb 14 | Protocol buffer module. The place to create proto files. 15 | Download protoc from [here](https://github.com/google/protobuf/releases) 16 | Then execute `go get -u github.com/golang/protobuf/{proto,protoc-gen-go}` 17 | 18 | *Note: don't forget to add GOBIN on your PATH* 19 | 20 | To generate protobuf file into go file: 21 | `protoc lorem.proto --go_out=plugins=grpc:.` 22 | 23 | ### service.go 24 | Business logic will be put here 25 | 26 | ### endpoint.go 27 | Endpoint will be created here 28 | 29 | ### model.go 30 | Encode and Decode json 31 | 32 | ### transport.go 33 | Implement interface from protocol buffer 34 | 35 | ### execute 36 | 37 | cd $GOPATH 38 | 39 | #Running grpc server 40 | go run src/github.com/ru-rocker/gokit-playground/lorem-grpc/server/server_grpc_main.go 41 | 42 | #Running client 43 | go run src/github.com/ru-rocker/gokit-playground/lorem-grpc/client/cmd/client_grpc_main.go lorem sentence 10 20 44 | -------------------------------------------------------------------------------- /lorem-grpc/client/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "github.com/ru-rocker/gokit-playground/lorem-grpc" 5 | "github.com/ru-rocker/gokit-playground/lorem-grpc/pb" 6 | grpctransport "github.com/go-kit/kit/transport/grpc" 7 | "google.golang.org/grpc" 8 | ) 9 | 10 | // Return new lorem_grpc service 11 | func New(conn *grpc.ClientConn) lorem_grpc.Service { 12 | var loremEndpoint = grpctransport.NewClient( 13 | conn, "Lorem", "Lorem", 14 | lorem_grpc.EncodeGRPCLoremRequest, 15 | lorem_grpc.DecodeGRPCLoremResponse, 16 | pb.LoremResponse{}, 17 | ).Endpoint() 18 | 19 | return lorem_grpc.Endpoints{ 20 | LoremEndpoint: loremEndpoint, 21 | } 22 | } -------------------------------------------------------------------------------- /lorem-grpc/client/cmd/client_grpc_main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "time" 6 | "log" 7 | grpcClient "github.com/ru-rocker/gokit-playground/lorem-grpc/client" 8 | "google.golang.org/grpc" 9 | "golang.org/x/net/context" 10 | "github.com/ru-rocker/gokit-playground/lorem-grpc" 11 | "fmt" 12 | "strconv" 13 | ) 14 | 15 | func main() { 16 | var ( 17 | grpcAddr = flag.String("addr", ":8081", 18 | "gRPC address") 19 | ) 20 | flag.Parse() 21 | ctx := context.Background() 22 | conn, err := grpc.Dial(*grpcAddr, grpc.WithInsecure(), 23 | grpc.WithTimeout(1*time.Second)) 24 | 25 | if err != nil { 26 | log.Fatalln("gRPC dial:", err) 27 | } 28 | defer conn.Close() 29 | 30 | loremService := grpcClient.New(conn) 31 | args := flag.Args() 32 | var cmd string 33 | cmd, args = pop(args) 34 | 35 | switch cmd { 36 | case "lorem": 37 | var requestType, minStr, maxStr string 38 | 39 | requestType, args = pop(args) 40 | minStr, args = pop(args) 41 | maxStr, args = pop(args) 42 | 43 | min, _ := strconv.Atoi(minStr) 44 | max, _ := strconv.Atoi(maxStr) 45 | lorem(ctx, loremService, requestType, min, max) 46 | default: 47 | log.Fatalln("unknown command", cmd) 48 | } 49 | } 50 | 51 | // parse command line argument one by one 52 | func pop(s []string) (string, []string) { 53 | if len(s) == 0 { 54 | return "", s 55 | } 56 | return s[0], s[1:] 57 | } 58 | 59 | // call lorem service 60 | func lorem(ctx context.Context, service lorem_grpc.Service, requestType string, min int, max int) { 61 | mesg, err := service.Lorem(ctx, requestType, min, max) 62 | if err != nil { 63 | log.Fatalln(err.Error()) 64 | } 65 | fmt.Println(mesg) 66 | } -------------------------------------------------------------------------------- /lorem-grpc/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem_grpc 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "errors" 6 | "context" 7 | ) 8 | 9 | 10 | //request 11 | type LoremRequest struct { 12 | RequestType string 13 | Min int32 14 | Max int32 15 | } 16 | 17 | //response 18 | type LoremResponse struct { 19 | Message string `json:"message"` 20 | Err string `json:"err,omitempty"` 21 | } 22 | 23 | // endpoints wrapper 24 | type Endpoints struct { 25 | LoremEndpoint endpoint.Endpoint 26 | } 27 | 28 | // creating Lorem Ipsum Endpoint 29 | func MakeLoremEndpoint(svc Service) endpoint.Endpoint { 30 | return func(ctx context.Context, request interface{}) (interface{}, error) { 31 | req := request.(LoremRequest) 32 | 33 | var ( 34 | min, max int 35 | ) 36 | 37 | min = int(req.Min) 38 | max = int(req.Max) 39 | txt, err := svc.Lorem(ctx, req.RequestType, min, max) 40 | 41 | if err != nil { 42 | return nil, err 43 | } 44 | 45 | return LoremResponse{Message: txt}, nil 46 | } 47 | 48 | } 49 | 50 | // Wrapping Endpoints as a Service implementation. 51 | // Will be used in gRPC client 52 | func (e Endpoints) Lorem(ctx context.Context, requestType string, min, max int) (string, error) { 53 | req := LoremRequest{ 54 | RequestType: requestType, 55 | Min: int32(min), 56 | Max: int32(max), 57 | } 58 | resp, err := e.LoremEndpoint(ctx, req) 59 | if err != nil { 60 | return "", err 61 | } 62 | loremResp := resp.(LoremResponse) 63 | if loremResp.Err != "" { 64 | return "", errors.New(loremResp.Err) 65 | } 66 | return loremResp.Message, nil 67 | } -------------------------------------------------------------------------------- /lorem-grpc/model.go: -------------------------------------------------------------------------------- 1 | package lorem_grpc 2 | 3 | import ( 4 | "github.com/ru-rocker/gokit-playground/lorem-grpc/pb" 5 | "context" 6 | ) 7 | 8 | //Encode and Decode Lorem Request 9 | func EncodeGRPCLoremRequest(_ context.Context, r interface{}) (interface{}, error) { 10 | req := r.(LoremRequest) 11 | return &pb.LoremRequest{ 12 | RequestType: req.RequestType, 13 | Max: req.Max, 14 | Min: req.Min, 15 | } , nil 16 | } 17 | 18 | func DecodeGRPCLoremRequest(_ context.Context, r interface{}) (interface{}, error) { 19 | req := r.(*pb.LoremRequest) 20 | return LoremRequest{ 21 | RequestType: req.RequestType, 22 | Max: req.Max, 23 | Min: req.Min, 24 | }, nil 25 | } 26 | 27 | // Encode and Decode Lorem Response 28 | func EncodeGRPCLoremResponse(_ context.Context, r interface{}) (interface{}, error) { 29 | resp := r.(LoremResponse) 30 | return &pb.LoremResponse{ 31 | Message: resp.Message, 32 | Err: resp.Err, 33 | }, nil 34 | } 35 | 36 | func DecodeGRPCLoremResponse(_ context.Context, r interface{}) (interface{}, error) { 37 | resp := r.(*pb.LoremResponse) 38 | return LoremResponse{ 39 | Message: resp.Message, 40 | Err: resp.Err, 41 | }, nil 42 | } -------------------------------------------------------------------------------- /lorem-grpc/pb/lorem.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: lorem.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package pb is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | lorem.proto 10 | 11 | It has these top-level messages: 12 | LoremRequest 13 | LoremResponse 14 | */ 15 | package pb 16 | 17 | import proto "github.com/golang/protobuf/proto" 18 | import fmt "fmt" 19 | import math "math" 20 | 21 | import ( 22 | context "golang.org/x/net/context" 23 | grpc "google.golang.org/grpc" 24 | ) 25 | 26 | // Reference imports to suppress errors if they are not otherwise used. 27 | var _ = proto.Marshal 28 | var _ = fmt.Errorf 29 | var _ = math.Inf 30 | 31 | // This is a compile-time assertion to ensure that this generated file 32 | // is compatible with the proto package it is being compiled against. 33 | // A compilation error at this line likely means your copy of the 34 | // proto package needs to be updated. 35 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 36 | 37 | type LoremRequest struct { 38 | RequestType string `protobuf:"bytes,1,opt,name=requestType" json:"requestType,omitempty"` 39 | Min int32 `protobuf:"varint,2,opt,name=min" json:"min,omitempty"` 40 | Max int32 `protobuf:"varint,3,opt,name=max" json:"max,omitempty"` 41 | } 42 | 43 | func (m *LoremRequest) Reset() { *m = LoremRequest{} } 44 | func (m *LoremRequest) String() string { return proto.CompactTextString(m) } 45 | func (*LoremRequest) ProtoMessage() {} 46 | func (*LoremRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 47 | 48 | func (m *LoremRequest) GetRequestType() string { 49 | if m != nil { 50 | return m.RequestType 51 | } 52 | return "" 53 | } 54 | 55 | func (m *LoremRequest) GetMin() int32 { 56 | if m != nil { 57 | return m.Min 58 | } 59 | return 0 60 | } 61 | 62 | func (m *LoremRequest) GetMax() int32 { 63 | if m != nil { 64 | return m.Max 65 | } 66 | return 0 67 | } 68 | 69 | type LoremResponse struct { 70 | Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` 71 | Err string `protobuf:"bytes,2,opt,name=err" json:"err,omitempty"` 72 | } 73 | 74 | func (m *LoremResponse) Reset() { *m = LoremResponse{} } 75 | func (m *LoremResponse) String() string { return proto.CompactTextString(m) } 76 | func (*LoremResponse) ProtoMessage() {} 77 | func (*LoremResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } 78 | 79 | func (m *LoremResponse) GetMessage() string { 80 | if m != nil { 81 | return m.Message 82 | } 83 | return "" 84 | } 85 | 86 | func (m *LoremResponse) GetErr() string { 87 | if m != nil { 88 | return m.Err 89 | } 90 | return "" 91 | } 92 | 93 | func init() { 94 | proto.RegisterType((*LoremRequest)(nil), "pb.LoremRequest") 95 | proto.RegisterType((*LoremResponse)(nil), "pb.LoremResponse") 96 | } 97 | 98 | // Reference imports to suppress errors if they are not otherwise used. 99 | var _ context.Context 100 | var _ grpc.ClientConn 101 | 102 | // This is a compile-time assertion to ensure that this generated file 103 | // is compatible with the grpc package it is being compiled against. 104 | const _ = grpc.SupportPackageIsVersion4 105 | 106 | // Client API for Lorem service 107 | 108 | type LoremClient interface { 109 | Lorem(ctx context.Context, in *LoremRequest, opts ...grpc.CallOption) (*LoremResponse, error) 110 | } 111 | 112 | type loremClient struct { 113 | cc *grpc.ClientConn 114 | } 115 | 116 | func NewLoremClient(cc *grpc.ClientConn) LoremClient { 117 | return &loremClient{cc} 118 | } 119 | 120 | func (c *loremClient) Lorem(ctx context.Context, in *LoremRequest, opts ...grpc.CallOption) (*LoremResponse, error) { 121 | out := new(LoremResponse) 122 | err := grpc.Invoke(ctx, "/pb.Lorem/Lorem", in, out, c.cc, opts...) 123 | if err != nil { 124 | return nil, err 125 | } 126 | return out, nil 127 | } 128 | 129 | // Server API for Lorem service 130 | 131 | type LoremServer interface { 132 | Lorem(context.Context, *LoremRequest) (*LoremResponse, error) 133 | } 134 | 135 | func RegisterLoremServer(s *grpc.Server, srv LoremServer) { 136 | s.RegisterService(&_Lorem_serviceDesc, srv) 137 | } 138 | 139 | func _Lorem_Lorem_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 140 | in := new(LoremRequest) 141 | if err := dec(in); err != nil { 142 | return nil, err 143 | } 144 | if interceptor == nil { 145 | return srv.(LoremServer).Lorem(ctx, in) 146 | } 147 | info := &grpc.UnaryServerInfo{ 148 | Server: srv, 149 | FullMethod: "/pb.Lorem/Lorem", 150 | } 151 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 152 | return srv.(LoremServer).Lorem(ctx, req.(*LoremRequest)) 153 | } 154 | return interceptor(ctx, in, info, handler) 155 | } 156 | 157 | var _Lorem_serviceDesc = grpc.ServiceDesc{ 158 | ServiceName: "pb.Lorem", 159 | HandlerType: (*LoremServer)(nil), 160 | Methods: []grpc.MethodDesc{ 161 | { 162 | MethodName: "Lorem", 163 | Handler: _Lorem_Lorem_Handler, 164 | }, 165 | }, 166 | Streams: []grpc.StreamDesc{}, 167 | Metadata: "lorem.proto", 168 | } 169 | 170 | func init() { proto.RegisterFile("lorem.proto", fileDescriptor0) } 171 | 172 | var fileDescriptor0 = []byte{ 173 | // 168 bytes of a gzipped FileDescriptorProto 174 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0xc9, 0x2f, 0x4a, 175 | 0xcd, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x2a, 0x48, 0x52, 0x0a, 0xe1, 0xe2, 0xf1, 176 | 0x01, 0x09, 0x05, 0xa5, 0x16, 0x96, 0xa6, 0x16, 0x97, 0x08, 0x29, 0x70, 0x71, 0x17, 0x41, 0x98, 177 | 0x21, 0x95, 0x05, 0xa9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0xc8, 0x42, 0x42, 0x02, 0x5c, 178 | 0xcc, 0xb9, 0x99, 0x79, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0xac, 0x41, 0x20, 0x26, 0x58, 0x24, 0xb1, 179 | 0x42, 0x82, 0x19, 0x2a, 0x92, 0x58, 0xa1, 0x64, 0xcd, 0xc5, 0x0b, 0x35, 0xb5, 0xb8, 0x20, 0x3f, 180 | 0xaf, 0x38, 0x55, 0x48, 0x82, 0x8b, 0x3d, 0x37, 0xb5, 0xb8, 0x38, 0x31, 0x1d, 0x66, 0x24, 0x8c, 181 | 0x0b, 0xd2, 0x9c, 0x5a, 0x54, 0x04, 0x36, 0x8e, 0x33, 0x08, 0xc4, 0x34, 0x32, 0xe7, 0x62, 0x05, 182 | 0x6b, 0x16, 0xd2, 0x83, 0x31, 0x04, 0xf4, 0x0a, 0x92, 0xf4, 0x90, 0x9d, 0x29, 0x25, 0x88, 0x24, 183 | 0x02, 0xb1, 0x42, 0x89, 0x21, 0x89, 0x0d, 0xec, 0x2d, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 184 | 0x15, 0xb5, 0xf1, 0x5e, 0xe5, 0x00, 0x00, 0x00, 185 | } 186 | -------------------------------------------------------------------------------- /lorem-grpc/pb/lorem.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package pb; 3 | 4 | service Lorem { 5 | rpc Lorem(LoremRequest) returns (LoremResponse) {} 6 | } 7 | 8 | message LoremRequest { 9 | string requestType = 1; 10 | int32 min = 2; 11 | int32 max = 3; 12 | } 13 | 14 | message LoremResponse { 15 | string message = 1; 16 | string err = 2; 17 | } -------------------------------------------------------------------------------- /lorem-grpc/server/server_grpc_main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | "flag" 6 | "github.com/ru-rocker/gokit-playground/lorem-grpc" 7 | context "golang.org/x/net/context" 8 | "google.golang.org/grpc" 9 | "github.com/ru-rocker/gokit-playground/lorem-grpc/pb" 10 | "os" 11 | "os/signal" 12 | "syscall" 13 | "fmt" 14 | ) 15 | 16 | func main() { 17 | 18 | var ( 19 | gRPCAddr = flag.String("grpc", ":8081", 20 | "gRPC listen address") 21 | ) 22 | flag.Parse() 23 | ctx := context.Background() 24 | 25 | // init lorem service 26 | var svc lorem_grpc.Service 27 | svc = lorem_grpc.LoremService{} 28 | errChan := make(chan error) 29 | 30 | // creating Endpoints struct 31 | endpoints := lorem_grpc.Endpoints{ 32 | LoremEndpoint: lorem_grpc.MakeLoremEndpoint(svc), 33 | } 34 | 35 | //execute grpc server 36 | go func() { 37 | listener, err := net.Listen("tcp", *gRPCAddr) 38 | if err != nil { 39 | errChan <- err 40 | return 41 | } 42 | handler := lorem_grpc.NewGRPCServer(ctx, endpoints) 43 | gRPCServer := grpc.NewServer() 44 | pb.RegisterLoremServer(gRPCServer, handler) 45 | errChan <- gRPCServer.Serve(listener) 46 | }() 47 | 48 | go func() { 49 | c := make(chan os.Signal, 1) 50 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 51 | errChan <- fmt.Errorf("%s", <-c) 52 | }() 53 | 54 | go func() { 55 | c := make(chan os.Signal, 1) 56 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 57 | errChan <- fmt.Errorf("%s", <-c) 58 | }() 59 | fmt.Println(<- errChan) 60 | } -------------------------------------------------------------------------------- /lorem-grpc/service.go: -------------------------------------------------------------------------------- 1 | package lorem_grpc 2 | 3 | import ( 4 | gl "github.com/drhodes/golorem" 5 | "strings" 6 | "errors" 7 | "context" 8 | ) 9 | 10 | var ( 11 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 12 | ) 13 | 14 | // Define service interface 15 | type Service interface { 16 | // generate a word with at least min letters and at most max letters. 17 | Lorem(ctx context.Context, requestType string, min, max int) (string, error) 18 | } 19 | 20 | // Implement service with empty struct 21 | type LoremService struct { 22 | 23 | } 24 | 25 | // Implement service functions 26 | func (LoremService) Lorem(_ context.Context, requestType string, min, max int) (string, error) { 27 | var result string 28 | var err error 29 | if strings.EqualFold(requestType, "Word") { 30 | result = gl.Word(min, max) 31 | } else if strings.EqualFold(requestType, "Sentence") { 32 | result = gl.Sentence(min, max) 33 | } else if strings.EqualFold(requestType, "Paragraph") { 34 | result = gl.Paragraph(min, max) 35 | } else { 36 | err = ErrRequestTypeNotFound 37 | } 38 | return result, err 39 | } -------------------------------------------------------------------------------- /lorem-grpc/transport.go: -------------------------------------------------------------------------------- 1 | package lorem_grpc 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | grpctransport "github.com/go-kit/kit/transport/grpc" 6 | "github.com/ru-rocker/gokit-playground/lorem-grpc/pb" 7 | ) 8 | 9 | type grpcServer struct { 10 | lorem grpctransport.Handler 11 | } 12 | 13 | // implement LoremServer Interface in lorem.pb.go 14 | func (s *grpcServer) Lorem(ctx context.Context, r *pb.LoremRequest) (*pb.LoremResponse, error) { 15 | _, resp, err := s.lorem.ServeGRPC(ctx, r) 16 | if err != nil { 17 | return nil, err 18 | } 19 | return resp.(*pb.LoremResponse), nil 20 | } 21 | 22 | // create new grpc server 23 | func NewGRPCServer(_ context.Context, endpoint Endpoints) pb.LoremServer { 24 | return &grpcServer{ 25 | lorem: grpctransport.NewServer( 26 | endpoint.LoremEndpoint, 27 | DecodeGRPCLoremRequest, 28 | EncodeGRPCLoremResponse, 29 | ), 30 | } 31 | } -------------------------------------------------------------------------------- /lorem-hystrix/README.md: -------------------------------------------------------------------------------- 1 | # lorem-hystrix 2 | This is simple service module. Only for showing the micro service with HTTP and return json. 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | In this part I will demonstrate the circuit breaker patter. I copied from `lorem-consul`. 6 | 7 | I am fully using all three functions from the golorem library. 8 | 9 | ## Required libraries 10 | 11 | go get github.com/go-kit/kit 12 | go get github.com/drhodes/golorem 13 | go get github.com/gorilla/mux 14 | go get github.com/juju/ratelimit 15 | go get github.com/prometheus/client_golang/prometheus 16 | go get github.com/go-kit/kit/sd/consul 17 | go get github.com/afex/hystrix-go/hystrix 18 | 19 | ### service.go 20 | Business logic will be put here 21 | 22 | ### endpoint.go 23 | Endpoint will be created here 24 | 25 | ### transport.go 26 | Handling about encode and decode json 27 | 28 | ### logging.go 29 | Logging function is under this file 30 | 31 | ### instrument.go 32 | Middleware function. 33 | For this sample, this function for rate limiting and metrics. 34 | 35 | ### discovery.go 36 | Consul service registration utility 37 | 38 | #### lorem-consul.d 39 | Go main function to build service and register to consul 40 | 41 | #### discover.d 42 | Go main function for discover service 43 | 44 | ### Running Consul 45 | 46 | docker run --rm -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap -ui-dir /ui 47 | 48 | ### Running hystrix dashboard 49 | The dashboard is running on http://localhost:8181/hystrix 50 | 51 | docker run -p 8181:9002 --name hystrix-dashboard mlabouardy/hystrix-dashboard:latest 52 | 53 | ### Running Prometheus and Grafana 54 | To execute type 55 | 56 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 57 | docker-compose -f docker/docker-compose-prometheus-grafana-consul.yml up -d 58 | 59 | ### execute 60 | 61 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 62 | go run lorem-hystrix/lorem-hystrix.d/main.go -consul.addr localhost -consul.port 8500 -advertise.addr 192.168.1.103 -advertise.port 7002 63 | go run lorem-hystrix/discover.d/main.go -consul.addr localhost -consul.port 8500 64 | 65 | ###### execute request in forever loop 66 | 67 | while true; do curl -XPOST -d'{"requestType":"word", "min":10, "max":10}' http://localhost:8080/sd-lorem; sleep 1; done; 68 | while true; do curl -XPOST -d'{"requestType":"sentence", "min":10, "max":10}' http://localhost:8080/sd-lorem; sleep 1; done; 69 | while true; do curl -XPOST -d'{"requestType":"paragraph", "min":10, "max":10}' http://localhost:8080/sd-lorem; sleep 1; done; -------------------------------------------------------------------------------- /lorem-hystrix/circuitbreaker.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | "context" 5 | "github.com/go-kit/kit/log" 6 | "github.com/afex/hystrix-go/hystrix" 7 | "github.com/go-kit/kit/endpoint" 8 | ) 9 | 10 | func Hystrix(commandName string, fallbackMesg string, logger log.Logger) endpoint.Middleware { 11 | return func(next endpoint.Endpoint) endpoint.Endpoint { 12 | return func(ctx context.Context, request interface{}) (response interface{}, err error) { 13 | var resp interface{} 14 | if err := hystrix.Do(commandName, func() (err error) { 15 | resp, err = next(ctx, request) 16 | return err 17 | }, func(err error) error { 18 | logger.Log("fallbackErrorDesc", err.Error()) 19 | resp = struct { 20 | Fallback string `json:"fallback"` 21 | }{ 22 | fallbackMesg, 23 | } 24 | return nil 25 | }); err != nil { 26 | return nil, err 27 | } 28 | return resp, nil 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /lorem-hystrix/discover.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/go-kit/kit/sd" 5 | "github.com/go-kit/kit/endpoint" 6 | "io" 7 | "strings" 8 | "net/url" 9 | "net/http" 10 | "context" 11 | ht "github.com/go-kit/kit/transport/http" 12 | consulsd "github.com/go-kit/kit/sd/consul" 13 | "os" 14 | "github.com/go-kit/kit/log" 15 | "github.com/hashicorp/consul/api" 16 | "github.com/go-kit/kit/sd/lb" 17 | "time" 18 | "github.com/gorilla/mux" 19 | "os/signal" 20 | "syscall" 21 | "fmt" 22 | "flag" 23 | "encoding/json" 24 | "strconv" 25 | "errors" 26 | "github.com/ru-rocker/gokit-playground/lorem-hystrix" 27 | "github.com/afex/hystrix-go/hystrix" 28 | "net" 29 | ) 30 | 31 | //to execute: go run src/github.com/ru-rocker/gokit-playground/lorem-consul/discover.d/main.go -consul.addr localhost -consul.port 8500 32 | // curl -XPOST -d'{"requestType":"word", "min":10, "max":10}' http://localhost:8080/sd-lorem 33 | func main() { 34 | 35 | var ( 36 | consulAddr = flag.String("consul.addr", "", "consul address") 37 | consulPort = flag.String("consul.port", "", "consul port") 38 | ) 39 | flag.Parse() 40 | 41 | // Logging domain. 42 | var logger log.Logger 43 | { 44 | logger = log.NewLogfmtLogger(os.Stdout) 45 | logger = log.With(logger,"ts", log.DefaultTimestampUTC) 46 | logger = log.With(logger,"caller", log.DefaultCaller) 47 | } 48 | 49 | 50 | // Service discovery domain. In this example we use Consul. 51 | var client consulsd.Client 52 | { 53 | consulConfig := api.DefaultConfig() 54 | 55 | consulConfig.Address = "http://" + *consulAddr + ":" + *consulPort 56 | consulClient, err := api.NewClient(consulConfig) 57 | if err != nil { 58 | logger.Log("err", err) 59 | os.Exit(1) 60 | } 61 | client = consulsd.NewClient(consulClient) 62 | } 63 | 64 | tags := []string{"lorem", "ru-rocker"} 65 | passingOnly := true 66 | duration := 500 * time.Millisecond 67 | var loremEndpoint endpoint.Endpoint 68 | 69 | ctx := context.Background() 70 | r := mux.NewRouter() 71 | 72 | factory := loremFactory(ctx, "POST", "/lorem") 73 | serviceName := "lorem" 74 | instancer := consulsd.NewInstancer(client, logger, serviceName, tags, passingOnly) 75 | endpointer := sd.NewEndpointer(instancer, factory, logger) 76 | balancer := lb.NewRoundRobin(endpointer) 77 | retry := lb.Retry(1, duration, balancer) 78 | loremEndpoint = retry 79 | 80 | // configure hystrix 81 | hystrix.ConfigureCommand("Lorem Request", hystrix.CommandConfig{Timeout: 1000}) 82 | loremEndpoint = lorem_hystrix.Hystrix("Lorem Request", "Service currently unavailable", logger)(loremEndpoint) 83 | 84 | // POST /sd-lorem 85 | // Payload: {"requestType":"word", "min":10, "max":10} 86 | r.Methods("POST").Path("/sd-lorem").Handler(ht.NewServer( 87 | loremEndpoint, 88 | decodeConsulLoremRequest, 89 | lorem_hystrix.EncodeResponse, // use existing encode response since I did not change the logic on response 90 | )) 91 | 92 | // Interrupt handler. 93 | errc := make(chan error) 94 | 95 | // configure the hystrix stream handler 96 | hystrixStreamHandler := hystrix.NewStreamHandler() 97 | hystrixStreamHandler.Start() 98 | go func() { 99 | errc <- http.ListenAndServe(net.JoinHostPort("", "9000"), hystrixStreamHandler) 100 | }() 101 | 102 | go func() { 103 | c := make(chan os.Signal) 104 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 105 | errc <- fmt.Errorf("%s", <-c) 106 | }() 107 | 108 | // HTTP transport. 109 | go func() { 110 | logger.Log("transport", "HTTP", "addr", "8080") 111 | errc <- http.ListenAndServe(":8080", r) 112 | }() 113 | 114 | // Run! 115 | logger.Log("exit", <-errc) 116 | } 117 | 118 | // factory function to parse URL from Consul to Endpoint 119 | func loremFactory(_ context.Context, method, path string) sd.Factory { 120 | return func(instance string) (endpoint.Endpoint, io.Closer, error) { 121 | if !strings.HasPrefix(instance, "http") { 122 | instance = "http://" + instance 123 | } 124 | 125 | tgt, err := url.Parse(instance) 126 | if err != nil { 127 | return nil, nil, err 128 | } 129 | tgt.Path = path 130 | 131 | var ( 132 | enc ht.EncodeRequestFunc 133 | dec ht.DecodeResponseFunc 134 | ) 135 | enc, dec = encodeLoremRequest, decodeLoremResponse 136 | 137 | return ht.NewClient(method, tgt, enc, dec).Endpoint(), nil, nil 138 | } 139 | } 140 | 141 | // decode request from client (/sd-lorem) 142 | // parsing JSON into LoremRequest 143 | func decodeConsulLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 144 | var request lorem_hystrix.LoremRequest 145 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { 146 | return nil, err 147 | } 148 | return request, nil 149 | } 150 | 151 | // Encode request form LoremRequest into existing Lorem Service 152 | // The encode will translate the LoremRequest into /lorem/{requestType}/{min}/{max} 153 | func encodeLoremRequest(_ context.Context, req *http.Request, request interface{}) error { 154 | 155 | lr := request.(lorem_hystrix.LoremRequest) 156 | p := "/" + lr.RequestType + "/" + strconv.Itoa(lr.Min) + "/" + strconv.Itoa(lr.Max) 157 | req.URL.Path += p 158 | return nil 159 | } 160 | 161 | // decode response from Lorem Service 162 | func decodeLoremResponse(_ context.Context, resp *http.Response) (interface{}, error) { 163 | 164 | var response lorem_hystrix.LoremResponse 165 | var s map[string]interface{} 166 | 167 | if respCode := resp.StatusCode; respCode >= 400 { 168 | if err := json.NewDecoder(resp.Body).Decode(&s); err != nil{ 169 | return nil, err 170 | } 171 | return nil, errors.New(s["error"].(string) + "\n") 172 | } 173 | 174 | if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { 175 | return nil, err 176 | } 177 | 178 | return response, nil 179 | } -------------------------------------------------------------------------------- /lorem-hystrix/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "strings" 6 | "errors" 7 | "context" 8 | ) 9 | 10 | var ( 11 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 12 | ) 13 | 14 | //Lorem Request 15 | type LoremRequest struct { 16 | RequestType string `json:"requestType"` 17 | Min int `json:"min"` 18 | Max int `json:"max"` 19 | } 20 | 21 | //Lorem Response 22 | type LoremResponse struct { 23 | Message string `json:"message"` 24 | Err error `json:"err,omitempty"` 25 | } 26 | 27 | //Health Request 28 | type HealthRequest struct { 29 | 30 | } 31 | 32 | //Health Response 33 | type HealthResponse struct { 34 | Status bool `json:"status"` 35 | } 36 | 37 | // endpoints wrapper 38 | type Endpoints struct { 39 | LoremEndpoint endpoint.Endpoint 40 | HealthEndpoint endpoint.Endpoint 41 | } 42 | 43 | // creating Lorem Ipsum Endpoint 44 | func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint { 45 | return func(ctx context.Context, request interface{}) (interface{}, error) { 46 | req := request.(LoremRequest) 47 | 48 | var ( 49 | txt string 50 | min, max int 51 | ) 52 | 53 | min = req.Min 54 | max = req.Max 55 | 56 | if strings.EqualFold(req.RequestType, "Word") { 57 | txt = svc.Word(min, max) 58 | } else if strings.EqualFold(req.RequestType, "Sentence"){ 59 | txt = svc.Sentence(min, max) 60 | } else if strings.EqualFold(req.RequestType, "Paragraph") { 61 | txt = svc.Paragraph(min, max) 62 | } else { 63 | return nil, ErrRequestTypeNotFound 64 | } 65 | return LoremResponse{Message: txt}, nil 66 | } 67 | 68 | } 69 | 70 | // creating health endpoint 71 | func MakeHealthEndpoint(svc Service) endpoint.Endpoint { 72 | return func(ctx context.Context, request interface{}) (interface{}, error) { 73 | status := svc.HealthCheck() 74 | return HealthResponse{Status: status }, nil 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /lorem-hystrix/instrument.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | "github.com/go-kit/kit/metrics" 5 | "time" 6 | ) 7 | 8 | func Metrics(requestCount metrics.Counter, 9 | requestLatency metrics.Histogram) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return metricsMiddleware{ 12 | next, 13 | requestCount, 14 | requestLatency, 15 | } 16 | } 17 | } 18 | 19 | // Make a new type and wrap into Service interface 20 | // Add expected metrics property to this type 21 | type metricsMiddleware struct { 22 | Service 23 | requestCount metrics.Counter 24 | requestLatency metrics.Histogram 25 | } 26 | 27 | // Implement service functions and add label method for our metrics 28 | func (mw metricsMiddleware) Word(min, max int) (output string) { 29 | defer func(begin time.Time) { 30 | lvs := []string{"method", "Word"} 31 | mw.requestCount.With(lvs...).Add(1) 32 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 33 | }(time.Now()) 34 | output = mw.Service.Word(min, max) 35 | return 36 | } 37 | 38 | func (mw metricsMiddleware) Sentence(min, max int) (output string) { 39 | defer func(begin time.Time) { 40 | lvs := []string{"method", "Sentence"} 41 | mw.requestCount.With(lvs...).Add(1) 42 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 43 | }(time.Now()) 44 | output = mw.Service.Sentence(min, max) 45 | return 46 | } 47 | 48 | func (mw metricsMiddleware) Paragraph(min, max int) (output string) { 49 | defer func(begin time.Time) { 50 | lvs := []string{"method", "Paragraph"} 51 | mw.requestCount.With(lvs...).Add(1) 52 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 53 | }(time.Now()) 54 | output = mw.Service.Paragraph(min, max) 55 | return 56 | } 57 | 58 | func (mw metricsMiddleware) HealthCheck() (output bool) { 59 | defer func(begin time.Time) { 60 | lvs := []string{"method", "HealthCheck"} 61 | mw.requestCount.With(lvs...).Add(1) 62 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 63 | }(time.Now()) 64 | output = mw.Service.HealthCheck() 65 | return 66 | } -------------------------------------------------------------------------------- /lorem-hystrix/logging.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | "github.com/go-kit/kit/log" 5 | "time" 6 | ) 7 | 8 | // implement function to return ServiceMiddleware 9 | func LoggingMiddleware(logger log.Logger) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return loggingMiddleware{next, logger} 12 | } 13 | } 14 | 15 | // Make a new type and wrap into Service interface 16 | // Add logger property to this type 17 | type loggingMiddleware struct { 18 | Service 19 | logger log.Logger 20 | } 21 | 22 | // Implement Service Interface for LoggingMiddleware 23 | func (mw loggingMiddleware) Word(min, max int) (output string) { 24 | defer func(begin time.Time){ 25 | mw.logger.Log( 26 | "function","Word", 27 | "min", min, 28 | "max", max, 29 | "result", output, 30 | "took", time.Since(begin), 31 | ) 32 | }(time.Now()) 33 | output = mw.Service.Word(min,max) 34 | return 35 | } 36 | 37 | func (mw loggingMiddleware) Sentence(min, max int) (output string) { 38 | defer func(begin time.Time){ 39 | mw.logger.Log( 40 | "function","Sentence", 41 | "min", min, 42 | "max", max, 43 | "result", output, 44 | "took", time.Since(begin), 45 | ) 46 | }(time.Now()) 47 | output = mw.Service.Sentence(min,max) 48 | return 49 | } 50 | 51 | func (mw loggingMiddleware) Paragraph(min, max int) (output string) { 52 | defer func(begin time.Time){ 53 | mw.logger.Log( 54 | "function","Paragraph", 55 | "min", min, 56 | "max", max, 57 | "result", output, 58 | "took", time.Since(begin), 59 | ) 60 | }(time.Now()) 61 | output = mw.Service.Paragraph(min,max) 62 | return 63 | } 64 | 65 | func (mw loggingMiddleware) HealthCheck() (output bool) { 66 | defer func(begin time.Time){ 67 | mw.logger.Log( 68 | "function","HealthCheck", 69 | "result", output, 70 | "took", time.Since(begin), 71 | ) 72 | }(time.Now()) 73 | output = mw.Service.HealthCheck() 74 | return 75 | } -------------------------------------------------------------------------------- /lorem-hystrix/lorem-hystrix.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | "os" 6 | "os/signal" 7 | "syscall" 8 | "net/http" 9 | "fmt" 10 | "github.com/go-kit/kit/log" 11 | ratelimitkit "github.com/go-kit/kit/ratelimit" 12 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" 13 | stdprometheus "github.com/prometheus/client_golang/prometheus" 14 | "time" 15 | "github.com/juju/ratelimit" 16 | "flag" 17 | ilog "log" 18 | "github.com/ru-rocker/gokit-playground/lorem-hystrix" 19 | ) 20 | 21 | func main() { 22 | 23 | // parse variable from input command 24 | var ( 25 | consulAddr = flag.String("consul.addr", "", "consul address") 26 | consulPort = flag.String("consul.port", "", "consul port") 27 | advertiseAddr = flag.String("advertise.addr", "", "advertise address") 28 | advertisePort = flag.String("advertise.port", "", "advertise port") 29 | ) 30 | flag.Parse() 31 | 32 | ctx := context.Background() 33 | errChan := make(chan error) 34 | 35 | // Logging domain. 36 | var logger log.Logger 37 | { 38 | logger = log.NewLogfmtLogger(os.Stdout) 39 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 40 | logger = log.With(logger, "caller", log.DefaultCaller) 41 | } 42 | 43 | //declare metrics 44 | fieldKeys := []string{"method"} 45 | requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ 46 | Namespace: "ru_rocker", 47 | Subsystem: "lorem_service", 48 | Name: "request_count", 49 | Help: "Number of requests received.", 50 | }, fieldKeys) 51 | requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ 52 | Namespace: "ru_rocker", 53 | Subsystem: "lorem_service", 54 | Name: "request_latency_microseconds", 55 | Help: "Total duration of requests in microseconds.", 56 | }, fieldKeys) 57 | 58 | var svc lorem_hystrix.Service 59 | svc = lorem_hystrix.LoremService{} 60 | svc = lorem_hystrix.LoggingMiddleware(logger)(svc) 61 | svc = lorem_hystrix.Metrics(requestCount, requestLatency)(svc) 62 | 63 | rlbucket := ratelimit.NewBucket(35*time.Millisecond, 100) 64 | e := lorem_hystrix.MakeLoremLoggingEndpoint(svc) 65 | e = ratelimitkit.NewTokenBucketThrottler(rlbucket, time.Sleep)(e) 66 | 67 | // Make health endpoint 68 | healthEndpoint := lorem_hystrix.MakeHealthEndpoint(svc) 69 | endpoint := lorem_hystrix.Endpoints{ 70 | LoremEndpoint: e, 71 | HealthEndpoint: healthEndpoint, 72 | } 73 | 74 | r := lorem_hystrix.MakeHttpHandler(ctx, endpoint, logger) 75 | 76 | // Register Service to Consul 77 | registar := lorem_hystrix.Register(*consulAddr, 78 | *consulPort, 79 | *advertiseAddr, 80 | *advertisePort) 81 | 82 | // HTTP transport 83 | go func() { 84 | ilog.Println("Starting server at port", *advertisePort) 85 | // register service 86 | registar.Register() 87 | handler := r 88 | errChan <- http.ListenAndServe(":" + *advertisePort, handler) 89 | }() 90 | 91 | 92 | go func() { 93 | c := make(chan os.Signal, 1) 94 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 95 | errChan <- fmt.Errorf("%s", <-c) 96 | }() 97 | error := <- errChan 98 | // deregister service 99 | registar.Deregister() 100 | ilog.Fatalln(error) 101 | } 102 | -------------------------------------------------------------------------------- /lorem-hystrix/prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s # By default, scrape targets every 15 seconds. 3 | 4 | # Attach these labels to any time series or alerts when communicating with 5 | # external systems (federation, remote storage, Alertmanager). 6 | external_labels: 7 | monitor: 'rurocker-monitor' 8 | 9 | # A scrape configuration containing exactly one endpoint to scrape: 10 | # Here it's Prometheus itself. 11 | scrape_configs: 12 | # The job name is added as a label `job=` to any timeseries scraped from this config. 13 | - job_name: 'prometheus' 14 | 15 | # Override the global default and scrape targets from this job every 5 seconds. 16 | scrape_interval: 5s 17 | 18 | static_configs: 19 | - targets: ['localhost:9090'] 20 | labels: 21 | group: 'local' 22 | 23 | - job_name: 'ru-rocker' 24 | 25 | scrape_interval: 5s 26 | 27 | consul_sd_configs: 28 | - server: '192.168.1.103:8500' 29 | 30 | relabel_configs: 31 | - source_labels: [__meta_consul_tags] 32 | regex: .*,lorem,.* 33 | action: keep 34 | - source_labels: [__meta_consul_service] 35 | target_label: job 36 | -------------------------------------------------------------------------------- /lorem-hystrix/register.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | consulsd "github.com/go-kit/kit/sd/consul" 5 | "github.com/go-kit/kit/log" 6 | "os" 7 | "github.com/hashicorp/consul/api" 8 | "github.com/go-kit/kit/sd" 9 | "math/rand" 10 | "strconv" 11 | "time" 12 | ) 13 | 14 | func Register(consulAddress string, 15 | consulPort string, 16 | advertiseAddress string, 17 | advertisePort string) (registar sd.Registrar) { 18 | 19 | // Logging domain. 20 | var logger log.Logger 21 | { 22 | logger = log.NewLogfmtLogger(os.Stderr) 23 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 24 | logger = log.With(logger, "caller", log.DefaultCaller) 25 | } 26 | 27 | rand.Seed(time.Now().UTC().UnixNano()) 28 | 29 | 30 | // Service discovery domain. In this example we use Consul. 31 | var client consulsd.Client 32 | { 33 | consulConfig := api.DefaultConfig() 34 | consulConfig.Address = consulAddress + ":" + consulPort 35 | consulClient, err := api.NewClient(consulConfig) 36 | if err != nil { 37 | logger.Log("err", err) 38 | os.Exit(1) 39 | } 40 | client = consulsd.NewClient(consulClient) 41 | } 42 | 43 | check := api.AgentServiceCheck{ 44 | HTTP: "http://" + advertiseAddress + ":" + advertisePort + "/health", 45 | Interval: "10s", 46 | Timeout: "1s", 47 | Notes: "Basic health checks", 48 | } 49 | 50 | port, _ := strconv.Atoi(advertisePort) 51 | num := rand.Intn(100) // to make service ID unique 52 | asr := api.AgentServiceRegistration{ 53 | ID: "lorem" + strconv.Itoa(num), //unique service ID 54 | Name: "lorem", 55 | Address: advertiseAddress, 56 | Port: port, 57 | Tags: []string{"lorem", "ru-rocker"}, 58 | Check: &check, 59 | } 60 | registar = consulsd.NewRegistrar(client, &asr, logger) 61 | return 62 | } 63 | -------------------------------------------------------------------------------- /lorem-hystrix/service.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | golorem "github.com/drhodes/golorem" 5 | ) 6 | 7 | // Define service interface 8 | type Service interface { 9 | // generate a word with at least min letters and at most max letters. 10 | Word(min, max int) string 11 | 12 | // generate a sentence with at least min words and at most max words. 13 | Sentence(min, max int) string 14 | 15 | // generate a paragraph with at least min sentences and at most max sentences. 16 | Paragraph(min, max int) string 17 | 18 | // health check 19 | HealthCheck() bool 20 | } 21 | 22 | // Implement service with empty struct 23 | type LoremService struct { 24 | 25 | } 26 | 27 | // create type that return function. 28 | // this will be needed in main.go 29 | type ServiceMiddleware func(Service) Service 30 | 31 | // Implement service functions 32 | func (LoremService) Word(min, max int) string { 33 | return golorem.Word(min, max) 34 | } 35 | 36 | func (LoremService) Sentence(min, max int) string { 37 | return golorem.Sentence(min, max) 38 | } 39 | 40 | func (LoremService) Paragraph(min, max int) string { 41 | return golorem.Paragraph(min, max) 42 | } 43 | 44 | func (LoremService) HealthCheck() bool { 45 | //to make the health check always return true is a bad idea 46 | //however, I did this on purpose to make the sample run easier. 47 | //Ideally, it should check things such as amount of free disk space or free memory 48 | return true 49 | } -------------------------------------------------------------------------------- /lorem-hystrix/transport.go: -------------------------------------------------------------------------------- 1 | package lorem_hystrix 2 | 3 | import ( 4 | "net/http" 5 | "errors" 6 | 7 | "github.com/gorilla/mux" 8 | "github.com/go-kit/kit/log" 9 | httptransport "github.com/go-kit/kit/transport/http" 10 | "encoding/json" 11 | "strconv" 12 | stdprometheus "github.com/prometheus/client_golang/prometheus/promhttp" 13 | "context" 14 | ) 15 | 16 | var ( 17 | // ErrBadRouting is returned when an expected path variable is missing. 18 | ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 19 | ) 20 | 21 | // Make Http Handler 22 | func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 23 | r := mux.NewRouter() 24 | options := []httptransport.ServerOption{ 25 | httptransport.ServerErrorLogger(logger), 26 | httptransport.ServerErrorEncoder(encodeError), 27 | } 28 | 29 | //POST /lorem/{type}/{min}/{max} 30 | r.Methods("POST").Path("/lorem/{type}/{min}/{max}").Handler(httptransport.NewServer( 31 | endpoint.LoremEndpoint, 32 | DecodeLoremRequest, 33 | EncodeResponse, 34 | options..., 35 | )) 36 | 37 | //GET /health 38 | r.Methods("GET").Path("/health").Handler(httptransport.NewServer( 39 | endpoint.HealthEndpoint, 40 | decodeHealthRequest, 41 | EncodeResponse, 42 | options..., 43 | )) 44 | 45 | // GET /metrics 46 | r.Path("/metrics").Handler(stdprometheus.Handler()) 47 | 48 | return r 49 | 50 | } 51 | 52 | // decode url path variables into request 53 | func DecodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 54 | vars := mux.Vars(r) 55 | requestType, ok := vars["type"] 56 | if !ok { 57 | return nil, ErrBadRouting 58 | } 59 | 60 | vmin, ok := vars["min"] 61 | if !ok { 62 | return nil, ErrBadRouting 63 | } 64 | 65 | vmax, ok := vars["max"] 66 | if !ok { 67 | return nil, ErrBadRouting 68 | } 69 | 70 | min, _ := strconv.Atoi(vmin) 71 | max, _ := strconv.Atoi(vmax) 72 | 73 | request := LoremRequest{ 74 | RequestType: requestType, 75 | Min: min, 76 | Max: max, 77 | } 78 | return request, nil 79 | } 80 | 81 | // decode health check 82 | func decodeHealthRequest(_ context.Context, _ *http.Request) (interface{}, error) { 83 | return HealthRequest{}, nil 84 | } 85 | 86 | 87 | // errorer is implemented by all concrete response types that may contain 88 | // errors. It allows us to change the HTTP response code without needing to 89 | // trigger an endpoint (transport-level) error. 90 | type errorer interface { 91 | error() error 92 | } 93 | 94 | // encodeResponse is the common method to encode all response types to the 95 | // client. 96 | func EncodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 97 | if e, ok := response.(errorer); ok && e.error() != nil { 98 | // Not a Go kit transport error, but a business-logic error. 99 | // Provide those as HTTP errors. 100 | encodeError(ctx, e.error(), w) 101 | return nil 102 | } 103 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 104 | return json.NewEncoder(w).Encode(response) 105 | } 106 | 107 | // encode error 108 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 109 | if err == nil { 110 | panic("encodeError with nil error") 111 | } 112 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 113 | w.WriteHeader(http.StatusInternalServerError) 114 | json.NewEncoder(w).Encode(map[string]interface{}{ 115 | "error": err.Error(), 116 | }) 117 | } -------------------------------------------------------------------------------- /lorem-logging/README.md: -------------------------------------------------------------------------------- 1 | # lorem-logging 2 | This is simple service module. Only for showing the micro service with HTTP and return json. 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | I am fully using all three functions from the golorem library. 6 | 7 | ## Required libraries 8 | 9 | go get github.com/go-kit/kit 10 | go get github.com/drhodes/golorem 11 | go get github.com/gorilla/mux 12 | 13 | 14 | ### service.go 15 | Business logic will be put here 16 | 17 | ### endpoint.go 18 | Endpoint will be created here 19 | 20 | ### transport.go 21 | Handling about encode and decode json 22 | 23 | ### logging.go 24 | Logging function is under this file 25 | 26 | #### lorem-logging.d 27 | Go main function will be located under this folder. The `dot d` means daemon. 28 | 29 | ### execute 30 | 31 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 32 | go run lorem-logging/lorem-logging.d/main.go 33 | 34 | ### Running Filebeat 35 | The filebeat is using docker-compose. 36 | To execute type 37 | 38 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 39 | docker-compose -f docker/docker-compose-filebeat.yml up 40 | 41 | *Notes: the log file is located under `$GOPATH/src/github.com/ru-rocker/gokit-playground/log/lorem/golorem.log`* -------------------------------------------------------------------------------- /lorem-logging/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem_logging 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "strings" 6 | "errors" 7 | "context" 8 | //"golang.org/x/net/context" 9 | ) 10 | 11 | var ( 12 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 13 | ) 14 | 15 | //request 16 | type LoremRequest struct { 17 | RequestType string 18 | Min int 19 | Max int 20 | } 21 | 22 | //response 23 | type LoremResponse struct { 24 | Message string `json:"message"` 25 | Err error `json:"err,omitempty"` 26 | } 27 | 28 | // endpoints wrapper 29 | type Endpoints struct { 30 | LoremEndpoint endpoint.Endpoint 31 | } 32 | 33 | // creating Lorem Ipsum Endpoint 34 | func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint { 35 | return func(ctx context.Context, request interface{}) (interface{}, error) { 36 | req := request.(LoremRequest) 37 | 38 | var ( 39 | txt string 40 | min, max int 41 | ) 42 | 43 | min = req.Min 44 | max = req.Max 45 | 46 | if strings.EqualFold(req.RequestType, "Word") { 47 | txt = svc.Word(min, max) 48 | } else if strings.EqualFold(req.RequestType, "Sentence"){ 49 | txt = svc.Sentence(min, max) 50 | } else if strings.EqualFold(req.RequestType, "Paragraph") { 51 | txt = svc.Paragraph(min, max) 52 | } else { 53 | return nil, ErrRequestTypeNotFound 54 | } 55 | 56 | return LoremResponse{Message: txt}, nil 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /lorem-logging/filebeat/filebeat.yml: -------------------------------------------------------------------------------- 1 | filebeat: 2 | prospectors: 3 | - 4 | paths: 5 | - "/golorem.log" 6 | input_type: log 7 | document_type: gokitlog 8 | 9 | output: 10 | logstash: 11 | enabled: true 12 | hosts: ["172.20.20.10:5044"] 13 | bulk_max_size: 1024 14 | console: 15 | pretty: true 16 | 17 | shipper: 18 | 19 | logging: 20 | files: 21 | rotateeverybytes: 10485760 # = 10MB 22 | -------------------------------------------------------------------------------- /lorem-logging/logging.go: -------------------------------------------------------------------------------- 1 | package lorem_logging 2 | 3 | import ( 4 | "github.com/go-kit/kit/log" 5 | "time" 6 | ) 7 | 8 | // implement function to return ServiceMiddleware 9 | func LoggingMiddleware(logger log.Logger) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return loggingMiddleware{next, logger} 12 | } 13 | } 14 | 15 | // Make a new type and wrap into Service interface 16 | // Add logger property to this type 17 | type loggingMiddleware struct { 18 | Service 19 | logger log.Logger 20 | } 21 | 22 | // Implement Service Interface for LoggingMiddleware 23 | func (mw loggingMiddleware) Word(min, max int) (output string) { 24 | defer func(begin time.Time){ 25 | mw.logger.Log( 26 | "function","Word", 27 | "min", min, 28 | "max", max, 29 | "result", output, 30 | "took", time.Since(begin), 31 | ) 32 | }(time.Now()) 33 | output = mw.Service.Word(min,max) 34 | return 35 | } 36 | 37 | func (mw loggingMiddleware) Sentence(min, max int) (output string) { 38 | defer func(begin time.Time){ 39 | mw.logger.Log( 40 | "function","Sentence", 41 | "min", min, 42 | "max", max, 43 | "result", output, 44 | "took", time.Since(begin), 45 | ) 46 | }(time.Now()) 47 | output = mw.Service.Sentence(min,max) 48 | return 49 | } 50 | 51 | func (mw loggingMiddleware) Paragraph(min, max int) (output string) { 52 | defer func(begin time.Time){ 53 | mw.logger.Log( 54 | "function","Paragraph", 55 | "min", min, 56 | "max", max, 57 | "result", output, 58 | "took", time.Since(begin), 59 | ) 60 | }(time.Now()) 61 | output = mw.Service.Paragraph(min,max) 62 | return 63 | } -------------------------------------------------------------------------------- /lorem-logging/lorem-logging.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | "os" 6 | "os/signal" 7 | "syscall" 8 | "net/http" 9 | "fmt" 10 | "github.com/ru-rocker/gokit-playground/lorem-logging" 11 | "github.com/go-kit/kit/log" 12 | ) 13 | 14 | func main() { 15 | ctx := context.Background() 16 | errChan := make(chan error) 17 | 18 | logfile, err := os.OpenFile("./log/lorem/golorem.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) 19 | if err != nil { 20 | panic(err) 21 | } 22 | defer logfile.Close() 23 | 24 | // Logging domain. 25 | var logger log.Logger 26 | { 27 | w := log.NewSyncWriter(logfile) 28 | logger = log.NewLogfmtLogger(w) 29 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 30 | logger = log.With(logger, "caller", log.DefaultCaller) 31 | } 32 | 33 | var svc lorem_logging.Service 34 | svc = lorem_logging.LoremService{} 35 | svc = lorem_logging.LoggingMiddleware(logger)(svc) 36 | endpoint := lorem_logging.Endpoints{ 37 | LoremEndpoint: lorem_logging.MakeLoremLoggingEndpoint(svc), 38 | } 39 | 40 | r := lorem_logging.MakeHttpHandler(ctx, endpoint, logger) 41 | 42 | // HTTP transport 43 | go func() { 44 | fmt.Println("Starting server at port 8080") 45 | handler := r 46 | errChan <- http.ListenAndServe(":8080", handler) 47 | }() 48 | 49 | 50 | go func() { 51 | c := make(chan os.Signal, 1) 52 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 53 | errChan <- fmt.Errorf("%s", <-c) 54 | }() 55 | fmt.Println(<- errChan) 56 | } 57 | -------------------------------------------------------------------------------- /lorem-logging/service.go: -------------------------------------------------------------------------------- 1 | package lorem_logging 2 | 3 | import ( 4 | golorem "github.com/drhodes/golorem" 5 | ) 6 | 7 | // Define service interface 8 | type Service interface { 9 | // generate a word with at least min letters and at most max letters. 10 | Word(min, max int) string 11 | 12 | // generate a sentence with at least min words and at most max words. 13 | Sentence(min, max int) string 14 | 15 | // generate a paragraph with at least min sentences and at most max sentences. 16 | Paragraph(min, max int) string 17 | } 18 | 19 | // Implement service with empty struct 20 | type LoremService struct { 21 | 22 | } 23 | 24 | // create type that return function. 25 | // this will be needed in main.go 26 | type ServiceMiddleware func(Service) Service 27 | 28 | // Implement service functions 29 | func (LoremService) Word(min, max int) string { 30 | return golorem.Word(min, max) 31 | } 32 | 33 | func (LoremService) Sentence(min, max int) string { 34 | return golorem.Sentence(min, max) 35 | } 36 | 37 | func (LoremService) Paragraph(min, max int) string { 38 | return golorem.Paragraph(min, max) 39 | } -------------------------------------------------------------------------------- /lorem-logging/transport.go: -------------------------------------------------------------------------------- 1 | package lorem_logging 2 | 3 | import ( 4 | "net/http" 5 | "errors" 6 | 7 | 8 | "github.com/gorilla/mux" 9 | "github.com/go-kit/kit/log" 10 | httptransport "github.com/go-kit/kit/transport/http" 11 | "encoding/json" 12 | "strconv" 13 | //"golang.org/x/net/context" 14 | "context" 15 | ) 16 | 17 | var ( 18 | // ErrBadRouting is returned when an expected path variable is missing. 19 | ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 20 | ) 21 | 22 | // Make Http Handler 23 | func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 24 | r := mux.NewRouter() 25 | options := []httptransport.ServerOption{ 26 | httptransport.ServerErrorLogger(logger), 27 | httptransport.ServerErrorEncoder(encodeError), 28 | } 29 | 30 | //POST /lorem/{type}/{min}/{max} 31 | r.Methods("POST").Path("/lorem/{type}/{min}/{max}").Handler(httptransport.NewServer( 32 | endpoint.LoremEndpoint, 33 | decodeLoremRequest, 34 | encodeResponse, 35 | options..., 36 | )) 37 | 38 | return r 39 | 40 | } 41 | 42 | // decode url path variables into request 43 | func decodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 44 | vars := mux.Vars(r) 45 | requestType, ok := vars["type"] 46 | if !ok { 47 | return nil, ErrBadRouting 48 | } 49 | 50 | vmin, ok := vars["min"] 51 | if !ok { 52 | return nil, ErrBadRouting 53 | } 54 | 55 | vmax, ok := vars["max"] 56 | if !ok { 57 | return nil, ErrBadRouting 58 | } 59 | 60 | min, _ := strconv.Atoi(vmin) 61 | max, _ := strconv.Atoi(vmax) 62 | return LoremRequest{ 63 | RequestType: requestType, 64 | Min: min, 65 | Max: max, 66 | }, nil 67 | } 68 | 69 | // errorer is implemented by all concrete response types that may contain 70 | // errors. It allows us to change the HTTP response code without needing to 71 | // trigger an endpoint (transport-level) error. 72 | type errorer interface { 73 | error() error 74 | } 75 | 76 | // encodeResponse is the common method to encode all response types to the 77 | // client. 78 | func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 79 | if e, ok := response.(errorer); ok && e.error() != nil { 80 | // Not a Go kit transport error, but a business-logic error. 81 | // Provide those as HTTP errors. 82 | encodeError(ctx, e.error(), w) 83 | return nil 84 | } 85 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 86 | return json.NewEncoder(w).Encode(response) 87 | } 88 | 89 | // encode error 90 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 91 | if err == nil { 92 | panic("encodeError with nil error") 93 | } 94 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 95 | w.WriteHeader(http.StatusInternalServerError) 96 | json.NewEncoder(w).Encode(map[string]interface{}{ 97 | "error": err.Error(), 98 | }) 99 | } 100 | -------------------------------------------------------------------------------- /lorem-metrics/README.md: -------------------------------------------------------------------------------- 1 | # lorem-metrics 2 | This is simple service module. Only for showing the micro service with HTTP and return json. 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | In this part I will demonstrate how to show your service metrics. I copied from `lorem-rate-limit` 6 | 7 | I am fully using all three functions from the golorem library. 8 | 9 | ## Required libraries 10 | 11 | go get github.com/go-kit/kit 12 | go get github.com/drhodes/golorem 13 | go get github.com/gorilla/mux 14 | go get github.com/juju/ratelimit 15 | go get github.com/prometheus/client_golang/prometheus 16 | 17 | ### service.go 18 | Business logic will be put here 19 | 20 | ### endpoint.go 21 | Endpoint will be created here 22 | 23 | ### transport.go 24 | Handling about encode and decode json 25 | 26 | ### logging.go 27 | Logging function is under this file 28 | 29 | ### instrument.go 30 | Middleware function. 31 | For this sample, this function for rate limiting and metrics. 32 | 33 | #### lorem-metrics.d 34 | Go main function will be located under this folder. The `dot d` means daemon. 35 | 36 | ### execute 37 | 38 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 39 | go run lorem-metrics/lorem-metrics.d/main.go 40 | 41 | ### Running Prometheus and Grafana 42 | To execute type 43 | 44 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 45 | docker-compose -f docker/docker-compose-prometheus-grafana.yml up -d 46 | -------------------------------------------------------------------------------- /lorem-metrics/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem_metrics 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "strings" 6 | "errors" 7 | "context" 8 | ) 9 | 10 | var ( 11 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 12 | ) 13 | 14 | //request 15 | type LoremRequest struct { 16 | RequestType string 17 | Min int 18 | Max int 19 | } 20 | 21 | //response 22 | type LoremResponse struct { 23 | Message string `json:"message"` 24 | Err error `json:"err,omitempty"` 25 | } 26 | 27 | // endpoints wrapper 28 | type Endpoints struct { 29 | LoremEndpoint endpoint.Endpoint 30 | } 31 | 32 | // creating Lorem Ipsum Endpoint 33 | func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint { 34 | return func(ctx context.Context, request interface{}) (interface{}, error) { 35 | req := request.(LoremRequest) 36 | 37 | var ( 38 | txt string 39 | min, max int 40 | ) 41 | 42 | min = req.Min 43 | max = req.Max 44 | 45 | if strings.EqualFold(req.RequestType, "Word") { 46 | txt = svc.Word(min, max) 47 | } else if strings.EqualFold(req.RequestType, "Sentence"){ 48 | txt = svc.Sentence(min, max) 49 | } else if strings.EqualFold(req.RequestType, "Paragraph") { 50 | txt = svc.Paragraph(min, max) 51 | } else { 52 | return nil, ErrRequestTypeNotFound 53 | } 54 | 55 | return LoremResponse{Message: txt}, nil 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /lorem-metrics/instrument.go: -------------------------------------------------------------------------------- 1 | package lorem_metrics 2 | 3 | import ( 4 | "github.com/go-kit/kit/metrics" 5 | "time" 6 | ) 7 | 8 | func Metrics(requestCount metrics.Counter, 9 | requestLatency metrics.Histogram) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return metricsMiddleware{ 12 | next, 13 | requestCount, 14 | requestLatency, 15 | } 16 | } 17 | } 18 | 19 | // Make a new type and wrap into Service interface 20 | // Add expected metrics property to this type 21 | type metricsMiddleware struct { 22 | Service 23 | requestCount metrics.Counter 24 | requestLatency metrics.Histogram 25 | } 26 | 27 | // Implement service functions and add label method for our metrics 28 | func (mw metricsMiddleware) Word(min, max int) (output string) { 29 | defer func(begin time.Time) { 30 | lvs := []string{"method", "Word"} 31 | mw.requestCount.With(lvs...).Add(1) 32 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 33 | }(time.Now()) 34 | output = mw.Service.Word(min, max) 35 | return 36 | } 37 | 38 | func (mw metricsMiddleware) Sentence(min, max int) (output string) { 39 | defer func(begin time.Time) { 40 | lvs := []string{"method", "Sentence"} 41 | mw.requestCount.With(lvs...).Add(1) 42 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 43 | }(time.Now()) 44 | output = mw.Service.Sentence(min, max) 45 | return 46 | } 47 | 48 | func (mw metricsMiddleware) Paragraph(min, max int) (output string) { 49 | defer func(begin time.Time) { 50 | lvs := []string{"method", "Paragraph"} 51 | mw.requestCount.With(lvs...).Add(1) 52 | mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) 53 | }(time.Now()) 54 | output = mw.Service.Paragraph(min, max) 55 | return 56 | } -------------------------------------------------------------------------------- /lorem-metrics/logging.go: -------------------------------------------------------------------------------- 1 | package lorem_metrics 2 | 3 | import ( 4 | "github.com/go-kit/kit/log" 5 | "time" 6 | ) 7 | 8 | // implement function to return ServiceMiddleware 9 | func LoggingMiddleware(logger log.Logger) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return loggingMiddleware{next, logger} 12 | } 13 | } 14 | 15 | // Make a new type and wrap into Service interface 16 | // Add logger property to this type 17 | type loggingMiddleware struct { 18 | Service 19 | logger log.Logger 20 | } 21 | 22 | // Implement Service Interface for LoggingMiddleware 23 | func (mw loggingMiddleware) Word(min, max int) (output string) { 24 | defer func(begin time.Time){ 25 | mw.logger.Log( 26 | "function","Word", 27 | "min", min, 28 | "max", max, 29 | "result", output, 30 | "took", time.Since(begin), 31 | ) 32 | }(time.Now()) 33 | output = mw.Service.Word(min,max) 34 | return 35 | } 36 | 37 | func (mw loggingMiddleware) Sentence(min, max int) (output string) { 38 | defer func(begin time.Time){ 39 | mw.logger.Log( 40 | "function","Sentence", 41 | "min", min, 42 | "max", max, 43 | "result", output, 44 | "took", time.Since(begin), 45 | ) 46 | }(time.Now()) 47 | output = mw.Service.Sentence(min,max) 48 | return 49 | } 50 | 51 | func (mw loggingMiddleware) Paragraph(min, max int) (output string) { 52 | defer func(begin time.Time){ 53 | mw.logger.Log( 54 | "function","Paragraph", 55 | "min", min, 56 | "max", max, 57 | "result", output, 58 | "took", time.Since(begin), 59 | ) 60 | }(time.Now()) 61 | output = mw.Service.Paragraph(min,max) 62 | return 63 | } -------------------------------------------------------------------------------- /lorem-metrics/lorem-metrics.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | "os" 6 | "os/signal" 7 | "syscall" 8 | "net/http" 9 | "fmt" 10 | "github.com/go-kit/kit/log" 11 | "github.com/ru-rocker/gokit-playground/lorem-metrics" 12 | ratelimitkit "github.com/go-kit/kit/ratelimit" 13 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" 14 | stdprometheus "github.com/prometheus/client_golang/prometheus" 15 | "time" 16 | "github.com/juju/ratelimit" 17 | ) 18 | 19 | func main() { 20 | ctx := context.Background() 21 | errChan := make(chan error) 22 | 23 | // Logging domain. 24 | var logger log.Logger 25 | { 26 | logger = log.NewLogfmtLogger(os.Stdout) 27 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 28 | logger = log.With(logger, "caller", log.DefaultCaller) 29 | } 30 | 31 | //declare metrics 32 | fieldKeys := []string{"method"} 33 | requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ 34 | Namespace: "ru_rocker", 35 | Subsystem: "lorem_service", 36 | Name: "request_count", 37 | Help: "Number of requests received.", 38 | }, fieldKeys) 39 | requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ 40 | Namespace: "ru_rocker", 41 | Subsystem: "lorem_service", 42 | Name: "request_latency_microseconds", 43 | Help: "Total duration of requests in microseconds.", 44 | }, fieldKeys) 45 | 46 | var svc lorem_metrics.Service 47 | svc = lorem_metrics.LoremService{} 48 | svc = lorem_metrics.LoggingMiddleware(logger)(svc) 49 | svc = lorem_metrics.Metrics(requestCount, requestLatency)(svc) 50 | 51 | rlbucket := ratelimit.NewBucket(1*time.Second, 5) 52 | e := lorem_metrics.MakeLoremLoggingEndpoint(svc) 53 | e = ratelimitkit.NewTokenBucketThrottler(rlbucket, time.Sleep)(e) 54 | endpoint := lorem_metrics.Endpoints{ 55 | LoremEndpoint: e, 56 | } 57 | 58 | r := lorem_metrics.MakeHttpHandler(ctx, endpoint, logger) 59 | 60 | // HTTP transport 61 | go func() { 62 | fmt.Println("Starting server at port 8080") 63 | handler := r 64 | errChan <- http.ListenAndServe(":8080", handler) 65 | }() 66 | 67 | 68 | go func() { 69 | c := make(chan os.Signal, 1) 70 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 71 | errChan <- fmt.Errorf("%s", <-c) 72 | }() 73 | fmt.Println(<- errChan) 74 | } 75 | -------------------------------------------------------------------------------- /lorem-metrics/prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s # By default, scrape targets every 15 seconds. 3 | 4 | # Attach these labels to any time series or alerts when communicating with 5 | # external systems (federation, remote storage, Alertmanager). 6 | external_labels: 7 | monitor: 'rurocker-monitor' 8 | 9 | # A scrape configuration containing exactly one endpoint to scrape: 10 | # Here it's Prometheus itself. 11 | scrape_configs: 12 | # The job name is added as a label `job=` to any timeseries scraped from this config. 13 | - job_name: 'prometheus' 14 | 15 | # Override the global default and scrape targets from this job every 5 seconds. 16 | scrape_interval: 5s 17 | 18 | static_configs: 19 | - targets: ['localhost:9090'] 20 | labels: 21 | group: 'local' 22 | 23 | - job_name: 'ru-rocker' 24 | scrape_interval: 5s 25 | static_configs: 26 | - targets: ['192.168.1.103:8080'] 27 | labels: 28 | group: 'lorem' -------------------------------------------------------------------------------- /lorem-metrics/service.go: -------------------------------------------------------------------------------- 1 | package lorem_metrics 2 | 3 | import ( 4 | golorem "github.com/drhodes/golorem" 5 | ) 6 | 7 | // Define service interface 8 | type Service interface { 9 | // generate a word with at least min letters and at most max letters. 10 | Word(min, max int) string 11 | 12 | // generate a sentence with at least min words and at most max words. 13 | Sentence(min, max int) string 14 | 15 | // generate a paragraph with at least min sentences and at most max sentences. 16 | Paragraph(min, max int) string 17 | } 18 | 19 | // Implement service with empty struct 20 | type LoremService struct { 21 | 22 | } 23 | 24 | // create type that return function. 25 | // this will be needed in main.go 26 | type ServiceMiddleware func(Service) Service 27 | 28 | // Implement service functions 29 | func (LoremService) Word(min, max int) string { 30 | return golorem.Word(min, max) 31 | } 32 | 33 | func (LoremService) Sentence(min, max int) string { 34 | return golorem.Sentence(min, max) 35 | } 36 | 37 | func (LoremService) Paragraph(min, max int) string { 38 | return golorem.Paragraph(min, max) 39 | } -------------------------------------------------------------------------------- /lorem-metrics/transport.go: -------------------------------------------------------------------------------- 1 | package lorem_metrics 2 | 3 | import ( 4 | "net/http" 5 | "errors" 6 | 7 | 8 | "github.com/gorilla/mux" 9 | "github.com/go-kit/kit/log" 10 | httptransport "github.com/go-kit/kit/transport/http" 11 | "encoding/json" 12 | "strconv" 13 | stdprometheus "github.com/prometheus/client_golang/prometheus/promhttp" 14 | "context" 15 | ) 16 | 17 | var ( 18 | // ErrBadRouting is returned when an expected path variable is missing. 19 | ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 20 | ) 21 | 22 | // Make Http Handler 23 | func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 24 | r := mux.NewRouter() 25 | options := []httptransport.ServerOption{ 26 | httptransport.ServerErrorLogger(logger), 27 | httptransport.ServerErrorEncoder(encodeError), 28 | } 29 | 30 | //POST /lorem/{type}/{min}/{max} 31 | r.Methods("POST").Path("/lorem/{type}/{min}/{max}").Handler(httptransport.NewServer( 32 | endpoint.LoremEndpoint, 33 | decodeLoremRequest, 34 | encodeResponse, 35 | options..., 36 | )) 37 | 38 | // GET /metrics 39 | r.Path("/metrics").Handler(stdprometheus.Handler()) 40 | 41 | return r 42 | 43 | } 44 | 45 | // decode url path variables into request 46 | func decodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 47 | vars := mux.Vars(r) 48 | requestType, ok := vars["type"] 49 | if !ok { 50 | return nil, ErrBadRouting 51 | } 52 | 53 | vmin, ok := vars["min"] 54 | if !ok { 55 | return nil, ErrBadRouting 56 | } 57 | 58 | vmax, ok := vars["max"] 59 | if !ok { 60 | return nil, ErrBadRouting 61 | } 62 | 63 | min, _ := strconv.Atoi(vmin) 64 | max, _ := strconv.Atoi(vmax) 65 | return LoremRequest{ 66 | RequestType: requestType, 67 | Min: min, 68 | Max: max, 69 | }, nil 70 | } 71 | 72 | // errorer is implemented by all concrete response types that may contain 73 | // errors. It allows us to change the HTTP response code without needing to 74 | // trigger an endpoint (transport-level) error. 75 | type errorer interface { 76 | error() error 77 | } 78 | 79 | // encodeResponse is the common method to encode all response types to the 80 | // client. 81 | func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 82 | if e, ok := response.(errorer); ok && e.error() != nil { 83 | // Not a Go kit transport error, but a business-logic error. 84 | // Provide those as HTTP errors. 85 | encodeError(ctx, e.error(), w) 86 | return nil 87 | } 88 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 89 | return json.NewEncoder(w).Encode(response) 90 | } 91 | 92 | // encode error 93 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 94 | if err == nil { 95 | panic("encodeError with nil error") 96 | } 97 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 98 | w.WriteHeader(http.StatusInternalServerError) 99 | json.NewEncoder(w).Encode(map[string]interface{}{ 100 | "error": err.Error(), 101 | }) 102 | } 103 | -------------------------------------------------------------------------------- /lorem-rate-limit/README.md: -------------------------------------------------------------------------------- 1 | # lorem-rate-limit 2 | This is simple service module. Only for showing the micro service with HTTP and return json. 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | In this part I will demonstrate how to limit a request based on Token Bucket Limiter algorithm. 6 | 7 | I am fully using all three functions from the golorem library. 8 | 9 | ## Required libraries 10 | 11 | go get github.com/go-kit/kit 12 | go get github.com/drhodes/golorem 13 | go get github.com/gorilla/mux 14 | go get github.com/juju/ratelimit 15 | 16 | ### service.go 17 | Business logic will be put here 18 | 19 | ### endpoint.go 20 | Endpoint will be created here 21 | 22 | ### transport.go 23 | Handling about encode and decode json 24 | 25 | ### logging.go 26 | Logging function is under this file 27 | 28 | ### instrument.go 29 | Middleware function. 30 | For this sample, this function only for rate limiting only. 31 | 32 | #### lorem-rate-limit.d 33 | Go main function will be located under this folder. The `dot d` means daemon. 34 | 35 | ### execute 36 | 37 | cd $GOPATH/src/github.com/ru-rocker/gokit-playground 38 | go run lorem-rate-limit/lorem-rate-limit.d/main.go 39 | -------------------------------------------------------------------------------- /lorem-rate-limit/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem_rate_limit 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "strings" 6 | "errors" 7 | "context" 8 | //"golang.org/x/net/context" 9 | ) 10 | 11 | var ( 12 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 13 | ) 14 | 15 | //request 16 | type LoremRequest struct { 17 | RequestType string 18 | Min int 19 | Max int 20 | } 21 | 22 | //response 23 | type LoremResponse struct { 24 | Message string `json:"message"` 25 | Err error `json:"err,omitempty"` 26 | } 27 | 28 | // endpoints wrapper 29 | type Endpoints struct { 30 | LoremEndpoint endpoint.Endpoint 31 | } 32 | 33 | // creating Lorem Ipsum Endpoint 34 | func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint { 35 | return func(ctx context.Context, request interface{}) (interface{}, error) { 36 | req := request.(LoremRequest) 37 | 38 | var ( 39 | txt string 40 | min, max int 41 | ) 42 | 43 | min = req.Min 44 | max = req.Max 45 | 46 | if strings.EqualFold(req.RequestType, "Word") { 47 | txt = svc.Word(min, max) 48 | } else if strings.EqualFold(req.RequestType, "Sentence"){ 49 | txt = svc.Sentence(min, max) 50 | } else if strings.EqualFold(req.RequestType, "Paragraph") { 51 | txt = svc.Paragraph(min, max) 52 | } else { 53 | return nil, ErrRequestTypeNotFound 54 | } 55 | 56 | return LoremResponse{Message: txt}, nil 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /lorem-rate-limit/instrument.go: -------------------------------------------------------------------------------- 1 | package lorem_rate_limit 2 | 3 | import ( 4 | "github.com/juju/ratelimit" 5 | "github.com/go-kit/kit/endpoint" 6 | "context" 7 | "errors" 8 | ) 9 | 10 | var ErrLimitExceed = errors.New("Rate Limit Exceed") 11 | 12 | func NewTokenBucketLimiter(tb *ratelimit.Bucket) endpoint.Middleware { 13 | return func(next endpoint.Endpoint) endpoint.Endpoint { 14 | return func(ctx context.Context, request interface{}) (interface{}, error) { 15 | if tb.TakeAvailable(1) == 0 { 16 | return nil, ErrLimitExceed 17 | } 18 | return next(ctx, request) 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /lorem-rate-limit/logging.go: -------------------------------------------------------------------------------- 1 | package lorem_rate_limit 2 | 3 | import ( 4 | "github.com/go-kit/kit/log" 5 | "time" 6 | ) 7 | 8 | // implement function to return ServiceMiddleware 9 | func LoggingMiddleware(logger log.Logger) ServiceMiddleware { 10 | return func(next Service) Service { 11 | return loggingMiddleware{next, logger} 12 | } 13 | } 14 | 15 | // Make a new type and wrap into Service interface 16 | // Add logger property to this type 17 | type loggingMiddleware struct { 18 | Service 19 | logger log.Logger 20 | } 21 | 22 | // Implement Service Interface for LoggingMiddleware 23 | func (mw loggingMiddleware) Word(min, max int) (output string) { 24 | defer func(begin time.Time){ 25 | mw.logger.Log( 26 | "function","Word", 27 | "min", min, 28 | "max", max, 29 | "result", output, 30 | "took", time.Since(begin), 31 | ) 32 | }(time.Now()) 33 | output = mw.Service.Word(min,max) 34 | return 35 | } 36 | 37 | func (mw loggingMiddleware) Sentence(min, max int) (output string) { 38 | defer func(begin time.Time){ 39 | mw.logger.Log( 40 | "function","Sentence", 41 | "min", min, 42 | "max", max, 43 | "result", output, 44 | "took", time.Since(begin), 45 | ) 46 | }(time.Now()) 47 | output = mw.Service.Sentence(min,max) 48 | return 49 | } 50 | 51 | func (mw loggingMiddleware) Paragraph(min, max int) (output string) { 52 | defer func(begin time.Time){ 53 | mw.logger.Log( 54 | "function","Paragraph", 55 | "min", min, 56 | "max", max, 57 | "result", output, 58 | "took", time.Since(begin), 59 | ) 60 | }(time.Now()) 61 | output = mw.Service.Paragraph(min,max) 62 | return 63 | } -------------------------------------------------------------------------------- /lorem-rate-limit/lorem-rate-limit.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | "os" 6 | "os/signal" 7 | "syscall" 8 | "net/http" 9 | "fmt" 10 | "github.com/go-kit/kit/log" 11 | ratelimitkit "github.com/go-kit/kit/ratelimit" 12 | "github.com/ru-rocker/gokit-playground/lorem-rate-limit" 13 | "time" 14 | "golang.org/x/time/rate" 15 | ) 16 | 17 | func main() { 18 | ctx := context.Background() 19 | errChan := make(chan error) 20 | 21 | // Logging domain. 22 | var logger log.Logger 23 | { 24 | logger = log.NewLogfmtLogger(os.Stdout) 25 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 26 | logger = log.With(logger, "caller", log.DefaultCaller) 27 | } 28 | 29 | var svc lorem_rate_limit.Service 30 | svc = lorem_rate_limit.LoremService{} 31 | svc = lorem_rate_limit.LoggingMiddleware(logger)(svc) 32 | 33 | limit := rate.NewLimiter(rate.Every(35*time.Millisecond), 100) 34 | e := lorem_rate_limit.MakeLoremLoggingEndpoint(svc) 35 | e = ratelimitkit.NewErroringLimiter(limit)(e) 36 | endpoint := lorem_rate_limit.Endpoints{ 37 | LoremEndpoint: e, 38 | } 39 | 40 | r := lorem_rate_limit.MakeHttpHandler(ctx, endpoint, logger) 41 | 42 | // HTTP transport 43 | go func() { 44 | fmt.Println("Starting server at port 8080") 45 | handler := r 46 | errChan <- http.ListenAndServe(":8080", handler) 47 | }() 48 | 49 | 50 | go func() { 51 | c := make(chan os.Signal, 1) 52 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 53 | errChan <- fmt.Errorf("%s", <-c) 54 | }() 55 | fmt.Println(<- errChan) 56 | } 57 | -------------------------------------------------------------------------------- /lorem-rate-limit/prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s # By default, scrape targets every 15 seconds. 3 | 4 | # Attach these labels to any time series or alerts when communicating with 5 | # external systems (federation, remote storage, Alertmanager). 6 | external_labels: 7 | monitor: 'rurocker-monitor' 8 | 9 | # A scrape configuration containing exactly one endpoint to scrape: 10 | # Here it's Prometheus itself. 11 | scrape_configs: 12 | # The job name is added as a label `job=` to any timeseries scraped from this config. 13 | - job_name: 'prometheus' 14 | 15 | # Override the global default and scrape targets from this job every 5 seconds. 16 | scrape_interval: 5s 17 | 18 | static_configs: 19 | - targets: ['localhost:9090'] 20 | labels: 21 | group: 'local' 22 | 23 | - job_name: 'ru-rocker' 24 | scrape_interval: 5s 25 | static_configs: 26 | - targets: ['192.168.1.103:8080'] 27 | labels: 28 | group: 'lorem' -------------------------------------------------------------------------------- /lorem-rate-limit/service.go: -------------------------------------------------------------------------------- 1 | package lorem_rate_limit 2 | 3 | import ( 4 | golorem "github.com/drhodes/golorem" 5 | ) 6 | 7 | // Define service interface 8 | type Service interface { 9 | // generate a word with at least min letters and at most max letters. 10 | Word(min, max int) string 11 | 12 | // generate a sentence with at least min words and at most max words. 13 | Sentence(min, max int) string 14 | 15 | // generate a paragraph with at least min sentences and at most max sentences. 16 | Paragraph(min, max int) string 17 | } 18 | 19 | // Implement service with empty struct 20 | type LoremService struct { 21 | 22 | } 23 | 24 | // create type that return function. 25 | // this will be needed in main.go 26 | type ServiceMiddleware func(Service) Service 27 | 28 | // Implement service functions 29 | func (LoremService) Word(min, max int) string { 30 | return golorem.Word(min, max) 31 | } 32 | 33 | func (LoremService) Sentence(min, max int) string { 34 | return golorem.Sentence(min, max) 35 | } 36 | 37 | func (LoremService) Paragraph(min, max int) string { 38 | return golorem.Paragraph(min, max) 39 | } -------------------------------------------------------------------------------- /lorem-rate-limit/transport.go: -------------------------------------------------------------------------------- 1 | package lorem_rate_limit 2 | 3 | import ( 4 | "net/http" 5 | "errors" 6 | 7 | 8 | "github.com/gorilla/mux" 9 | "github.com/go-kit/kit/log" 10 | httptransport "github.com/go-kit/kit/transport/http" 11 | "encoding/json" 12 | "strconv" 13 | //"golang.org/x/net/context" 14 | "context" 15 | ) 16 | 17 | var ( 18 | // ErrBadRouting is returned when an expected path variable is missing. 19 | ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 20 | ) 21 | 22 | // Make Http Handler 23 | func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 24 | r := mux.NewRouter() 25 | options := []httptransport.ServerOption{ 26 | httptransport.ServerErrorLogger(logger), 27 | httptransport.ServerErrorEncoder(encodeError), 28 | } 29 | 30 | //POST /lorem/{type}/{min}/{max} 31 | r.Methods("POST").Path("/lorem/{type}/{min}/{max}").Handler(httptransport.NewServer( 32 | endpoint.LoremEndpoint, 33 | decodeLoremRequest, 34 | encodeResponse, 35 | options..., 36 | )) 37 | 38 | return r 39 | 40 | } 41 | 42 | // decode url path variables into request 43 | func decodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 44 | vars := mux.Vars(r) 45 | requestType, ok := vars["type"] 46 | if !ok { 47 | return nil, ErrBadRouting 48 | } 49 | 50 | vmin, ok := vars["min"] 51 | if !ok { 52 | return nil, ErrBadRouting 53 | } 54 | 55 | vmax, ok := vars["max"] 56 | if !ok { 57 | return nil, ErrBadRouting 58 | } 59 | 60 | min, _ := strconv.Atoi(vmin) 61 | max, _ := strconv.Atoi(vmax) 62 | return LoremRequest{ 63 | RequestType: requestType, 64 | Min: min, 65 | Max: max, 66 | }, nil 67 | } 68 | 69 | // errorer is implemented by all concrete response types that may contain 70 | // errors. It allows us to change the HTTP response code without needing to 71 | // trigger an endpoint (transport-level) error. 72 | type errorer interface { 73 | error() error 74 | } 75 | 76 | // encodeResponse is the common method to encode all response types to the 77 | // client. 78 | func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 79 | if e, ok := response.(errorer); ok && e.error() != nil { 80 | // Not a Go kit transport error, but a business-logic error. 81 | // Provide those as HTTP errors. 82 | encodeError(ctx, e.error(), w) 83 | return nil 84 | } 85 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 86 | return json.NewEncoder(w).Encode(response) 87 | } 88 | 89 | // encode error 90 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 91 | if err == nil { 92 | panic("encodeError with nil error") 93 | } 94 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 95 | w.WriteHeader(http.StatusInternalServerError) 96 | json.NewEncoder(w).Encode(map[string]interface{}{ 97 | "error": err.Error(), 98 | }) 99 | } 100 | -------------------------------------------------------------------------------- /lorem/README.md: -------------------------------------------------------------------------------- 1 | # lorem 2 | This is simple service module. Only for showing the micro service with HTTP and return json. 3 | The purpose for this service is only generating lorem ipsum paragraph and return the payload. 4 | 5 | I am fully using all three functions from the golorem library. 6 | 7 | ## Required libraries 8 | 9 | go get github.com/go-kit/kit 10 | go get github.com/drhodes/golorem 11 | go get github.com/gorilla/mux 12 | 13 | 14 | ### service.go 15 | Business logic will be put here 16 | 17 | ### endpoint.go 18 | Endpoint will be created here 19 | 20 | ### transport.go 21 | Handling about encode and decode json 22 | 23 | #### lorem.d 24 | Go main function will be located under this folder. The `dot d` means daemon. 25 | 26 | ### execute 27 | 28 | cd $GOPATH 29 | go run src/github.com/ru-rocker/gokit-playground/lorem/lorem.d/main.go 30 | -------------------------------------------------------------------------------- /lorem/endpoints.go: -------------------------------------------------------------------------------- 1 | package lorem 2 | 3 | import ( 4 | "github.com/go-kit/kit/endpoint" 5 | "strings" 6 | "errors" 7 | "context" 8 | ) 9 | 10 | var ( 11 | ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph") 12 | ) 13 | 14 | //request 15 | type LoremRequest struct { 16 | RequestType string 17 | Min int 18 | Max int 19 | } 20 | 21 | //response 22 | type LoremResponse struct { 23 | Message string `json:"message"` 24 | Err error `json:"err,omitempty"` 25 | } 26 | 27 | // endpoints wrapper 28 | type Endpoints struct { 29 | LoremEndpoint endpoint.Endpoint 30 | } 31 | 32 | // creating Lorem Ipsum Endpoint 33 | func MakeLoremEndpoint(svc Service) endpoint.Endpoint { 34 | return func(ctx context.Context, request interface{}) (interface{}, error) { 35 | req := request.(LoremRequest) 36 | 37 | var ( 38 | txt string 39 | min, max int 40 | ) 41 | 42 | min = req.Min 43 | max = req.Max 44 | 45 | if strings.EqualFold(req.RequestType, "Word") { 46 | txt = svc.Word(min, max) 47 | } else if strings.EqualFold(req.RequestType, "Sentence"){ 48 | txt = svc.Sentence(min, max) 49 | } else if strings.EqualFold(req.RequestType, "Paragraph") { 50 | txt = svc.Paragraph(min, max) 51 | } else { 52 | return nil, ErrRequestTypeNotFound 53 | } 54 | 55 | return LoremResponse{Message: txt}, nil 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /lorem/lorem.d/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/net/context" 5 | "os" 6 | "os/signal" 7 | "syscall" 8 | "net/http" 9 | "fmt" 10 | "github.com/ru-rocker/gokit-playground/lorem" 11 | "github.com/go-kit/kit/log" 12 | ) 13 | 14 | func main() { 15 | ctx := context.Background() 16 | errChan := make(chan error) 17 | 18 | var svc lorem.Service 19 | svc = lorem.LoremService{} 20 | endpoint := lorem.Endpoints{ 21 | LoremEndpoint: lorem.MakeLoremEndpoint(svc), 22 | } 23 | 24 | // Logging domain. 25 | var logger log.Logger 26 | { 27 | logger = log.NewLogfmtLogger(os.Stderr) 28 | logger = log.With(logger, "ts", log.DefaultTimestampUTC) 29 | logger = log.With(logger, "caller", log.DefaultCaller) 30 | } 31 | 32 | r := lorem.MakeHttpHandler(ctx, endpoint, logger) 33 | 34 | // HTTP transport 35 | go func() { 36 | fmt.Println("Starting server at port 8080") 37 | handler := r 38 | errChan <- http.ListenAndServe(":8080", handler) 39 | }() 40 | 41 | 42 | go func() { 43 | c := make(chan os.Signal, 1) 44 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 45 | errChan <- fmt.Errorf("%s", <-c) 46 | }() 47 | fmt.Println(<- errChan) 48 | } 49 | -------------------------------------------------------------------------------- /lorem/service.go: -------------------------------------------------------------------------------- 1 | package lorem 2 | 3 | import ( 4 | golorem "github.com/drhodes/golorem" 5 | ) 6 | 7 | // Define service interface 8 | type Service interface { 9 | // generate a word with at least min letters and at most max letters. 10 | Word(min, max int) string 11 | 12 | // generate a sentence with at least min words and at most max words. 13 | Sentence(min, max int) string 14 | 15 | // generate a paragraph with at least min sentences and at most max sentences. 16 | Paragraph(min, max int) string 17 | } 18 | 19 | // Implement service with empty struct 20 | type LoremService struct { 21 | 22 | } 23 | 24 | // Implement service functions 25 | func (LoremService) Word(min, max int) string { 26 | return golorem.Word(min, max) 27 | } 28 | 29 | func (LoremService) Sentence(min, max int) string { 30 | return golorem.Sentence(min, max) 31 | } 32 | 33 | func (LoremService) Paragraph(min, max int) string { 34 | return golorem.Paragraph(min, max) 35 | } -------------------------------------------------------------------------------- /lorem/transport.go: -------------------------------------------------------------------------------- 1 | package lorem 2 | 3 | import ( 4 | "net/http" 5 | "errors" 6 | 7 | 8 | "github.com/gorilla/mux" 9 | "github.com/go-kit/kit/log" 10 | httptransport "github.com/go-kit/kit/transport/http" 11 | "encoding/json" 12 | "strconv" 13 | "context" 14 | ) 15 | 16 | var ( 17 | // ErrBadRouting is returned when an expected path variable is missing. 18 | ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)") 19 | ) 20 | 21 | // Make Http Handler 22 | func MakeHttpHandler(ctx context.Context, endpoint Endpoints, logger log.Logger) http.Handler { 23 | r := mux.NewRouter() 24 | options := []httptransport.ServerOption{ 25 | httptransport.ServerErrorLogger(logger), 26 | httptransport.ServerErrorEncoder(encodeError), 27 | } 28 | 29 | //POST /lorem/{type}/{min}/{max} 30 | r.Methods("POST").Path("/lorem/{type}/{min}/{max}").Handler(httptransport.NewServer( 31 | endpoint.LoremEndpoint, 32 | decodeLoremRequest, 33 | encodeResponse, 34 | options..., 35 | )) 36 | 37 | return r 38 | 39 | } 40 | 41 | // decode url path variables into request 42 | func decodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error) { 43 | vars := mux.Vars(r) 44 | requestType, ok := vars["type"] 45 | if !ok { 46 | return nil, ErrBadRouting 47 | } 48 | 49 | vmin, ok := vars["min"] 50 | if !ok { 51 | return nil, ErrBadRouting 52 | } 53 | 54 | vmax, ok := vars["max"] 55 | if !ok { 56 | return nil, ErrBadRouting 57 | } 58 | 59 | min, _ := strconv.Atoi(vmin) 60 | max, _ := strconv.Atoi(vmax) 61 | return LoremRequest{ 62 | RequestType: requestType, 63 | Min: min, 64 | Max: max, 65 | }, nil 66 | } 67 | 68 | // errorer is implemented by all concrete response types that may contain 69 | // errors. It allows us to change the HTTP response code without needing to 70 | // trigger an endpoint (transport-level) error. 71 | type errorer interface { 72 | error() error 73 | } 74 | 75 | // encodeResponse is the common method to encode all response types to the 76 | // client. 77 | func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { 78 | if e, ok := response.(errorer); ok && e.error() != nil { 79 | // Not a Go kit transport error, but a business-logic error. 80 | // Provide those as HTTP errors. 81 | encodeError(ctx, e.error(), w) 82 | return nil 83 | } 84 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 85 | return json.NewEncoder(w).Encode(response) 86 | } 87 | 88 | // encode error 89 | func encodeError(_ context.Context, err error, w http.ResponseWriter) { 90 | if err == nil { 91 | panic("encodeError with nil error") 92 | } 93 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 94 | w.WriteHeader(http.StatusInternalServerError) 95 | json.NewEncoder(w).Encode(map[string]interface{}{ 96 | "error": err.Error(), 97 | }) 98 | } 99 | --------------------------------------------------------------------------------