├── .gitignore ├── .travis.yml ├── BUILD ├── Dockerfile ├── LICENSE ├── README.md ├── build.sbt ├── example ├── ExampleServers.java ├── run.sh └── traffic.sh ├── images └── diffy_topology.png ├── project ├── build.properties └── plugins.sbt ├── sbt ├── scalastyle-config.xml ├── src ├── main │ ├── resources │ │ ├── BUILD │ │ └── templates │ │ │ ├── cron_report.mustache │ │ │ └── dashboard.mustache │ ├── scala │ │ ├── BUILD │ │ └── com │ │ │ └── twitter │ │ │ └── diffy │ │ │ ├── ApiController.scala │ │ │ ├── BUILD │ │ │ ├── DiffyServiceModule.scala │ │ │ ├── Frontend.scala │ │ │ ├── Main.scala │ │ │ ├── Renderer.scala │ │ │ ├── TimerModule.scala │ │ │ ├── analysis │ │ │ ├── BUILD │ │ │ ├── DifferenceCollector.scala │ │ │ ├── DifferenceCounter.scala │ │ │ ├── InMemoryDifferenceCollector.scala │ │ │ └── JoinedDifferences.scala │ │ │ ├── compare │ │ │ ├── BUILD │ │ │ └── Difference.scala │ │ │ ├── lifter │ │ │ ├── BUILD │ │ │ ├── FieldMap.scala │ │ │ ├── HtmlLifter.scala │ │ │ ├── HttpLifter.scala │ │ │ ├── JsonLifter.scala │ │ │ ├── MapLifter.scala │ │ │ ├── StringLifter.scala │ │ │ └── ThriftLifter.scala │ │ │ ├── proxy │ │ │ ├── BUILD │ │ │ ├── ClientService.scala │ │ │ ├── DifferenceProxy.scala │ │ │ ├── HttpDifferenceProxy.scala │ │ │ ├── ParallelMulticastService.scala │ │ │ ├── SequentialMulticastService.scala │ │ │ ├── Settings.scala │ │ │ └── ThriftDifferenceProxy.scala │ │ │ ├── scrooge │ │ │ ├── BUILD │ │ │ └── ThriftImporter.scala │ │ │ ├── util │ │ │ ├── BUILD │ │ │ └── EmailSender.scala │ │ │ └── workflow │ │ │ ├── BUILD │ │ │ ├── FunctionalReport.scala │ │ │ ├── ReportGenerator.scala │ │ │ └── Workflow.scala │ ├── thrift │ │ ├── BUILD │ │ └── diffy.thrift │ └── webapp │ │ ├── BUILD │ │ ├── css │ │ ├── bootstrap.lumen.css │ │ ├── bootstrap.superhero.css │ │ ├── diff.css │ │ └── source │ │ │ └── diff.sass │ │ └── scripts │ │ ├── angular-bindonce.min.js │ │ ├── angular.min.js │ │ ├── deep-diff.min.js │ │ ├── diff.js │ │ ├── grapnel.min.js │ │ ├── jquery.min.js │ │ ├── moment.min.js │ │ ├── source │ │ └── diff.coffee │ │ └── underscore.min.js └── test │ └── scala │ ├── BUILD │ └── com │ └── twitter │ └── diffy │ ├── ParentSpec.scala │ ├── StartupFeatureTest.scala │ ├── TestHelper.scala │ ├── compare │ └── DifferenceSpec.scala │ ├── lifter │ ├── HtmlLifterSpec.scala │ ├── HttpLifterSpec.scala │ ├── JsonLifterSpec.scala │ └── StringLifterSpec.scala │ ├── proxy │ └── SequentialMulticastServiceSpec.scala │ ├── util │ └── EmailSenderSpec.scala │ └── workflow │ └── DifferenceStatsMonitorSpec.scala └── version.sbt /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | dist/ 3 | project/boot/ 4 | project/plugins/project/ 5 | project/plugins/src_managed/ 6 | *.log 7 | *.tmproj 8 | lib_managed/ 9 | *.swp 10 | *.iml 11 | .idea/ 12 | .idea/* 13 | .DS_Store 14 | .ensime 15 | .ivyjars 16 | out/ 17 | sbt-launch.jar 18 | example/*.class 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | language: scala 3 | 4 | scala: 5 | - 2.10.5 6 | - 2.11.7 7 | 8 | jdk: 9 | - oraclejdk8 10 | 11 | cache: 12 | directories: 13 | - $HOME/.ivy2/cache 14 | - $HOME/.sbt/boot/scala-$TRAVIS_SCALA_VERSION 15 | 16 | before_script: unset SBT_OPTS 17 | 18 | script: sbt clean coverage test coverageReport 19 | 20 | after_script: 21 | - find $HOME/.sbt -name "*.lock" | xargs rm 22 | - find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm 23 | 24 | after_success: bash <(curl -s https://codecov.io/bash) 25 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | target( 2 | name='diffy', 3 | dependencies=[ 4 | 'diffy/src/main/scala', 5 | ] 6 | ) 7 | 8 | target( 9 | name='tests', 10 | dependencies=[ 11 | 'diffy/src/test/scala', 12 | ] 13 | ) 14 | 15 | target( 16 | name='bin', 17 | dependencies=['diffy/src/main/scala:bin'] 18 | ) 19 | 20 | jvm_app( 21 | name='bundle', 22 | basename='diffy-package-dist', 23 | binary='diffy/src/main/scala:bin', 24 | bundles=[ 25 | bundle(fileset=globs('config/*')) 26 | ] 27 | ) 28 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM hseeberger/scala-sbt 2 | 3 | RUN apt-get update 4 | 5 | ADD . /usr/local/src 6 | WORKDIR /usr/local/src 7 | RUN ./sbt assembly 8 | RUN mv target/scala-2.11 /bin/diffy 9 | 10 | ENTRYPOINT ["java", "-jar", "/bin/diffy/diffy-server.jar"] 11 | 12 | CMD [ "-candidate=localhost:9992", \ 13 | "-master.primary=localhost:9990", \ 14 | "-master.secondary=localhost:9991", \ 15 | "-service.protocol=http", \ 16 | "-serviceName='Test-Service'", \ 17 | "-proxy.port=:8880", \ 18 | "-admin.port=:8881", \ 19 | "-http.port=:8888", \ 20 | "-rootUrl=localhost:8888" \ 21 | ] 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Twitter has decided to archive this project, as it was built on our outdated tech stack, making it unpractical to maintain it in its current form. Furthermore, we are reinventing in this space internally to accommodate newer use-cases for validating our services. As we mature these new approaches, we will reevaluate how and when to re-release these new solutions as open source software. Thank you for your understanding. 2 | We won't be accepting pull requests or responding to issues for this project anymore. 3 | 4 | Note that @puneetkhanduri, this project's original author and a former Twitter employee maintains their own version of this project, called [Opendiffy](https://github.com/opendiffy/diffy). 5 | 6 | 7 | # Diffy 8 | 9 | [![GitHub license](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) 10 | [![Build status](https://img.shields.io/travis/twitter/diffy/master.svg)](https://travis-ci.org/twitter/diffy) 11 | [![Coverage status](https://img.shields.io/codecov/c/github/twitter/diffy/master.svg)](https://codecov.io/github/twitter/diffy) 12 | [![Project status](https://img.shields.io/badge/status-active-brightgreen.svg)](#status) 13 | [![Gitter](https://img.shields.io/badge/gitter-join%20chat-green.svg)](https://gitter.im/twitter/diffy) 14 | [![Maven Central](https://img.shields.io/maven-central/v/com.twitter/diffy_2.11.svg)](https://maven-badges.herokuapp.com/maven-central/com.twitter/diffy_2.11) 15 | 16 | 17 | ## What is Diffy? 18 | 19 | Diffy finds potential bugs in your service using running instances of your new code and your old 20 | code side by side. Diffy behaves as a proxy and multicasts whatever requests it receives to each of 21 | the running instances. It then compares the responses, and reports any regressions that may surface 22 | from those comparisons. The premise for Diffy is that if two implementations of the service return 23 | “similar” responses for a sufficiently large and diverse set of requests, then the two 24 | implementations can be treated as equivalent and the newer implementation is regression-free. 25 | For a more detailed analysis of Diffy checkout this [blogpost](https://blog.twitter.com/engineering/en_us/a/2015/diffy-testing-services-without-writing-tests.html). 26 | 27 | ## How does Diffy work? 28 | 29 | Diffy acts as a proxy that accepts requests drawn from any source that you provide and multicasts 30 | each of those requests to three different service instances: 31 | 32 | 1. A candidate instance running your new code 33 | 2. A primary instance running your last known-good code 34 | 3. A secondary instance running the same known-good code as the primary instance 35 | 36 | As Diffy receives a request, it is multicast and sent to your candidate, primary, and secondary 37 | instances. When those services send responses back, Diffy compares those responses and looks for two 38 | things: 39 | 40 | 1. Raw differences observed between the candidate and primary instances. 41 | 2. Non-deterministic noise observed between the primary and secondary instances. Since both of these 42 | instances are running known-good code, you should expect responses to be in agreement. If not, 43 | your service may have non-deterministic behavior, which is to be expected. 44 | ![Diffy Topology](/images/diffy_topology.png) 45 | 46 | Diffy measures how often primary and secondary disagree with each other vs. how often primary and 47 | candidate disagree with each other. If these measurements are roughly the same, then Diffy 48 | determines that there is nothing wrong and that the error can be ignored. 49 | 50 | ## How to get started? 51 | # Running the example 52 | The example.sh script included here builds and launches example servers as well as a diffy instance. Verify 53 | that the following ports are available (9000, 9100, 9200, 8880, 8881, & 8888) and run `./example/run.sh start`. 54 | 55 | Once your local Diffy instance is deployed, you send it a few requests 56 | like `curl --header "Canonical-Resource: Json" localhost:8880/json?Twitter`. You can then go to your browser at 57 | [http://localhost:8888](http://localhost:8888) to see what the differences across our example instances look like. 58 | 59 | # Digging deeper 60 | That was cool but now you want to compare old and new versions of your own service. Here’s how you can 61 | start using Diffy to compare three instances of your service: 62 | 63 | 1. Deploy your old code to `localhost:9990`. This is your primary. 64 | 2. Deploy your old code to `localhost:9991`. This is your secondary. 65 | 3. Deploy your new code to `localhost:9992`. This is your candidate. 66 | 4. Download the latest Diffy binary from maven central or build your own from the code using `./sbt assembly`. 67 | 5. Run the Diffy jar with following command line arguments: 68 | 69 | ``` 70 | java -jar diffy-server.jar \ 71 | -candidate=localhost:9992 \ 72 | -master.primary=localhost:9990 \ 73 | -master.secondary=localhost:9991 \ 74 | -service.protocol=http \ 75 | -serviceName=My-Service \ 76 | -proxy.port=:8880 \ 77 | -admin.port=:8881 \ 78 | -http.port=:8888 \ 79 | -rootUrl='localhost:8888' 80 | ``` 81 | 82 | 6. Send a few test requests to your Diffy instance on its proxy port: 83 | 84 | ``` 85 | curl localhost:8880/your/application/route?with=queryparams 86 | ``` 87 | 88 | 7. Watch the differences show up in your browser at [http://localhost:8888](http://localhost:8888). 89 | 90 | ## Using Diffy with Docker 91 | 92 | You can pull the official docker image with `docker pull diffy/diffy` 93 | 94 | And run it with 95 | ``` 96 | docker run -ti \ 97 | -p 8880:8880 -p 8881:8881 -p 8888:8888 \ 98 | diffy/diffy \ 99 | -candidate=localhost:9992 \ 100 | -master.primary=localhost:9990 \ 101 | -master.secondary=localhost:9991 \ 102 | -service.protocol=http \ 103 | -serviceName="Test-Service" \ 104 | -proxy.port=:8880 \ 105 | -admin.port=:8881 \ 106 | -http.port=:8888 \ 107 | -rootUrl=localhost:8888 108 | ``` 109 | 110 | You should now be able to point to: 111 | - http://localhost:8888 to see the web interface 112 | - http://localhost:8881/admin for admin console 113 | - Use port 8880 to make the API requests 114 | 115 | To build from source you can run `docker build -t diffy .` 116 | 117 | ## FAQ's 118 | For safety reasons `POST`, `PUT`, ` DELETE ` are ignored by default . Add ` -allowHttpSideEffects=true ` to your command line arguments to enable these verbs. 119 | 120 | ## HTTPS 121 | If you are trying to run Diffy over a HTTPS API, the config required is: 122 | 123 | -service.protocol=https 124 | 125 | And in case of the HTTPS port be different than 443: 126 | 127 | -https.port=123 128 | 129 | ## License 130 | 131 | Licensed under the **[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)** (the "License"); 132 | you may not use this software except in compliance with the License. 133 | 134 | Unless required by applicable law or agreed to in writing, software 135 | distributed under the License is distributed on an "AS IS" BASIS, 136 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137 | See the License for the specific language governing permissions and 138 | limitations under the License. 139 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | import ReleaseTransformations._ 2 | 3 | lazy val buildSettings = Seq( 4 | organization := "com.twitter", 5 | scalaVersion := "2.11.7", 6 | crossScalaVersions := Seq("2.10.5", "2.11.7") 7 | ) 8 | 9 | lazy val compilerOptions = Seq( 10 | "-deprecation", 11 | "-encoding", "UTF-8", 12 | "-feature", 13 | "-language:existentials", 14 | "-language:higherKinds", 15 | "-unchecked", 16 | "-Ywarn-dead-code", 17 | "-Ywarn-numeric-widen", 18 | "-Xfuture" 19 | ) 20 | 21 | lazy val testDependencies = Seq( 22 | "junit" % "junit" % "4.8.1", 23 | "org.mockito" % "mockito-all" % "1.8.5", 24 | "org.scalacheck" %% "scalacheck" % "1.12.4", 25 | "org.scalatest" %% "scalatest" % "2.2.5" 26 | ) 27 | 28 | lazy val finatraVersion = "2.0.0.M2" 29 | 30 | lazy val finatraDependencies = Seq( 31 | "com.twitter.finatra" %% "finatra-http" % finatraVersion, 32 | "com.twitter.finatra" %% "finatra-http" % finatraVersion % "test" classifier "tests", 33 | "com.twitter.inject" %% "inject-app" % finatraVersion % "test", 34 | "com.twitter.inject" %% "inject-app" % finatraVersion % "test" classifier "tests", 35 | "com.twitter.inject" %% "inject-core" % finatraVersion % "test", 36 | "com.twitter.inject" %% "inject-core" % finatraVersion % "test" classifier "tests", 37 | "com.twitter.inject" %% "inject-modules" % finatraVersion % "test", 38 | "com.twitter.inject" %% "inject-modules" % finatraVersion % "test" classifier "tests", 39 | "com.twitter.inject" %% "inject-server" % finatraVersion % "test", 40 | "com.twitter.inject" %% "inject-server" % finatraVersion % "test" classifier "tests" 41 | ) 42 | 43 | lazy val baseSettings = Seq( 44 | resolvers += "Twitter's Repository" at "https://maven.twttr.com/", 45 | scalacOptions ++= compilerOptions ++ ( 46 | CrossVersion.partialVersion(scalaVersion.value) match { 47 | case Some((2, 11)) => Seq("-Ywarn-unused-import") 48 | case _ => Nil 49 | } 50 | ), 51 | scalacOptions in (Compile, console) := compilerOptions, 52 | libraryDependencies ++= Seq( 53 | "io.netty" % "netty-tcnative-boringssl-static" % "2.0.25.Final", 54 | "com.twitter" %% "finagle-http" % "6.28.0", 55 | "com.twitter" %% "finagle-thriftmux" % "6.28.0", 56 | "com.twitter" %% "scrooge-generator" % "4.0.0", 57 | "javax.mail" % "mail" % "1.4.7", 58 | "org.jsoup" % "jsoup" % "1.7.2", 59 | "org.scala-lang" % "scala-compiler" % scalaVersion.value 60 | ) ++ finatraDependencies ++ testDependencies.map(_ % "test"), 61 | assemblyMergeStrategy in assembly := { 62 | case "BUILD" => MergeStrategy.discard 63 | case PathList("scala", "tools", _*) => MergeStrategy.last 64 | case other => MergeStrategy.defaultMergeStrategy(other) 65 | } 66 | ) 67 | 68 | lazy val docSettings = site.settings ++ ghpages.settings ++ site.includeScaladoc("docs") :+ ( 69 | git.remoteRepo := "git@github.com:twitter/diffy.git" 70 | ) 71 | 72 | lazy val diffy = project.in(file(".")) 73 | .settings( 74 | moduleName := "diffy", 75 | assemblyJarName := "diffy-server.jar", 76 | excludeFilter in unmanagedResources := HiddenFileFilter || "BUILD", 77 | unmanagedResourceDirectories in Compile += 78 | baseDirectory.value / "src" / "main" / "webapp" 79 | ) 80 | .settings(buildSettings ++ baseSettings ++ docSettings ++ publishSettings) 81 | 82 | lazy val publishSettings = Seq( 83 | releaseCrossBuild := true, 84 | releasePublishArtifactsAction := PgpKeys.publishSigned.value, 85 | homepage := Some(url("https://github.com/twitter/diffy")), 86 | licenses := Seq("Apache 2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")), 87 | publishMavenStyle := true, 88 | publishArtifact in Test := false, 89 | pomIncludeRepository := { _ => false }, 90 | publishTo := { 91 | val nexus = "https://oss.sonatype.org/" 92 | if (isSnapshot.value) 93 | Some("snapshots" at nexus + "content/repositories/snapshots") 94 | else 95 | Some("releases" at nexus + "service/local/staging/deploy/maven2") 96 | }, 97 | autoAPIMappings := true, 98 | apiURL := Some(url("https://twitter.github.io/diffy/docs/")), 99 | scmInfo := Some( 100 | ScmInfo( 101 | url("https://github.com/twitter/diffy"), 102 | "scm:git:git@github.com:twitter/diffy.git" 103 | ) 104 | ), 105 | pomExtra := ( 106 | 107 | 108 | puneetkhanduri 109 | Puneet Khanduri 110 | https://twitter.com/pzdk 111 | 112 | 113 | ) 114 | ) 115 | 116 | lazy val sharedReleaseProcess = Seq( 117 | releaseProcess := Seq[ReleaseStep]( 118 | checkSnapshotDependencies, 119 | inquireVersions, 120 | runClean, 121 | runTest, 122 | setReleaseVersion, 123 | commitReleaseVersion, 124 | tagRelease, 125 | publishArtifacts, 126 | setNextVersion, 127 | commitNextVersion, 128 | ReleaseStep(action = Command.process("sonatypeReleaseAll", _)), 129 | pushChanges 130 | ) 131 | ) 132 | 133 | credentials ++= ( 134 | for { 135 | username <- Option(System.getenv().get("SONATYPE_USERNAME")) 136 | password <- Option(System.getenv().get("SONATYPE_PASSWORD")) 137 | } yield Credentials( 138 | "Sonatype Nexus Repository Manager", 139 | "oss.sonatype.org", 140 | username, 141 | password 142 | ) 143 | ).toSeq 144 | -------------------------------------------------------------------------------- /example/ExampleServers.java: -------------------------------------------------------------------------------- 1 | import java.net.InetSocketAddress; 2 | import java.io.IOException; 3 | import java.io.OutputStream; 4 | import java.util.function.Function; 5 | import java.util.stream.Stream; 6 | 7 | import com.sun.net.httpserver.HttpExchange; 8 | import com.sun.net.httpserver.HttpHandler; 9 | import com.sun.net.httpserver.HttpServer; 10 | 11 | public class ExampleServers { 12 | public static void main(String[] args) throws Exception { 13 | int primary = Integer.parseInt(args[0]); 14 | int secondary = Integer.parseInt(args[1]); 15 | int candidate = Integer.parseInt(args[2]); 16 | Thread p = new Thread(() -> bind(primary, x -> x.toLowerCase())); 17 | Thread s = new Thread(() -> bind(secondary, x -> x.toLowerCase())); 18 | Thread c = new Thread(() -> bind(candidate, x -> x.toUpperCase())); 19 | p.start(); 20 | s.start(); 21 | c.start(); 22 | while(true){ 23 | Thread.sleep(10); 24 | } 25 | } 26 | 27 | public static void bind(int port, Function lambda) { 28 | try { 29 | HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); 30 | server.createContext( 31 | "/json", 32 | new Handler( 33 | "{\"name\":\"%s\", \"timestamp\":\"%s\"}", 34 | "application/json", 35 | lambda)); 36 | server.createContext( 37 | "/html", 38 | new Handler( 39 | "%s%s", 40 | "text/html", 41 | lambda)); 42 | server.setExecutor(null); 43 | server.start(); 44 | } catch (Exception exception) { 45 | System.err.println("!!!failed to start!!!"); 46 | } 47 | } 48 | } 49 | class Handler implements HttpHandler { 50 | private String template; 51 | private String contentType; 52 | private Function lambda; 53 | public Handler(String template, String contentType, Function lambda) { 54 | super(); 55 | this.template = template; 56 | this.contentType = contentType; 57 | this.lambda = lambda; 58 | } 59 | 60 | @Override 61 | public void handle(HttpExchange t) throws IOException { 62 | String name = lambda.apply(t.getRequestURI().getQuery()); 63 | String response = String.format(template, name, System.currentTimeMillis()); 64 | System.out.println(response); 65 | t.getResponseHeaders().add("Content-Type", contentType); 66 | t.sendResponseHeaders(200, response.length()); 67 | OutputStream os = t.getResponseBody(); 68 | os.write(response.getBytes()); 69 | os.close(); 70 | } 71 | } -------------------------------------------------------------------------------- /example/run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ "$1" = "start" ]; 4 | then 5 | 6 | # Build primary, secondary, and candidate servers 7 | javac example/ExampleServers.java && \ 8 | 9 | # Deploy primary, secondary, and candidate servers 10 | java -cp example ExampleServers 9000 9100 9200 & \ 11 | 12 | # Build diffy 13 | ./sbt assembly && \ 14 | 15 | # Deploy diffy 16 | java -jar ./target/scala-2.11/diffy-server.jar \ 17 | -candidate='localhost:9200' \ 18 | -master.primary='localhost:9000' \ 19 | -master.secondary='localhost:9100' \ 20 | -service.protocol='http' \ 21 | -serviceName='My Service' \ 22 | -proxy.port=:8880 \ 23 | -admin.port=:8881 \ 24 | -http.port=:8888 \ 25 | -rootUrl='localhost:8888' &\ 26 | 27 | sleep 3 28 | echo "Wait for Diffy to deploy" 29 | sleep 2 30 | else 31 | echo "Please make sure ports 9000, 9100, 9200, 8880, 8881, & 8888 are available before running \"example/run.sh start\"" 32 | fi 33 | 34 | -------------------------------------------------------------------------------- /example/traffic.sh: -------------------------------------------------------------------------------- 1 | echo "Send some traffic to your Diffy instance" 2 | for i in {1..20} 3 | do 4 | sleep 0.1 5 | curl -s -i -H "Canonical-Resource : json" http://localhost:8880/json?ByteDance > /dev/null 6 | sleep 0.1 7 | curl -s -i -H "Canonical-Resource : json" http://localhost:8880/json?Twitter > /dev/null 8 | sleep 0.1 9 | curl -s -i -H "Canonical-Resource : json" http://localhost:8880/json?Airbnb > /dev/null 10 | sleep 0.1 11 | curl -s -i -H "Canonical-Resource : json" http://localhost:8880/json?Paytm > /dev/null 12 | sleep 0.1 13 | curl -s -i -H "Canonical-Resource : json" http://localhost:8880/json?Baidu > /dev/null 14 | done 15 | -------------------------------------------------------------------------------- /images/diffy_topology.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twitter-archive/diffy/6212af84fcc2a1d3bbf3f3223ec65fec5273329d/images/diffy_topology.png -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.9 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | resolvers ++= Seq( 2 | "Twitter's Repository" at "https://maven.twttr.com/", 3 | "jgit-repo" at "https://download.eclipse.org/jgit/maven" 4 | ) 5 | 6 | addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0") 7 | addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.0") 8 | addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") 9 | addSbtPlugin("com.twitter" %% "scrooge-sbt-plugin" % "4.0.0") 10 | addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.5.3") 11 | addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "0.8.1") 12 | addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.6.0") 13 | addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.2.0") 14 | -------------------------------------------------------------------------------- /sbt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sbtver=0.13.8 4 | sbtjar=sbt-launch.jar 5 | sbtsha128=57d0f04f4b48b11ef7e764f4cea58dee4e806ffd 6 | 7 | sbtrepo=https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch 8 | 9 | if [ ! -f $sbtjar ]; then 10 | echo "downloading $sbtjar" 1>&2 11 | if ! curl --location --silent --fail --remote-name $sbtrepo/$sbtver/$sbtjar; then 12 | exit 1 13 | fi 14 | fi 15 | 16 | checksum=`openssl dgst -sha1 $sbtjar | awk '{ print $2 }'` 17 | if [ "$checksum" != $sbtsha128 ]; then 18 | echo "bad $sbtjar. delete $sbtjar and run $0 again." 19 | exit 1 20 | fi 21 | 22 | [ -f ~/.sbtconfig ] && . ~/.sbtconfig 23 | 24 | java -ea \ 25 | $SBT_OPTS \ 26 | $JAVA_OPTS \ 27 | -Djava.net.preferIPv4Stack=true \ 28 | -XX:+AggressiveOpts \ 29 | -XX:+UseParNewGC \ 30 | -XX:+UseConcMarkSweepGC \ 31 | -XX:+CMSParallelRemarkEnabled \ 32 | -XX:+CMSClassUnloadingEnabled \ 33 | -XX:ReservedCodeCacheSize=128m \ 34 | -XX:MaxPermSize=1024m \ 35 | -XX:SurvivorRatio=128 \ 36 | -XX:MaxTenuringThreshold=0 \ 37 | -Xss8M \ 38 | -Xms512M \ 39 | -Xmx2G \ 40 | -server \ 41 | -jar $sbtjar "$@" 42 | -------------------------------------------------------------------------------- /scalastyle-config.xml: -------------------------------------------------------------------------------- 1 | 2 | Scalastyle standard configuration 3 | 4 | 5 | FOR 6 | IF 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | true 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/main/resources/BUILD: -------------------------------------------------------------------------------- 1 | resources( 2 | name='resources', 3 | sources=rglobs('*', exclude=[rglobs('BUILD*', '*.pyc')]) 4 | ) 5 | -------------------------------------------------------------------------------- /src/main/resources/templates/cron_report.mustache: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | {{criticalDiffs}} critical warnings 6 |
7 | Diffy for {{serviceName}} compared master against production for {{delay}}. 8 | {{^criticalEndpoints.isEmpty}} 9 |
10 |
Critical Endpoints
11 | {{#criticalEndpoints}} 12 |
13 |
14 | {{endpointName}} 15 | - 16 | {{count}} requests 17 |
18 |
19 | {{#fields}} 20 |
21 | {{absoluteDifference}}% 22 | {{fieldName}} 23 |
24 | {{/fields}} 25 |
26 |
27 | {{/criticalEndpoints}} 28 |
29 |
30 | * The percentages above are the relative difference between live-prod and prod-prod mismatches. See difference metrics for more details. 31 |
32 | {{/criticalEndpoints.isEmpty}} 33 |
34 |
Passing Endpoints
35 | {{#passingEndpoints}} 36 |
37 | {{endpointName}} 38 | - 39 | {{count}} requests 40 |
41 | {{/passingEndpoints}} 42 |
43 |
44 | 45 | -------------------------------------------------------------------------------- /src/main/resources/templates/dashboard.mustache: -------------------------------------------------------------------------------- 1 | 2 | 3 | Diffy - {{serviceName}} 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 20 | 21 | 59 | 60 | 77 | 78 | 84 | 85 | 86 | 87 | 88 | 124 | 125 | 149 | 150 |
151 |
152 | 156 | 157 |
158 |

{{serviceName}}

159 |
{{serviceClass}}
160 |
161 | 162 |
163 | Last reset [[info.last_reset | ago]] 164 |
165 | 166 |
167 |
168 | 172 |
173 |
174 | 175 | 195 |
196 | 197 |
198 |
199 |

[[endpointName]]

200 |
    201 |
  • 202 |
    [[diffs]]
    203 |
    DIFFS
    204 |
  • 205 |
  • 206 |
    [[endpoint.total]]
    207 |
    REQUESTS
    208 |
  • 209 |
  • 210 |
    [[percentage | number:2]]
    211 |
    % FAILING
    212 |
  • 213 |
214 |
215 |
216 | 217 |
218 | No differences 219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 | Expand All 227 | Clear Exclusions 228 | Auto Exclude 229 | Collapse Excluded 230 |
231 |
232 | 233 |
234 | 235 |
236 | 237 |
238 |
239 |
240 |
Field
241 |
[[pathItem]]/
242 |
243 |
244 |
Expected
245 |
[[difference.left]]
246 |
247 |
248 |
Actual
249 |
[[difference.right]]
250 |
251 |
252 |
253 | 254 | 255 | 256 |
257 |
258 |
259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | -------------------------------------------------------------------------------- /src/main/scala/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='scala', 3 | dependencies=[ 4 | 'diffy/src/main/scala/com/twitter/diffy', 5 | 'diffy/src/main/webapp' 6 | ], 7 | resources=[ 8 | 'diffy/src/main/resources' 9 | ] 10 | ) 11 | 12 | jvm_binary( 13 | name='bin', 14 | basename='diffy-server', 15 | main='com.twitter.diffy.Main', 16 | dependencies=[':scala'] 17 | ) -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/ApiController.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import javax.inject.Inject 4 | 5 | import com.twitter.diffy.analysis._ 6 | import com.twitter.diffy.proxy._ 7 | import com.twitter.diffy.workflow._ 8 | import com.twitter.finagle.http.{Request, Response} 9 | import com.twitter.finagle.{Service, SimpleFilter} 10 | import com.twitter.finatra.http.Controller 11 | import com.twitter.util._ 12 | 13 | object ApiController { 14 | val MissingEndpointException = Future.value(Renderer.error("Specify an endpoint")) 15 | val MissingEndpointPathException = Future.value(Renderer.error("Specify an endpoint and path")) 16 | val RequestPurgedException = Future.value(Renderer.error("Request purged")) 17 | val IndexOutOfBoundsException = Future.value(Renderer.error("Request index out of bounds")) 18 | } 19 | 20 | class ApiController @Inject()( 21 | proxy: DifferenceProxy, 22 | settings: Settings, 23 | workflow: FunctionalReport, 24 | reportGenerator: ReportGenerator) 25 | extends Controller 26 | { 27 | import ApiController._ 28 | 29 | val thresholdFilter = 30 | DifferencesFilterFactory(settings.relativeThreshold, settings.absoluteThreshold) 31 | 32 | def endpointMap( 33 | JoinedEndpoint: JoinedEndpoint, 34 | includeWeights: Boolean, 35 | excludeNoise: Boolean 36 | ) = 37 | Map( 38 | "endpoint" -> Renderer.endpoint(JoinedEndpoint.endpoint), 39 | "fields" -> 40 | Renderer.fields( 41 | // Only show fields that are above the thresholds 42 | if (excludeNoise) { 43 | JoinedEndpoint.fields.filter { case ((_, field)) => 44 | thresholdFilter(field) 45 | } 46 | } else { 47 | JoinedEndpoint.fields 48 | }, 49 | includeWeights 50 | ) 51 | ) 52 | 53 | get("/api/1/overview") { req: Request => 54 | val excludeNoise = req.params.getBooleanOrElse("exclude_noise", true) 55 | val includeWeights = req.params.getBooleanOrElse("include_weights", false) 56 | 57 | proxy.joinedDifferences.endpoints map { eps => 58 | eps.map { case (endpoint, diffs) => 59 | endpoint -> endpointMap(diffs, includeWeights, excludeNoise) 60 | } 61 | } 62 | } 63 | 64 | get("/api/1/report") { req: Request => 65 | reportGenerator.extractReportFromDifferences 66 | } 67 | 68 | get("/api/1/endpoints") { req: Request => 69 | proxy.joinedDifferences.raw.counter.endpoints map Renderer.endpoints 70 | } 71 | 72 | get("/api/1/endpoints/:endpoint/stats") { req: Request => 73 | val excludeNoise = req.params.getBooleanOrElse("exclude_noise", true) 74 | val includeWeights = req.params.getBooleanOrElse("include_weights", false) 75 | 76 | req.params.get("endpoint") match { 77 | case Some(endpoint) => 78 | proxy.joinedDifferences.endpoint(endpoint) map { case joinedEndpoint => 79 | endpointMap(joinedEndpoint, includeWeights, excludeNoise) 80 | } handle { 81 | case t: Throwable => Renderer.error(t.getMessage()) 82 | } 83 | 84 | case None => MissingEndpointException 85 | } 86 | } 87 | 88 | get("/api/1/endpoints/:endpoint/fields/:path/results") { req: Request => 89 | ( 90 | req.params.get("endpoint"), 91 | req.params.get("path") 92 | ) match { 93 | case (Some(endpoint), Some(path)) => 94 | proxy.collector.prefix(analysis.Field(endpoint, path)) map { drs => 95 | Map( 96 | "endpoint" -> endpoint, 97 | "path" -> path, 98 | "requests" -> 99 | Renderer.differenceResults( 100 | drs, 101 | req.params.getBooleanOrElse("include_request", false) 102 | ) 103 | ) 104 | } 105 | 106 | case _ => MissingEndpointPathException 107 | } 108 | } 109 | 110 | get("/api/1/endpoints/:endpoint/fields/:path/results/:index") { req: Request => 111 | ( 112 | req.params.get("endpoint"), 113 | req.params.get("path"), 114 | req.params.getInt("index") 115 | ) match { 116 | case (Some(endpoint), Some(path), Some(index)) => 117 | proxy.collector.prefix(analysis.Field(endpoint, path)) map { 118 | _.toSeq.lift(index) 119 | } map { 120 | case Some(dr) => 121 | Renderer.differenceResult( 122 | dr, 123 | req.params.getBooleanOrElse("include_request", true) 124 | ) 125 | 126 | case None => IndexOutOfBoundsException 127 | } 128 | 129 | case _ => MissingEndpointPathException 130 | } 131 | } 132 | 133 | get("/api/1/requests/:id") { req: Request => 134 | req.params.get("id") map { id => 135 | proxy.collector(id.toLong) map { dr => 136 | Renderer.differenceResult( 137 | dr, 138 | req.params.getBooleanOrElse("include_request", true) 139 | ) 140 | } 141 | } match { 142 | case Some(request) => request 143 | case None => RequestPurgedException 144 | } 145 | } 146 | 147 | get("/api/1/clear") { req: Request => 148 | proxy.clear() map { _ => 149 | workflow.schedule() 150 | Renderer.success("Diffs cleared") 151 | } 152 | } 153 | 154 | post("/api/1/email") { req: Request => 155 | reportGenerator.sendEmail 156 | Renderer.success("Sent report e-mail.") 157 | } 158 | 159 | get("/api/1/info") { req: Request => 160 | (proxy match { 161 | case thrift: ThriftDifferenceProxy => Map( 162 | "candidate" -> thriftServiceToMap(settings.candidate.path, thrift.candidate), 163 | "primary" -> thriftServiceToMap(settings.primary.path, thrift.primary), 164 | "secondary" -> thriftServiceToMap(settings.secondary.path, thrift.secondary), 165 | "thrift_jar" -> settings.pathToThriftJar, 166 | "service_class" -> settings.serviceClass, 167 | "service_name" -> settings.serviceName, 168 | "client_id" -> settings.clientId, 169 | "threshold_relative" -> settings.relativeThreshold, 170 | "threshold_absolute" -> settings.absoluteThreshold, 171 | "protocol" -> "thrift" 172 | ) 173 | 174 | case http: HttpDifferenceProxy => Map( 175 | "candidate" -> httpServiceToMap(settings.candidate.path, http.candidate), 176 | "primary" -> httpServiceToMap(settings.primary.path, http.primary), 177 | "secondary" -> httpServiceToMap(settings.secondary.path, http.secondary), 178 | "protocol" -> "http" 179 | ) 180 | }) ++ Map("last_reset" -> proxy.lastReset.inMillis) 181 | } 182 | 183 | private[this] def thriftServiceToMap(target: String, srv: ThriftService) = 184 | Map( 185 | "target" -> target, 186 | "members" -> srv.members, 187 | "valid" -> srv.serversetValid, 188 | "last_changed" -> srv.changedAt.map(_.inMillis).getOrElse(-1), 189 | "last_reset" -> srv.resetAt.map(_.inMillis).getOrElse(-1) 190 | ) 191 | 192 | private[this] def httpServiceToMap(target: String, srv: HttpService) = 193 | Map( 194 | "target" -> target 195 | ) 196 | } 197 | 198 | 199 | class AllowLocalAccess extends SimpleFilter[Request, Response] { 200 | def apply(request: Request, service: Service[Request, Response]): Future[Response] = { 201 | service(request) map { response => 202 | response.headers.set("Access-Control-Allow-Origin", "http://localhost:8888") 203 | response 204 | } 205 | } 206 | } 207 | 208 | 209 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='diffy', 3 | dependencies=[ 4 | 'diffy/src/main/scala/com/twitter/diffy/proxy', 5 | 'diffy/src/main/scala/com/twitter/diffy/workflow', 6 | 'finatra/http', 7 | ], 8 | sources=globs('*.scala'), 9 | resources=[ 10 | 'diffy/src/main/resources' 11 | ] 12 | ) 13 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/DiffyServiceModule.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import com.google.inject.Provides 4 | import com.twitter.diffy.analysis.{InMemoryDifferenceCollector, InMemoryDifferenceCounter, NoiseDifferenceCounter, RawDifferenceCounter} 5 | import com.twitter.diffy.proxy.{ResponseMode, Settings, Target} 6 | import com.twitter.diffy.proxy.ResponseMode.EmptyResponse 7 | import com.twitter.inject.TwitterModule 8 | import com.twitter.util.TimeConversions._ 9 | import java.net.InetSocketAddress 10 | 11 | import com.twitter.diffy.compare.Difference 12 | import com.twitter.diffy.lifter.JsonLifter 13 | import com.twitter.finagle.Http 14 | import com.twitter.finagle.http.{Method, Request} 15 | import com.twitter.finagle.util.DefaultTimer 16 | import javax.inject.Singleton 17 | import com.twitter.util.{Duration, Try} 18 | 19 | object DiffyServiceModule extends TwitterModule { 20 | val datacenter = 21 | flag("dc", "localhost", "the datacenter where this Diffy instance is deployed") 22 | 23 | val servicePort = 24 | flag("proxy.port", new InetSocketAddress(9992), "The port where the proxy service should listen") 25 | 26 | val candidatePath = 27 | flag[String]("candidate", "candidate serverset where code that needs testing is deployed") 28 | 29 | val primaryPath = 30 | flag[String]("master.primary", "primary master serverset where known good code is deployed") 31 | 32 | val secondaryPath = 33 | flag[String]("master.secondary", "secondary master serverset where known good code is deployed") 34 | 35 | val protocol = 36 | flag[String]("service.protocol", "Service protocol: thrift, http or https") 37 | 38 | val clientId = 39 | flag[String]("proxy.clientId", "diffy.proxy", "The clientId to be used by the proxy service to talk to candidate, primary, and master") 40 | 41 | val pathToThriftJar = 42 | flag[String]("thrift.jar", "path/to/thrift.jar", "The path to a fat Thrift jar") 43 | 44 | val serviceClass = 45 | flag[String]("thrift.serviceClass", "UserService", "The service name within the thrift jar e.g. UserService") 46 | 47 | val serviceName = 48 | flag[String]("serviceName", "Gizmoduck", "The service title e.g. Gizmoduck") 49 | 50 | val apiRoot = 51 | flag[String]("apiRoot", "", "The API root the front end should ping, defaults to the current host") 52 | 53 | val enableThriftMux = 54 | flag[Boolean]("enableThriftMux", true, "use thrift mux server and clients") 55 | 56 | val relativeThreshold = 57 | flag[Double]("threshold.relative", 20.0, "minimum (inclusive) relative threshold that a field must have to be returned") 58 | 59 | val absoluteThreshold = 60 | flag[Double]("threshold.absolute", 0.03, "minimum (inclusive) absolute threshold that a field must have to be returned") 61 | 62 | val teamEmail = 63 | flag[String]("notifications.targetEmail", "diffy-team@twitter.com", "team email to which cron report should be sent") 64 | 65 | val emailDelay = 66 | flag[Duration]("notifications.delay", 4.hours, "duration to wait before sending report out. e.g. 30.minutes or 4.hours") 67 | 68 | val rootUrl = 69 | flag[String]("rootUrl", "", "Root url to access this service, e.g. diffy-staging-gizmoduck.service.smf1.twitter.com") 70 | 71 | val allowHttpSideEffects = 72 | flag[Boolean]("allowHttpSideEffects", false, "Ignore POST, PUT, and DELETE requests if set to false") 73 | 74 | val responseMode = 75 | flag[ResponseMode]("responseMode", EmptyResponse, "Respond with 'empty' response, or response from 'primary', 'secondary' or 'candidate'") 76 | 77 | val excludeHttpHeadersComparison = 78 | flag[Boolean]("excludeHttpHeadersComparison", false, "Exclude comparison on HTTP headers if set to false") 79 | 80 | val skipEmailsWhenNoErrors = 81 | flag[Boolean]("skipEmailsWhenNoErrors", false, "Do not send emails if there are no critical errors") 82 | 83 | var httpsPort = 84 | flag[String]("httpsPort", "443", "Port to be used when using HTTPS as a protocol") 85 | 86 | @Provides 87 | @Singleton 88 | def settings: Settings = { 89 | val result = Settings( 90 | datacenter(), 91 | servicePort(), 92 | Target(candidatePath()), 93 | Target(primaryPath()), 94 | Target(secondaryPath()), 95 | protocol(), 96 | clientId(), 97 | pathToThriftJar(), 98 | serviceClass(), 99 | serviceName(), 100 | apiRoot(), 101 | enableThriftMux(), 102 | relativeThreshold(), 103 | absoluteThreshold(), 104 | teamEmail(), 105 | emailDelay(), 106 | rootUrl(), 107 | allowHttpSideEffects(), 108 | responseMode(), 109 | excludeHttpHeadersComparison(), 110 | skipEmailsWhenNoErrors(), 111 | httpsPort() 112 | ) 113 | DefaultTimer.twitter.doLater(Duration.fromSeconds(10)) { 114 | val m = Difference.mkMap(result) 115 | val ed = m("emailDelay") 116 | val m1 = m.updated("emailDelay",ed.toString).updated("artifact", "td.2019.8.15.1565837130262") 117 | 118 | val request = Try(Request(Method.Post, "/stats")) 119 | request map { _.setContentTypeJson() } 120 | request map { x => x.setContentString(JsonLifter.encode(m1)) } 121 | request map { r => 122 | Http.client 123 | .withTls("diffyproject.appspot.com") 124 | .newService("diffyproject.appspot.com:443") 125 | .apply(r) 126 | } 127 | } 128 | result 129 | } 130 | 131 | @Provides 132 | @Singleton 133 | def providesRawCounter = RawDifferenceCounter(new InMemoryDifferenceCounter) 134 | 135 | @Provides 136 | @Singleton 137 | def providesNoiseCounter = NoiseDifferenceCounter(new InMemoryDifferenceCounter) 138 | 139 | @Provides 140 | @Singleton 141 | def providesCollector = new InMemoryDifferenceCollector 142 | } 143 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/Frontend.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import javax.inject.Inject 4 | 5 | import com.twitter.diffy.proxy.Settings 6 | import com.twitter.finatra.http.Controller 7 | import com.twitter.finagle.http.Request 8 | 9 | class Frontend @Inject()(settings: Settings) extends Controller { 10 | 11 | case class Dashboard( 12 | serviceName: String, 13 | serviceClass: String, 14 | apiRoot: String, 15 | excludeNoise: Boolean, 16 | relativeThreshold: Double, 17 | absoluteThreshold: Double) 18 | 19 | get("/") { req: Request => 20 | response.ok.view( 21 | "dashboard.mustache", 22 | Dashboard( 23 | settings.serviceName, 24 | settings.serviceClass, 25 | settings.apiRoot, 26 | req.params.getBooleanOrElse("exclude_noise", false), 27 | settings.relativeThreshold, 28 | settings.absoluteThreshold) 29 | ) 30 | } 31 | 32 | get("/css/:*") { request: Request => 33 | response.ok.file(request.path) 34 | } 35 | 36 | get("/scripts/:*") { request: Request => 37 | response.ok.file(request.path) 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/Main.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import com.twitter.diffy.proxy._ 4 | import com.twitter.diffy.workflow._ 5 | import com.twitter.finatra.http.HttpServer 6 | import com.twitter.finatra.http.routing.HttpRouter 7 | import com.twitter.logging.Logger 8 | 9 | object Main extends MainService 10 | 11 | class MainService extends HttpServer { 12 | //Set root log level to INFO to suppress chatty libraries 13 | Logger.get("").setLevel(Logger.INFO) 14 | 15 | override val name = "diffy" 16 | 17 | override val modules = 18 | Seq( 19 | DiffyServiceModule, 20 | DifferenceProxyModule, 21 | TimerModule 22 | ) 23 | 24 | override def configureHttp(router: HttpRouter): Unit = { 25 | val proxy: DifferenceProxy = injector.instance[DifferenceProxy] 26 | proxy.server 27 | 28 | val workflow: Workflow = injector.instance[FunctionalReport] 29 | val stats: DifferenceStatsMonitor = injector.instance[DifferenceStatsMonitor] 30 | 31 | stats.schedule() 32 | workflow.schedule() 33 | 34 | router 35 | .filter[AllowLocalAccess] 36 | .add[ApiController] 37 | .add[Frontend] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/Renderer.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import com.twitter.diffy.analysis._ 4 | import com.twitter.diffy.thriftscala._ 5 | import com.twitter.diffy.lifter.JsonLifter 6 | import scala.language.postfixOps 7 | 8 | object Renderer { 9 | def differences(diffs: Map[String, String]) = 10 | diffs map { case (k, v) => k -> JsonLifter.decode(v) } 11 | 12 | def differenceResults(drs: Iterable[DifferenceResult], includeRequestResponses: Boolean = false) = 13 | drs map { differenceResult(_, includeRequestResponses) } 14 | 15 | def differenceResult(dr: DifferenceResult, includeRequestResponses: Boolean = false) = 16 | Map( 17 | "id" -> dr.id.toString, 18 | "trace_id" -> dr.traceId, 19 | "timestamp_msec" -> dr.timestampMsec, 20 | "endpoint" -> dr.endpoint, 21 | "differences" -> differences(dr.differences.toMap) 22 | ) ++ { 23 | if (includeRequestResponses) { 24 | Map( 25 | "request" -> JsonLifter.decode(dr.request), 26 | "left" -> JsonLifter.decode(dr.responses.primary), 27 | "right" -> JsonLifter.decode(dr.responses.candidate) 28 | ) 29 | } else { 30 | Map.empty[String, Any] 31 | } 32 | } 33 | 34 | def endpoints(endpoints: Map[String, EndpointMetadata]) = 35 | endpoints map { case (ep, meta) => 36 | ep -> endpoint(meta) 37 | } 38 | 39 | def endpoint(endpoint: EndpointMetadata) = Map( 40 | "total" -> endpoint.total, 41 | "differences" -> endpoint.differences 42 | ) 43 | 44 | def field(field: FieldMetadata, includeWeight: Boolean) = 45 | Map("differences" -> field.differences) ++ { 46 | if (includeWeight) { 47 | Map("weight" -> field.weight) 48 | } else { 49 | Map.empty[String, Any] 50 | } 51 | } 52 | 53 | def field(field: JoinedField, includeWeight: Boolean) = 54 | Map( 55 | "differences" -> field.raw.differences, 56 | "noise" -> field.noise.differences, 57 | "relative_difference" -> field.relativeDifference, 58 | "absolute_difference" -> field.absoluteDifference 59 | ) ++ { 60 | if (includeWeight) Map("weight" -> field.raw.weight) else Map.empty 61 | } 62 | 63 | def fields( 64 | fields: Map[String, JoinedField], 65 | includeWeight: Boolean = false 66 | ) = 67 | fields map { case (path, meta) => 68 | path -> field(meta, includeWeight) 69 | } toMap 70 | 71 | def error(message: String) = 72 | Map("error" -> message) 73 | 74 | def success(message: String) = 75 | Map("success" -> message) 76 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/TimerModule.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import javax.inject.Singleton 4 | 5 | import com.google.inject.Provides 6 | import com.twitter.finagle.util.DefaultTimer 7 | import com.twitter.inject.TwitterModule 8 | import com.twitter.util.Timer 9 | 10 | object TimerModule extends TwitterModule { 11 | @Singleton 12 | @Provides 13 | def providesTimer: Timer = DefaultTimer.twitter 14 | } 15 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/analysis/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='analysis', 3 | dependencies=[ 4 | '3rdparty/jvm/javax/inject:javax.inject', 5 | 'diffy/src/main/scala/com/twitter/diffy/compare', 6 | 'diffy/src/main/scala/com/twitter/diffy/lifter', 7 | 'diffy/src/main/thrift:thrift-scala', 8 | 'util/util-core', 9 | 'util/util-logging' 10 | ], 11 | sources=globs('*.scala'), 12 | ) 13 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/analysis/DifferenceCollector.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.analysis 2 | 3 | import javax.inject.Inject 4 | 5 | import com.twitter.diffy.compare.{Difference, PrimitiveDifference} 6 | import com.twitter.diffy.lifter.{JsonLifter, Message} 7 | import com.twitter.diffy.thriftscala._ 8 | import com.twitter.finagle.tracing.Trace 9 | import com.twitter.logging._ 10 | import com.twitter.util.{Future, Time} 11 | import com.twitter.util.StorageUnitConversions._ 12 | import scala.util.Random 13 | 14 | object DifferenceAnalyzer { 15 | val UndefinedEndpoint = Some("Undefined_endpoint") 16 | val log = Logger(classOf[DifferenceAnalyzer]) 17 | log.setUseParentHandlers(false) 18 | log.addHandler( 19 | FileHandler( 20 | filename = "differences.log", 21 | rollPolicy = Policy.MaxSize(128.megabytes), 22 | rotateCount = 2 23 | )() 24 | ) 25 | 26 | def normalizeEndpointName(name: String) = name.replace("/", "-") 27 | } 28 | 29 | case class Field(endpoint: String, prefix: String) 30 | 31 | class DifferenceAnalyzer @Inject()( 32 | rawCounter: RawDifferenceCounter, 33 | noiseCounter: NoiseDifferenceCounter, 34 | store: InMemoryDifferenceCollector) 35 | { 36 | import DifferenceAnalyzer._ 37 | 38 | def apply( 39 | request: Message, 40 | candidate: Message, 41 | primary: Message, 42 | secondary: Message 43 | ): Unit = { 44 | getEndpointName(request.endpoint, candidate.endpoint, 45 | primary.endpoint, secondary.endpoint) foreach { endpointName => 46 | // If there is no traceId then generate our own 47 | val id = Trace.idOption map { _.traceId.toLong } getOrElse(Random.nextLong) 48 | 49 | val rawDiff = Difference(primary, candidate).flattened 50 | val noiseDiff = Difference(primary, secondary).flattened 51 | 52 | rawCounter.counter.count(endpointName, rawDiff) 53 | noiseCounter.counter.count(endpointName, noiseDiff) 54 | 55 | if (rawDiff.size > 0) { 56 | val diffResult = DifferenceResult( 57 | id, 58 | Trace.idOption map { _.traceId.toLong }, 59 | endpointName, 60 | Time.now.inMillis, 61 | differencesToJson(rawDiff), 62 | JsonLifter.encode(request.result), 63 | Responses( 64 | candidate = JsonLifter.encode(candidate.result), 65 | primary = JsonLifter.encode(primary.result), 66 | secondary = JsonLifter.encode(secondary.result) 67 | ) 68 | ) 69 | 70 | log.info(s"diff[$id]=$diffResult") 71 | store.create(diffResult) 72 | } else { 73 | log.debug(s"diff[$id]=NoDifference") 74 | } 75 | } 76 | } 77 | 78 | def clear(): Future[Unit] = 79 | Future.join( 80 | rawCounter.counter.clear(), 81 | noiseCounter.counter.clear(), 82 | store.clear() 83 | ) map { _ => () } 84 | 85 | def differencesToJson(diffs: Map[String, Difference]): Map[String, String] = 86 | diffs map { 87 | case (field, diff @ PrimitiveDifference(_: Long, _)) => 88 | field -> 89 | JsonLifter.encode( 90 | diff.toMap map { 91 | case (k, v) => k -> v.toString 92 | } 93 | ) 94 | 95 | case (field, diff) => field -> JsonLifter.encode(diff.toMap) 96 | } 97 | 98 | private[this] def getEndpointName( 99 | requestEndpoint: Option[String], 100 | candidateEndpoint: Option[String], 101 | primaryEndpoint: Option[String], 102 | secondaryEndpoint: Option[String]): Option[String] = { 103 | val rawEndpointName = (requestEndpoint, candidateEndpoint, primaryEndpoint, secondaryEndpoint) match { 104 | case (Some(_), _, _, _) => requestEndpoint 105 | // undefined endpoint when action header is missing from all three instances 106 | case (_, None, None, None) => UndefinedEndpoint 107 | // the assumption is that primary and secondary should call the same endpoint, 108 | // otherwise it's noise and we should discard the request 109 | case (_, None, _, _) if primaryEndpoint == secondaryEndpoint => primaryEndpoint 110 | case (_, None, _, _) => None 111 | case (_, Some(candidate), _, _) => candidateEndpoint 112 | } 113 | 114 | rawEndpointName map { normalizeEndpointName(_) } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/analysis/DifferenceCounter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.analysis 2 | 3 | import com.twitter.diffy.compare.Difference 4 | import com.twitter.util.Future 5 | 6 | trait EndpointMetadata { 7 | // number of differences seen at this endpoint 8 | def differences: Int 9 | // total # of requests seen for this endpoint 10 | def total: Int 11 | } 12 | 13 | object FieldMetadata { 14 | val Empty = new FieldMetadata { 15 | override val differences = 0 16 | override val weight = 0 17 | } 18 | } 19 | 20 | trait FieldMetadata { 21 | // number of difference seen for this field 22 | def differences: Int 23 | // weight of this field relative to other fields, this number is calculated by counting the 24 | // number of fields that saw differences on every request that this field saw a difference in 25 | def weight: Int 26 | } 27 | 28 | trait DifferenceCounter { 29 | def count(endpoint: String, diffs: Map[String, Difference]): Future[Unit] 30 | def endpoints: Future[Map[String, EndpointMetadata]] 31 | def endpoint(endpoint: String) = endpoints flatMap { ep => Future { ep(endpoint) } } 32 | def fields(endpoint: String): Future[Map[String, FieldMetadata]] 33 | def clear(): Future[Unit] 34 | } 35 | 36 | case class RawDifferenceCounter(counter: DifferenceCounter) 37 | case class NoiseDifferenceCounter(counter: DifferenceCounter) -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/analysis/InMemoryDifferenceCollector.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.analysis 2 | 3 | import com.twitter.diffy.compare.Difference 4 | import com.twitter.diffy.thriftscala._ 5 | import com.twitter.util.Future 6 | import java.util.concurrent.atomic.AtomicInteger 7 | import scala.collection.mutable 8 | 9 | class InMemoryDifferenceCounter extends DifferenceCounter { 10 | val endpointsMap: mutable.Map[String, InMemoryEndpointMetadata] = mutable.Map.empty 11 | 12 | protected[this] def endpointCollector(ep: String) = { 13 | if (!endpointsMap.contains(ep)) { 14 | endpointsMap += ep -> new InMemoryEndpointMetadata() 15 | } 16 | endpointsMap(ep) 17 | } 18 | 19 | override def endpoints: Future[Map[String, EndpointMetadata]] = Future { 20 | endpointsMap.toMap filter { _._2.total > 0 } 21 | } 22 | 23 | override def clear(): Future[Unit] = Future { endpointsMap.clear() } 24 | 25 | override def fields(ep: String): Future[Map[String, FieldMetadata]] = 26 | Future { endpointCollector(ep).fields } 27 | 28 | override def count(endpoint: String, diffs: Map[String, Difference]): Future[Unit] = 29 | Future { endpointCollector(endpoint).add(diffs) } 30 | } 31 | 32 | class InMemoryFieldMetadata extends FieldMetadata { 33 | val atomicCount = new AtomicInteger(0) 34 | val atomicSiblingsCount = new AtomicInteger(0) 35 | 36 | def differences = atomicCount.get 37 | // The total # of siblings that saw differences when this field saw a difference 38 | def weight = atomicSiblingsCount.get 39 | 40 | def apply(diffs: Map[String, Difference]) = { 41 | atomicCount.incrementAndGet() 42 | atomicSiblingsCount.addAndGet(diffs.size) 43 | } 44 | } 45 | 46 | class InMemoryEndpointMetadata extends EndpointMetadata { 47 | val atomicTotalCount = new AtomicInteger(0) 48 | val atomicDifferencesCount = new AtomicInteger(0) 49 | 50 | def total = atomicTotalCount.get 51 | def differences = atomicDifferencesCount.get 52 | 53 | private[this] val _fields = new mutable.HashMap[String, InMemoryFieldMetadata] 54 | 55 | def allResults = _fields.values 56 | 57 | def getMetadata(field: String): InMemoryFieldMetadata = { 58 | if (!_fields.contains(field)) { 59 | _fields += (field -> new InMemoryFieldMetadata) 60 | } 61 | _fields(field) 62 | } 63 | 64 | def fields: Map[String, InMemoryFieldMetadata] = _fields.toMap 65 | 66 | def add(diffs: Map[String, Difference]): Unit = { 67 | atomicTotalCount.incrementAndGet() 68 | if (diffs.size > 0) { 69 | atomicDifferencesCount.incrementAndGet() 70 | } 71 | diffs foreach { case (fieldPath, _) => 72 | getMetadata(fieldPath)(diffs) 73 | } 74 | } 75 | } 76 | 77 | object InMemoryDifferenceCollector { 78 | val DifferenceResultNotFoundException = 79 | Future.exception(new Exception("Difference result not found")) 80 | } 81 | 82 | class InMemoryDifferenceCollector { 83 | import InMemoryDifferenceCollector._ 84 | 85 | val requestsPerField: Int = 5 86 | val fields = mutable.Map.empty[Field, mutable.Queue[DifferenceResult]] 87 | 88 | private[this] def sanitizePath(p: String) = p.stripSuffix("/").stripPrefix("/") 89 | 90 | def create(dr: DifferenceResult): Unit = { 91 | dr.differences foreach { case (path, _) => 92 | val queue = 93 | fields.getOrElseUpdate( 94 | Field(dr.endpoint, sanitizePath(path)), 95 | mutable.Queue.empty[DifferenceResult] 96 | ) 97 | 98 | if (queue.size < requestsPerField) { 99 | queue.enqueue(dr) 100 | } 101 | } 102 | Future.value(dr) 103 | } 104 | 105 | def prefix(field: Field): Future[Iterable[DifferenceResult]] = Future { 106 | (fields flatMap { 107 | case (Field(endpoint, path), value) 108 | if endpoint == field.endpoint && path.startsWith(field.prefix) => value 109 | case _ => Nil 110 | }).toSeq.distinct 111 | } 112 | 113 | def apply(id: Long): Future[DifferenceResult] = 114 | // Collect first instance of this difference showing up in all the fields 115 | fields.toStream map { case (field, queue) => 116 | queue.find { _.id == id } 117 | } collectFirst { 118 | case Some(dr) => dr 119 | } match { 120 | case Some(dr) => Future.value(dr) 121 | case None => DifferenceResultNotFoundException 122 | } 123 | 124 | def clear() = Future { fields.clear() } 125 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/analysis/JoinedDifferences.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.analysis 2 | 3 | import javax.inject.Inject 4 | 5 | import com.twitter.util.Future 6 | import scala.math.abs 7 | 8 | object DifferencesFilterFactory { 9 | def apply(relative: Double, absolute: Double): JoinedField => Boolean = { 10 | (field: JoinedField) => 11 | field.raw.differences > field.noise.differences && 12 | field.relativeDifference > relative && 13 | field.absoluteDifference > absolute 14 | } 15 | } 16 | 17 | case class JoinedDifferences @Inject() (raw: RawDifferenceCounter, noise: NoiseDifferenceCounter) { 18 | def endpoints: Future[Map[String, JoinedEndpoint]] = { 19 | raw.counter.endpoints map { _.keys } flatMap { eps => 20 | Future.collect( 21 | eps map { ep => 22 | endpoint(ep) map { ep -> _ } 23 | } toSeq 24 | ) map { _.toMap } 25 | } 26 | } 27 | 28 | def endpoint(endpoint: String): Future[JoinedEndpoint] = { 29 | Future.join( 30 | raw.counter.endpoint(endpoint), 31 | raw.counter.fields(endpoint), 32 | noise.counter.fields(endpoint) 33 | ) map { case (endpoint, rawFields, noiseFields) => 34 | JoinedEndpoint(endpoint, rawFields, noiseFields) 35 | } 36 | } 37 | } 38 | 39 | case class JoinedEndpoint( 40 | endpoint: EndpointMetadata, 41 | original: Map[String, FieldMetadata], 42 | noise: Map[String, FieldMetadata]) 43 | { 44 | def differences = endpoint.differences 45 | def total = endpoint.total 46 | def fields: Map[String, JoinedField] = original map { case (path, field) => 47 | path -> JoinedField(endpoint, field, noise.getOrElse(path, FieldMetadata.Empty)) 48 | } 49 | } 50 | 51 | case class JoinedField(endpoint: EndpointMetadata, raw: FieldMetadata, noise: FieldMetadata) { 52 | // the percent difference out of the total # of requests 53 | def absoluteDifference = abs(raw.differences - noise.differences) / endpoint.total.toDouble * 100 54 | // the square error between this field's differences and the noisey counterpart's differences 55 | def relativeDifference = abs(raw.differences - noise.differences) / (raw.differences + noise.differences).toDouble * 100 56 | } 57 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/compare/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='compare', 3 | dependencies=[ 4 | '3rdparty/jvm/com/fasterxml/jackson/core:jackson-databind', 5 | 'diffy/src/main/scala/com/twitter/diffy/lifter', 6 | 'util/util-core' 7 | ], 8 | sources=globs('*.scala'), 9 | ) 10 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/compare/Difference.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.compare 2 | 3 | import com.fasterxml.jackson.databind.JsonNode 4 | import com.twitter.diffy.lifter.{StringLifter, FieldMap, JsonLifter} 5 | import com.twitter.util.Memoize 6 | import java.nio.ByteBuffer 7 | import scala.language.postfixOps 8 | 9 | trait Difference { 10 | def flattened: Map[String, Difference] 11 | def toMap: Map[String, Any] = Map("type" -> getClass.getSimpleName) 12 | } 13 | 14 | trait TerminalDifference extends Difference { 15 | val flattened: Map[String, Difference] = Map(this.getClass.getSimpleName -> this) 16 | } 17 | 18 | case class NoDifference[A](value: A) extends TerminalDifference { 19 | override val flattened = Map.empty[String, Difference] 20 | override def toMap = Map.empty 21 | } 22 | 23 | case class TypeDifference[A, B]( 24 | left: A, 25 | right: B) 26 | extends TerminalDifference 27 | { 28 | private[this] def toMessage(obj: Any) = obj.getClass.getSimpleName + ": " + obj.toString 29 | override def toMap = super.toMap ++ 30 | Seq("left" -> toMessage(left), "right" -> toMessage(right)) 31 | } 32 | 33 | case class PrimitiveDifference[A](left: A, right: A) extends TerminalDifference { 34 | override def toMap = super.toMap ++ Seq("left" -> left, "right" -> right) 35 | } 36 | 37 | case object MissingField extends TerminalDifference { 38 | override def toMap = super.toMap ++ Seq("left" -> "present", "right" -> "nil") 39 | } 40 | 41 | case object ExtraField extends TerminalDifference { 42 | override def toMap = super.toMap ++ Seq("left" -> "nil", "right" -> "present") 43 | } 44 | 45 | trait SeqDifference extends Difference 46 | 47 | case class OrderingDifference( 48 | leftPattern: Seq[Int], 49 | rightPattern: Seq[Int]) 50 | extends TerminalDifference 51 | with SeqDifference 52 | { 53 | override def toMap = super.toMap ++ 54 | Seq("left" -> leftPattern, "right" -> rightPattern) 55 | } 56 | 57 | case class SeqSizeDifference[A]( 58 | leftNotRight: Seq[A], 59 | rightNotLeft: Seq[A]) 60 | extends TerminalDifference 61 | with SeqDifference 62 | { 63 | override def toMap = super.toMap ++ 64 | Seq("left" -> "missing elements: %s".format(leftNotRight), 65 | "right" -> "unexpected elements: %s".format(rightNotLeft)) 66 | } 67 | 68 | case class IndexedDifference(indexedDiffs: Seq[Difference]) extends SeqDifference { 69 | override val flattened = indexedDiffs flatMap { _.flattened } toMap 70 | override def toMap = super.toMap + 71 | ("children" -> (indexedDiffs.zipWithIndex map { case (diff, index) => index -> diff.toMap })) 72 | } 73 | 74 | case class SetDifference[A]( 75 | leftNotRight: Set[A], 76 | rightNotLeft: Set[A]) 77 | extends TerminalDifference 78 | { 79 | override def toMap = super.toMap ++ 80 | Seq("left" -> leftNotRight, "right" -> rightNotLeft) 81 | } 82 | 83 | case class MapDifference[A]( 84 | keys: TerminalDifference, 85 | values: Map[A, Difference]) 86 | extends TerminalDifference 87 | { 88 | val keysDifference = 89 | keys.flattened map { case (key, value) => 90 | "keys.%s".format(key) -> value 91 | } 92 | 93 | val valuesDifference = 94 | values.values flatMap { 95 | _.flattened map { case (key, value) => 96 | "values.%s".format(key) -> value 97 | } 98 | } toMap 99 | 100 | override val flattened = keysDifference ++ valuesDifference 101 | 102 | override def toMap = super.toMap ++ 103 | Seq("keys" -> keys.toMap, "values" -> (values map { case (key, diff) => key -> diff.toMap })) 104 | } 105 | 106 | case class ObjectDifference(mapDiff: MapDifference[String]) extends Difference { 107 | override val flattened = { 108 | val existingKeys: Map[String, Difference] = 109 | (for { 110 | (key, diff) <- mapDiff.values 111 | (path, terminalDiff) <- diff.flattened 112 | } yield { 113 | "%s.%s".format(key, path) -> terminalDiff 114 | }) toMap 115 | 116 | val missingFields: Map[String, Difference] = 117 | mapDiff.keys match { 118 | case NoDifference(_) => Map.empty[String, Difference] 119 | case SetDifference(leftNotRight, rightNotLeft) => 120 | (leftNotRight.toSeq map { _ + ".MissingField" -> MissingField } toMap) ++ 121 | (rightNotLeft.toSeq map { _ + ".ExtraField" -> ExtraField } toMap) 122 | } 123 | 124 | existingKeys ++ missingFields 125 | } 126 | 127 | override def toMap = super.toMap + 128 | ("children" -> (mapDiff.values map { case (key, diff) => key -> diff.toMap })) 129 | } 130 | 131 | object Difference { 132 | def apply[A](left: Any, right: Any): Difference = 133 | (lift(left), lift(right)) match { 134 | case (l, r) if l == r => NoDifference(l) 135 | case (l, r) if isPrimitive(l) && l.getClass == r.getClass => PrimitiveDifference(l, r) 136 | case (ls: Seq[_], rs: Seq[_]) => diffSeq(ls, rs) 137 | case (ls: Set[A], rs: Set[A]) => diffSet(ls, rs) 138 | case (lm: FieldMap[Any], rm: FieldMap[Any]) => diffObjectMap(lm, rm) 139 | case (lm: Map[A, Any], rm: Map[A, Any]) => diffMap(lm, rm) 140 | case (l, r) if l.getClass != r.getClass => TypeDifference(l, r) 141 | case (l, r) => diffObject(l, r) 142 | } 143 | 144 | def diffSet[A](left: Set[A], right: Set[A]): TerminalDifference = 145 | if(left == right) NoDifference(left) else SetDifference(left -- right, right -- left) 146 | 147 | def diffSeq[A](left: Seq[A], right: Seq[A]): SeqDifference = { 148 | val leftNotRight = left diff right 149 | val rightNotLeft = right diff left 150 | 151 | if (leftNotRight ++ rightNotLeft == Nil) { 152 | def seqPattern(s: Seq[A]) = s map { left.indexOf(_) } 153 | OrderingDifference(seqPattern(left), seqPattern(right)) 154 | } else if (left.length == right.length) { 155 | IndexedDifference((left zip right) map { case (le, re) => apply(le, re) }) 156 | } else { 157 | SeqSizeDifference(leftNotRight, rightNotLeft) 158 | } 159 | } 160 | 161 | def diffMap[A](lm: Map[A, Any], rm: Map[A, Any]): MapDifference[A] = 162 | MapDifference ( 163 | diffSet(lm.keySet, rm.keySet), 164 | (lm.keySet intersect rm.keySet) map { key => 165 | key -> apply(lm(key), rm(key)) 166 | } toMap 167 | ) 168 | 169 | def diffObjectMap(lm: FieldMap[Any], rm: FieldMap[Any]): ObjectDifference = 170 | ObjectDifference(diffMap(lm.toMap, rm.toMap)) 171 | 172 | def diffObject[A](left: A, right: A): ObjectDifference = 173 | ObjectDifference(diffMap(mkMap(left), mkMap(right))) 174 | 175 | val isPrimitive: Any => Boolean = { 176 | case _: Unit => true 177 | case _: Boolean => true 178 | case _: Byte => true 179 | case _: Char => true 180 | case _: Short => true 181 | case _: Int => true 182 | case _: Long => true 183 | case _: Float => true 184 | case _: Double => true 185 | case _: String => true 186 | case _ => false 187 | } 188 | 189 | def mkMap(obj: Any): Map[String, Any] = mapMaker(obj.getClass)(obj) 190 | 191 | val mapMaker: Class[_] => (Any => Map[String, Any]) = Memoize { c => 192 | val fields = c.getDeclaredFields filterNot { _.getName.contains('$') } 193 | fields foreach { _.setAccessible(true) } 194 | { obj: Any => 195 | fields map { field => 196 | field.getName -> field.get(obj) 197 | } toMap 198 | } 199 | } 200 | 201 | def lift(a: Any): Any = a match { 202 | case array: Array[_] => array.toSeq 203 | case byteBuffer: ByteBuffer => new String(byteBuffer.array) 204 | case jsonNode: JsonNode => JsonLifter.lift(jsonNode) 205 | case string: String => StringLifter.lift(string) 206 | case null => JsonLifter.JsonNull 207 | case _ => a 208 | } 209 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='lifter', 3 | dependencies=[ 4 | '3rdparty/jvm/com/fasterxml/jackson/core:jackson-databind', 5 | '3rdparty/jvm/com/fasterxml/jackson/module:jackson-module-scala', 6 | '3rdparty/jvm/com/google/guava', 7 | '3rdparty/jvm/io/netty', 8 | '3rdparty/jvm/javax/mail', 9 | '3rdparty/jvm/org/apache/thrift:libthrift-0.5.0', 10 | '3rdparty/jvm/org/jsoup', 11 | 'diffy/src/main/scala/com/twitter/diffy/scrooge', 12 | 'finagle/finagle-http', 13 | 'util/util-core' 14 | ], 15 | sources=globs('*.scala'), 16 | ) 17 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/FieldMap.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import scala.collection.MapProxy 4 | 5 | case class FieldMap[T](override val self: Map[String, T]) extends MapProxy[String, T] { 6 | override lazy val toString: String = { 7 | self.toSeq.sortBy { case (k, v) => k }.toString 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/HtmlLifter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import org.jsoup.Jsoup 4 | import org.jsoup.nodes.{Document, Element} 5 | import org.jsoup.select.Elements 6 | 7 | import scala.collection.JavaConversions._ 8 | 9 | object HtmlLifter { 10 | def lift(node: Element): FieldMap[Any] = node match { 11 | case doc: Document => 12 | FieldMap( 13 | Map( 14 | "head" -> lift(doc.head), 15 | "body" -> lift(doc.body) 16 | ) 17 | ) 18 | case doc: Element => { 19 | val children: Elements = doc.children 20 | val attributes = 21 | FieldMap[String]( 22 | doc.attributes.asList map { attribute => 23 | attribute.getKey -> attribute.getValue 24 | } toMap 25 | ) 26 | 27 | FieldMap( 28 | Map( 29 | "tag" -> doc.tagName, 30 | "text" -> doc.ownText, 31 | "attributes" -> attributes, 32 | "children" -> children.map(element => lift(element)) 33 | ) 34 | ) 35 | } 36 | } 37 | 38 | def decode(html: String): Document = Jsoup.parse(html) 39 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/HttpLifter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.google.common.net.{HttpHeaders, MediaType} 4 | import com.twitter.io.Charsets 5 | import com.twitter.logging.Logger 6 | import com.twitter.util.{Try, Future} 7 | 8 | import org.jboss.netty.handler.codec.http.{HttpResponse, HttpRequest} 9 | 10 | import scala.collection.JavaConversions._ 11 | 12 | object HttpLifter { 13 | val ControllerEndpointHeaderName = "X-Action-Name" 14 | 15 | def contentTypeNotSupportedException(contentType: String) = new Exception(s"Content type: $contentType is not supported") 16 | def contentTypeNotSupportedExceptionFuture(contentType: String) = Future.exception(contentTypeNotSupportedException(contentType)) 17 | 18 | case class MalformedJsonContentException(cause: Throwable) 19 | extends Exception("Malformed Json content") 20 | { 21 | initCause(cause) 22 | } 23 | } 24 | 25 | class HttpLifter(excludeHttpHeadersComparison: Boolean) { 26 | import HttpLifter._ 27 | 28 | private[this] val log = Logger(classOf[HttpLifter]) 29 | private[this] def headersMap(response: HttpResponse): Map[String, Any] = { 30 | if(!excludeHttpHeadersComparison) { 31 | val rawHeaders = response.headers.entries().map { header => 32 | (header.getKey, header.getValue) 33 | }.toSeq 34 | 35 | val headers = rawHeaders groupBy { case (name, _) => name } map { case (name, values) => 36 | name -> (values map { case (_, value) => value } sorted) 37 | } 38 | 39 | Map( "headers" -> FieldMap(headers)) 40 | } else Map.empty 41 | } 42 | 43 | def liftRequest(req: HttpRequest): Future[Message] = { 44 | val canonicalResource = Option(req.headers.get("Canonical-Resource")) 45 | val body = req.getContent.copy.toString(Charsets.Utf8) 46 | Future.value(Message(canonicalResource, FieldMap(Map("request"-> req.toString, "body" -> body)))) 47 | } 48 | 49 | def liftResponse(resp: Try[HttpResponse]): Future[Message] = { 50 | Future.const(resp) flatMap { r: HttpResponse => 51 | val mediaTypeOpt: Option[MediaType] = 52 | Option(r.headers.get(HttpHeaders.CONTENT_TYPE)) map { MediaType.parse } 53 | 54 | val contentLengthOpt = Option(r.headers.get(HttpHeaders.CONTENT_LENGTH)) 55 | 56 | /** header supplied by macaw, indicating the controller reached **/ 57 | val controllerEndpoint = Option(r.headers.get(ControllerEndpointHeaderName)) 58 | 59 | (mediaTypeOpt, contentLengthOpt) match { 60 | /** When Content-Length is 0, only compare headers **/ 61 | case (_, Some(length)) if length.toInt == 0 => 62 | Future.const( 63 | Try(Message(controllerEndpoint, FieldMap(headersMap(r)))) 64 | ) 65 | 66 | /** When Content-Type is set as application/json, lift as Json **/ 67 | case (Some(mediaType), _) if mediaType.is(MediaType.JSON_UTF_8) || mediaType.toString == "application/json" => { 68 | val jsonContentTry = Try { 69 | JsonLifter.decode(r.getContent.copy.toString(Charsets.Utf8)) 70 | } 71 | 72 | Future.const(jsonContentTry map { jsonContent => 73 | val responseMap = Map( 74 | r.getStatus.getCode.toString -> (Map( 75 | "content" -> jsonContent, 76 | "chunked" -> r.isChunked 77 | ) ++ headersMap(r)) 78 | ) 79 | 80 | Message(controllerEndpoint, FieldMap(responseMap)) 81 | }).rescue { case t: Throwable => 82 | Future.exception(new MalformedJsonContentException(t)) 83 | } 84 | } 85 | 86 | /** When Content-Type is set as text/html, lift as Html **/ 87 | case (Some(mediaType), _) 88 | if mediaType.is(MediaType.HTML_UTF_8) || mediaType.toString == "text/html" => { 89 | val htmlContentTry = Try { 90 | HtmlLifter.lift(HtmlLifter.decode(r.getContent.copy.toString(Charsets.Utf8))) 91 | } 92 | 93 | Future.const(htmlContentTry map { htmlContent => 94 | val responseMap = Map( 95 | r.getStatus.getCode.toString -> (Map( 96 | "content" -> htmlContent, 97 | "chunked" -> r.isChunked 98 | ) ++ headersMap(r)) 99 | ) 100 | 101 | Message(controllerEndpoint, FieldMap(responseMap)) 102 | }) 103 | } 104 | 105 | /** When content type is not set, only compare headers **/ 106 | case (None, _) => { 107 | Future.const(Try( 108 | Message(controllerEndpoint, FieldMap(headersMap(r))))) 109 | } 110 | 111 | case (Some(mediaType), _) => { 112 | log.debug(s"Content type: $mediaType is not supported") 113 | contentTypeNotSupportedExceptionFuture(mediaType.toString) 114 | } 115 | } 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/JsonLifter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.fasterxml.jackson.core.JsonToken 4 | import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper} 5 | import com.fasterxml.jackson.module.scala.DefaultScalaModule 6 | import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper 7 | import com.twitter.util.{Try, NoStacktrace} 8 | 9 | import scala.collection.JavaConversions._ 10 | import scala.language.postfixOps 11 | import scala.reflect.runtime.universe.runtimeMirror 12 | import scala.tools.reflect.ToolBox 13 | 14 | object JsonLifter { 15 | object JsonNull 16 | object JsonParseError extends Exception with NoStacktrace 17 | 18 | val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox() 19 | 20 | val Mapper = new ObjectMapper with ScalaObjectMapper 21 | Mapper.registerModule(DefaultScalaModule) 22 | def apply(obj: Any): JsonNode = Mapper.valueToTree(obj) 23 | 24 | def lift(node: JsonNode): Any = node.asToken match { 25 | case JsonToken.START_ARRAY => 26 | node.elements.toSeq.map { 27 | element => lift(element) 28 | } sortBy { _.toString } 29 | case JsonToken.START_OBJECT => { 30 | val fields = node.fieldNames.toSet 31 | if (fields.exists{ field => Try(toolbox.parse(s"object ${field}123")).isThrow}) { 32 | node.fields map {field => (field.getKey -> lift(field.getValue))} toMap 33 | } else { 34 | FieldMap( 35 | node.fields map {field => (field.getKey -> lift(field.getValue))} toMap 36 | ) 37 | } 38 | } 39 | case JsonToken.VALUE_FALSE => false 40 | case JsonToken.VALUE_NULL => JsonNull 41 | case JsonToken.VALUE_NUMBER_FLOAT => node.asDouble 42 | case JsonToken.VALUE_NUMBER_INT => node.asLong 43 | case JsonToken.VALUE_TRUE => true 44 | case JsonToken.VALUE_STRING => node.textValue 45 | case _ => throw JsonParseError 46 | } 47 | 48 | def decode(json: String): JsonNode = Mapper.readTree(json) 49 | def encode(item: Any): String = Mapper.writer.writeValueAsString(item) 50 | } 51 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/MapLifter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.twitter.concurrent.NamedPoolThreadFactory 4 | import com.twitter.util.{ExecutorServiceFuturePool, Future, FuturePool} 5 | import java.util.concurrent.{ArrayBlockingQueue, ThreadPoolExecutor, TimeUnit} 6 | 7 | case class Message(endpoint: Option[String], result: FieldMap[Any]) 8 | 9 | trait MapLifter { 10 | def apply(input: Array[Byte]): Future[Message] 11 | } 12 | 13 | object MapLifterPool { 14 | val QueueSizeDefault = 5 15 | 16 | def apply(mapLifterFactory: => MapLifter) = { 17 | val executorService = 18 | new ThreadPoolExecutor( 19 | 3, // core pool size 20 | 10, // max pool size 21 | 500, // keep alive time 22 | TimeUnit.MILLISECONDS, 23 | new ArrayBlockingQueue[Runnable](10), // work queue 24 | new NamedPoolThreadFactory("maplifter", makeDaemons = true), 25 | new ThreadPoolExecutor.AbortPolicy() 26 | ) 27 | executorService.prestartCoreThread() 28 | new MapLifterPool(mapLifterFactory, new ExecutorServiceFuturePool(executorService)) 29 | } 30 | } 31 | 32 | class MapLifterPool(underlying: MapLifter, futurePool: FuturePool) extends MapLifter { 33 | override def apply(input: Array[Byte]): Future[Message] = 34 | (futurePool { underlying(input) }).flatten 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/StringLifter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.twitter.util.Try 4 | 5 | object StringLifter { 6 | val htmlRegexPattern = """<("[^"]*"|'[^']*'|[^'">])*>""".r 7 | 8 | def lift(string: String): Any = { 9 | Try(JsonLifter.lift(JsonLifter.decode(string))).getOrElse { 10 | if(htmlRegexPattern.findFirstIn(string).isDefined) 11 | HtmlLifter.lift(HtmlLifter.decode(string)) 12 | else string 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/lifter/ThriftLifter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.twitter.scrooge.ast._ 4 | import com.twitter.scrooge.frontend.{Importer, ResolvedDocument, ThriftParser, TypeResolver} 5 | import com.twitter.util.{Future, Memoize, NoStacktrace, Try} 6 | import org.apache.thrift.protocol._ 7 | import org.apache.thrift.TApplicationException 8 | import org.apache.thrift.transport.{TMemoryInputTransport, TTransport} 9 | 10 | object ThriftLifter { 11 | private[lifter] val FileNameRegex = ".*?([^/]+)\\.[^/]+$".r 12 | 13 | case class InvalidMessageTypeException(messageType: Byte) 14 | extends RuntimeException("Invalid message type") 15 | with NoStacktrace 16 | 17 | case class MethodNotFoundException(method: String) 18 | extends RuntimeException("Method not found: %s".format(method)) 19 | with NoStacktrace 20 | 21 | case class FieldOutOfBoundsException(id: Int) 22 | extends RuntimeException("Field out of bounds: %d".format(id)) 23 | with NoStacktrace 24 | 25 | case class UnexpectedThriftTypeException(thriftType: String) 26 | extends RuntimeException("Unexpected thrift type: %s".format(thriftType)) 27 | with NoStacktrace 28 | 29 | case class InvalidServiceException(service: String) 30 | extends RuntimeException("Invalid service: %s".format(service)) 31 | with NoStacktrace 32 | 33 | private[this] def filename(path: String): Option[String] = 34 | path match { 35 | case FileNameRegex(name) => Some(name) 36 | case _ => None 37 | } 38 | 39 | def fromImporter(importer: Importer, thriftFiles: Seq[String], serviceName: String): ThriftLifter = { 40 | val parser = new ThriftParser(importer, false, false, false) 41 | val resolver = TypeResolver() 42 | val resolvedServices: Seq[(ResolvedDocument, Service)] = 43 | thriftFiles flatMap { file: String => 44 | val doc = parser.parseFile(file) 45 | val resolvedDocument = resolver(doc) 46 | resolvedDocument.document.services.map { svc => 47 | (resolvedDocument, svc) 48 | } 49 | } 50 | resolvedServices.find { case (_, svc) => svc.sid.name == serviceName } match { 51 | case Some((rdoc, service)) => 52 | val inheritedMethods = rdoc.collectParentServices(service) flatMap { 53 | case (_, svc) => svc.functions 54 | } 55 | new ThriftLifter(service, inheritedMethods) 56 | case None => throw InvalidServiceException(serviceName) 57 | } 58 | } 59 | } 60 | 61 | class ThriftLifter( 62 | service: Service, 63 | inheritedMethods: Seq[Function], 64 | protocolFactory: TProtocolFactory = new TBinaryProtocol.Factory()) 65 | extends MapLifter 66 | { 67 | import ThriftLifter._ 68 | 69 | def apply(input: Array[Byte]): Future[Message] = { 70 | val buffer = new TMemoryInputTransport(input) 71 | Future.const(apply(buffer)) 72 | } 73 | 74 | def apply(transport: TTransport): Try[Message] = 75 | apply(protocolFactory.getProtocol(transport)) 76 | 77 | def apply(proto: TProtocol): Try[Message] = Try { readMessage(proto) } 78 | 79 | def readMessage(proto: TProtocol): Message = { 80 | val msg = proto.readMessageBegin() 81 | val result = msg.`type` match { 82 | case TMessageType.EXCEPTION => 83 | val exception = TApplicationException.read(proto) 84 | proto.readMessageEnd() 85 | throw exception 86 | 87 | case mType => 88 | readMethod(proto, msg.name, mType) 89 | } 90 | proto.readMessageEnd() 91 | result 92 | } 93 | 94 | def readMethod(proto: TProtocol, methodName: String, methodType: Byte): Message = { 95 | val method = findMethod(methodName) 96 | proto.readStructBegin() 97 | val result = methodType match { 98 | case TMessageType.CALL | TMessageType.ONEWAY => 99 | Message(Some(methodName), readMethodCall(proto, method)) 100 | case TMessageType.REPLY => 101 | Message(Some(methodName), readMethodResponse(proto, method)) 102 | case _ => throw InvalidMessageTypeException(methodType) 103 | } 104 | proto.readStructEnd() 105 | result 106 | } 107 | 108 | private[this] def findMethod(method: String): Function = 109 | findMethodSignature(method) 110 | 111 | private[lifter] val findMethodSignature: (String => Function) = 112 | Memoize { meth: String => 113 | service.functions.find(_.funcName.name == meth) match { 114 | case Some(function) => function 115 | case None => 116 | inheritedMethods.find(_.funcName.name == meth).getOrElse( 117 | throw MethodNotFoundException(meth)) 118 | } 119 | } 120 | 121 | def readFields(proto: TProtocol)(fieldReader: TField => Map[String, Any]): FieldMap[Any] = { 122 | val field = proto.readFieldBegin() 123 | FieldMap(field.`type` match { 124 | case TType.STOP => Map.empty[String, Any] 125 | case ft => 126 | val fieldMap = fieldReader(field) 127 | proto.readFieldEnd() 128 | fieldMap ++ readFields(proto)(fieldReader).toMap 129 | }) 130 | } 131 | 132 | def readMethodCall(proto: TProtocol, method: Function): FieldMap[Any] = 133 | readFields(proto) { field => 134 | val index = math.max(0, field.id - 1) 135 | val arg = method.args(index) 136 | Map(arg.originalName -> readType(proto, arg.fieldType)) 137 | } 138 | 139 | def readMethodResponse(proto: TProtocol, method: Function): FieldMap[Any] = 140 | readFields(proto) { field => 141 | field.id match { 142 | case 0 => Map("success" -> readType(proto, method.funcType)) 143 | case i => 144 | val throwField = method.throws(i - 1) 145 | Map(throwField.originalName -> readType(proto, throwField.fieldType)) 146 | } 147 | } 148 | 149 | def readType(proto: TProtocol, funcType: FunctionType): Any = 150 | funcType match { 151 | case Void | OnewayVoid => () 152 | case TBool => proto.readBool() 153 | case TByte => proto.readByte() 154 | case TI16 => proto.readI16() 155 | case TI32 => proto.readI32() 156 | case TI64 => proto.readI64() 157 | case TDouble => proto.readDouble() 158 | case TString => proto.readString() 159 | case TBinary => proto.readBinary() 160 | case struct: StructType => readStruct(proto, struct) 161 | case enum: EnumType => readEnum(proto, enum) 162 | case map: MapType => readMap(proto, map) 163 | case set: SetType => readSet(proto, set) 164 | case list: ListType => readList(proto, list) 165 | case _: NamedType => throw UnexpectedThriftTypeException("NamedType") 166 | case _: ReferenceType => throw UnexpectedThriftTypeException("ReferenceType") 167 | } 168 | 169 | def readStruct(proto: TProtocol, structType: StructType): FieldMap[Any] = { 170 | proto.readStructBegin() 171 | readFields(proto) { field => 172 | structType.struct.fields.find(_.index == field.id) match { 173 | case Some(f) => Map(f.originalName -> readType(proto, f.fieldType)) 174 | case None => throw FieldOutOfBoundsException(field.id) 175 | } 176 | } 177 | } 178 | 179 | def readList(proto: TProtocol, listType: ListType): Seq[Any] = { 180 | val listHeader = proto.readListBegin() 181 | val result = 182 | (0 until listHeader.size) map { _ => 183 | readType(proto, listType.eltType) 184 | } 185 | proto.readListEnd() 186 | result.toSeq 187 | } 188 | 189 | def readMap(proto: TProtocol, mapType: MapType): Map[Any, Any] = { 190 | val mapHeader = proto.readMapBegin() 191 | val result = 192 | ((0 until mapHeader.size) map { _ => 193 | readType(proto, mapType.keyType) -> readType(proto, mapType.valueType) 194 | }).toMap 195 | proto.readMapEnd() 196 | result 197 | } 198 | 199 | def readSet(proto: TProtocol, setType: SetType): Set[Any] = { 200 | val setHeader = proto.readSetBegin() 201 | val result = 202 | ((0 until setHeader.size) map { _ => 203 | readType(proto, setType.eltType) 204 | }).toSet 205 | proto.readSetEnd() 206 | result 207 | } 208 | 209 | def readEnum(proto: TProtocol, enumType: EnumType) = { 210 | val enumValue = proto.readI32() 211 | val enum = enumType.enum.values.find(_.value == enumValue) 212 | enum.map(_.sid.name).getOrElse("unknown (%d)".format(enumValue)) 213 | } 214 | 215 | private def typeToHuman(byte: Byte) = 216 | byte match { 217 | case TType.STOP => "STOP" 218 | case TType.VOID => "VOID" 219 | case TType.BOOL => "BOOL" 220 | case TType.BYTE => "BYTE" 221 | case TType.DOUBLE => "DOUBLE" 222 | case TType.I16 => "I16" 223 | case TType.I32 => "I32" 224 | case TType.I64 => "I64" 225 | case TType.STRING => "STRING" 226 | case TType.STRUCT => "STRUCT" 227 | case TType.MAP => "MAP" 228 | case TType.SET => "SET" 229 | case TType.LIST => "LIST" 230 | case TType.ENUM => "ENUM" 231 | case _ => "UNKNOWN (" + byte + ")" 232 | } 233 | 234 | } 235 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='proxy', 3 | dependencies=[ 4 | 'diffy/src/main/scala/com/twitter/diffy/analysis', 5 | 'diffy/src/main/scala/com/twitter/diffy/lifter', 6 | 'finatra/inject/inject-core', 7 | 'finagle/finagle-thriftmux', 8 | 'util/util-core', 9 | 'util/util-logging' 10 | ], 11 | sources=globs('*.scala'), 12 | ) 13 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/ClientService.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import com.twitter.finagle.{Addr, Name, Resolver, Service} 4 | import com.twitter.finagle.thrift.ThriftClientRequest 5 | import com.twitter.util.{Time, Var} 6 | import org.jboss.netty.handler.codec.http.{HttpRequest, HttpResponse} 7 | 8 | trait ClientService[Req, Rep] { 9 | val client: Service[Req, Rep] 10 | } 11 | 12 | case class ThriftService( 13 | override val client: Service[ThriftClientRequest, Array[Byte]], 14 | path: Name) 15 | extends ClientService[ThriftClientRequest, Array[Byte]] 16 | { 17 | var members: Int = 0 18 | var serversetValid = false 19 | var changedAt: Option[Time] = None 20 | var resetAt: Option[Time] = None 21 | var changeCount: Int = 0 22 | 23 | val boundServerset: Option[Var[Addr]] = 24 | path match { 25 | case Name.Bound(addr) => 26 | serversetValid = true 27 | Some(addr) 28 | case _ => 29 | serversetValid = false 30 | None 31 | } 32 | 33 | boundServerset foreach { 34 | _.changes.respond { 35 | case Addr.Bound(addrs, _) => 36 | serversetValid = true 37 | sizeChange(addrs.size) 38 | case Addr.Failed(_) | Addr.Neg => 39 | serversetValid = false 40 | sizeChange(0) 41 | case Addr.Pending => () 42 | } 43 | } 44 | 45 | private[this] def sizeChange(size: Int) { 46 | changeCount += 1 47 | if (changeCount > 1) { 48 | changedAt = Some(Time.now) 49 | if (members == 0) { 50 | resetAt = Some(Time.now) 51 | } 52 | } 53 | members = size 54 | } 55 | } 56 | 57 | case class HttpService( 58 | override val client: Service[HttpRequest, HttpResponse]) 59 | extends ClientService[HttpRequest, HttpResponse] 60 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/DifferenceProxy.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import javax.inject.Singleton 4 | 5 | import com.google.inject.Provides 6 | import com.twitter.diffy.analysis._ 7 | import com.twitter.diffy.lifter.Message 8 | import com.twitter.diffy.proxy.ResponseMode._ 9 | import com.twitter.finagle._ 10 | import com.twitter.inject.TwitterModule 11 | import com.twitter.logging.Logger 12 | import com.twitter.util._ 13 | 14 | object DifferenceProxyModule extends TwitterModule { 15 | @Provides 16 | @Singleton 17 | def providesDifferenceProxy( 18 | settings: Settings, 19 | collector: InMemoryDifferenceCollector, 20 | joinedDifferences: JoinedDifferences, 21 | analyzer: DifferenceAnalyzer 22 | ): DifferenceProxy = 23 | settings.protocol match { 24 | case "thrift" => ThriftDifferenceProxy(settings, collector, joinedDifferences, analyzer) 25 | case "http" => SimpleHttpDifferenceProxy(settings, collector, joinedDifferences, analyzer) 26 | case "https" => SimpleHttpsDifferenceProxy(settings, collector, joinedDifferences, analyzer) 27 | } 28 | } 29 | 30 | object DifferenceProxy { 31 | object NoResponseException extends Exception("No responses provided by diffy") 32 | val NoResponseExceptionFuture = Future.exception(NoResponseException) 33 | val log = Logger(classOf[DifferenceProxy]) 34 | } 35 | 36 | trait DifferenceProxy { 37 | import DifferenceProxy._ 38 | 39 | type Req 40 | type Rep 41 | type Srv <: ClientService[Req, Rep] 42 | 43 | val server: ListeningServer 44 | val settings: Settings 45 | var lastReset: Time = Time.now 46 | 47 | def serviceFactory(serverset: String, label: String): Srv 48 | 49 | def liftRequest(req: Req): Future[Message] 50 | def liftResponse(rep: Try[Rep]): Future[Message] 51 | 52 | // Clients for services 53 | val candidate = serviceFactory(settings.candidate.path, "candidate") 54 | val primary = serviceFactory(settings.primary.path, "primary") 55 | val secondary = serviceFactory(settings.secondary.path, "secondary") 56 | 57 | val collector: InMemoryDifferenceCollector 58 | 59 | val joinedDifferences: JoinedDifferences 60 | 61 | val analyzer: DifferenceAnalyzer 62 | 63 | private[this] lazy val multicastHandler = 64 | new SequentialMulticastService(Seq(primary.client, candidate.client, secondary.client)) 65 | 66 | def proxy = new Service[Req, Rep] { 67 | override def apply(req: Req): Future[Rep] = { 68 | val rawResponses = 69 | multicastHandler(req) respond { 70 | case Return(_) => log.debug("success networking") 71 | case Throw(t) => log.debug(t, "error networking") 72 | } 73 | 74 | val responses: Future[Seq[Message]] = 75 | rawResponses flatMap { reps => 76 | Future.collect(reps map liftResponse) respond { 77 | case Return(rs) => 78 | log.debug(s"success lifting ${rs.head.endpoint}") 79 | 80 | case Throw(t) => log.debug(t, "error lifting") 81 | } 82 | } 83 | 84 | responses foreach { 85 | case Seq(primaryResponse, candidateResponse, secondaryResponse) => 86 | liftRequest(req) respond { 87 | case Return(m) => 88 | log.debug(s"success lifting request for ${m.endpoint}") 89 | 90 | case Throw(t) => log.debug(t, "error lifting request") 91 | } foreach { req => 92 | analyzer(req, candidateResponse, primaryResponse, secondaryResponse) 93 | } 94 | } 95 | 96 | def pickRawResponse(pos: Int) = 97 | rawResponses flatMap { reps => Future.const(reps(pos)) } 98 | 99 | settings.responseMode match { 100 | case EmptyResponse => NoResponseExceptionFuture 101 | case FromPrimary => pickRawResponse(0) 102 | case FromCandidate => pickRawResponse(1) 103 | case FromSecondary => pickRawResponse(2) 104 | } 105 | } 106 | } 107 | 108 | def clear() = { 109 | lastReset = Time.now 110 | analyzer.clear() 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/HttpDifferenceProxy.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import java.net.SocketAddress 4 | 5 | import com.twitter.diffy.analysis.{DifferenceAnalyzer, JoinedDifferences, InMemoryDifferenceCollector} 6 | import com.twitter.diffy.lifter.{HttpLifter, Message} 7 | import com.twitter.diffy.proxy.DifferenceProxy.NoResponseException 8 | import com.twitter.finagle.{Service, Http, Filter} 9 | import com.twitter.finagle.http.{Status, Response, Method, Request} 10 | import com.twitter.util.{Try, Future} 11 | import org.jboss.netty.handler.codec.http.{HttpResponse, HttpRequest} 12 | 13 | object HttpDifferenceProxy { 14 | val okResponse = Future.value(Response(Status.Ok)) 15 | 16 | val noResponseExceptionFilter = 17 | new Filter[HttpRequest, HttpResponse, HttpRequest, HttpResponse] { 18 | override def apply( 19 | request: HttpRequest, 20 | service: Service[HttpRequest, HttpResponse] 21 | ): Future[HttpResponse] = { 22 | service(request).rescue[HttpResponse] { case NoResponseException => 23 | okResponse 24 | } 25 | } 26 | } 27 | } 28 | 29 | trait HttpDifferenceProxy extends DifferenceProxy { 30 | val servicePort: SocketAddress 31 | val lifter = new HttpLifter(settings.excludeHttpHeadersComparison) 32 | 33 | override type Req = HttpRequest 34 | override type Rep = HttpResponse 35 | override type Srv = HttpService 36 | 37 | override def serviceFactory(serverset: String, label: String) = 38 | HttpService(Http.newClient(serverset, label).toService) 39 | 40 | override lazy val server = 41 | Http.serve( 42 | servicePort, 43 | HttpDifferenceProxy.noResponseExceptionFilter andThen proxy 44 | ) 45 | 46 | override def liftRequest(req: HttpRequest): Future[Message] = 47 | lifter.liftRequest(req) 48 | 49 | override def liftResponse(resp: Try[HttpResponse]): Future[Message] = 50 | lifter.liftResponse(resp) 51 | } 52 | 53 | object SimpleHttpDifferenceProxy { 54 | /** 55 | * Side effects can be dangerous if replayed on production backends. This 56 | * filter ignores all POST, PUT, and DELETE requests if the 57 | * "allowHttpSideEffects" flag is set to false. 58 | */ 59 | lazy val httpSideEffectsFilter = 60 | Filter.mk[HttpRequest, HttpResponse, HttpRequest, HttpResponse] { (req, svc) => 61 | val hasSideEffects = 62 | Set(Method.Post, Method.Put, Method.Delete).contains(Request(req).method) 63 | 64 | if (hasSideEffects) DifferenceProxy.NoResponseExceptionFuture else svc(req) 65 | } 66 | } 67 | 68 | /** 69 | * A Twitter-specific difference proxy that adds custom filters to unpickle 70 | * TapCompare traffic from TFE and optionally drop requests that have side 71 | * effects 72 | * @param settings The settings needed by DifferenceProxy 73 | */ 74 | case class SimpleHttpDifferenceProxy ( 75 | settings: Settings, 76 | collector: InMemoryDifferenceCollector, 77 | joinedDifferences: JoinedDifferences, 78 | analyzer: DifferenceAnalyzer) 79 | extends HttpDifferenceProxy 80 | { 81 | import SimpleHttpDifferenceProxy._ 82 | 83 | override val servicePort = settings.servicePort 84 | override val proxy = 85 | Filter.identity andThenIf 86 | (!settings.allowHttpSideEffects, httpSideEffectsFilter) andThen 87 | super.proxy 88 | } 89 | 90 | /** 91 | * Alternative to SimpleHttpDifferenceProxy allowing HTTPS requests 92 | */ 93 | case class SimpleHttpsDifferenceProxy ( 94 | settings: Settings, 95 | collector: InMemoryDifferenceCollector, 96 | joinedDifferences: JoinedDifferences, 97 | analyzer: DifferenceAnalyzer) 98 | extends HttpDifferenceProxy 99 | { 100 | import SimpleHttpDifferenceProxy._ 101 | 102 | override val servicePort = settings.servicePort 103 | 104 | override val proxy = 105 | Filter.identity andThenIf 106 | (!settings.allowHttpSideEffects, httpSideEffectsFilter) andThen 107 | super.proxy 108 | 109 | override def serviceFactory(serverset: String, label: String) = 110 | HttpService( 111 | Http.client 112 | .withTls(serverset) 113 | .newService(serverset+":"+settings.httpsPort, label) 114 | ) 115 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/ParallelMulticastService.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import com.twitter.finagle.Service 4 | import com.twitter.util.{Future, Try} 5 | 6 | class ParallelMulticastService[-A, +B]( 7 | services: Seq[Service[A, B]]) 8 | extends Service[A, Seq[Try[B]]] 9 | { 10 | def apply(request: A): Future[Seq[Try[B]]] = 11 | Future.collect(services map { _(request).liftToTry }) 12 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/SequentialMulticastService.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import com.twitter.finagle.Service 4 | import com.twitter.util.{Future, Try} 5 | 6 | class SequentialMulticastService[-A, +B]( 7 | services: Seq[Service[A, B]]) 8 | extends Service[A, Seq[Try[B]]] 9 | { 10 | def apply(request: A): Future[Seq[Try[B]]] = 11 | services.foldLeft[Future[Seq[Try[B]]]](Future.Nil){ case (acc, service) => 12 | acc flatMap { responseTries => 13 | val nextResponse = service(request).liftToTry 14 | nextResponse map { responseTry => responseTries ++ Seq(responseTry) } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/Settings.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import java.net.InetSocketAddress 4 | 5 | import com.twitter.app.Flaggable 6 | import com.twitter.util.{Duration, Try} 7 | 8 | case class Settings( 9 | datacenter: String, 10 | servicePort:InetSocketAddress, 11 | candidate: Target, 12 | primary: Target, 13 | secondary: Target, 14 | protocol: String, 15 | clientId: String, 16 | pathToThriftJar: String, 17 | serviceClass: String, 18 | serviceName: String, 19 | apiRoot: String, 20 | enableThriftMux: Boolean, 21 | relativeThreshold: Double, 22 | absoluteThreshold: Double, 23 | teamEmail: String, 24 | emailDelay: Duration, 25 | rootUrl: String, 26 | allowHttpSideEffects: Boolean, 27 | responseMode: ResponseMode, 28 | excludeHttpHeadersComparison: Boolean, 29 | skipEmailsWhenNoErrors: Boolean, 30 | httpsPort: String, 31 | hostname: String = Try(java.net.InetAddress.getLocalHost.toString).getOrElse("unknown"), 32 | user: String = Try(sys.env("USER")).getOrElse("unknown")) 33 | 34 | case class Target(path: String) 35 | 36 | sealed trait ResponseMode { def name: String } 37 | object ResponseMode { 38 | case object EmptyResponse extends ResponseMode { val name = "empty" } 39 | case object FromPrimary extends ResponseMode { val name = "primary" } 40 | case object FromSecondary extends ResponseMode { val name = "secondary" } 41 | case object FromCandidate extends ResponseMode { val name = "candidate" } 42 | 43 | implicit val flaggable: Flaggable[ResponseMode] = new Flaggable[ResponseMode] { 44 | override def parse(s: String): ResponseMode = s match { 45 | case EmptyResponse.name => EmptyResponse 46 | case FromPrimary.name => FromPrimary 47 | case FromSecondary.name => FromSecondary 48 | case FromCandidate.name => FromCandidate 49 | } 50 | override def show(m: ResponseMode) = m.name 51 | } 52 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/proxy/ThriftDifferenceProxy.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import java.io.File 4 | import java.util.zip.ZipFile 5 | 6 | import com.twitter.diffy.analysis.{DifferenceAnalyzer, JoinedDifferences, InMemoryDifferenceCollector} 7 | import com.twitter.diffy.lifter.{MapLifterPool, Message, ThriftLifter} 8 | import com.twitter.diffy.scrooge._ 9 | import com.twitter.finagle.{Resolver, Thrift, ThriftMux} 10 | import com.twitter.finagle.thrift.{ClientId, ThriftClientRequest} 11 | import com.twitter.util.{Try, Future} 12 | import scala.collection.JavaConversions._ 13 | 14 | case class ThriftDifferenceProxy ( 15 | settings: Settings, 16 | collector: InMemoryDifferenceCollector, 17 | joinedDifferences: JoinedDifferences, 18 | analyzer: DifferenceAnalyzer) 19 | extends DifferenceProxy 20 | { 21 | override type Req = ThriftClientRequest 22 | override type Rep = Array[Byte] 23 | override type Srv = ThriftService 24 | 25 | private[this] lazy val clientId = new ClientId(settings.clientId) 26 | 27 | override val proxy = super.proxy 28 | 29 | private[this] val zipfile = new ZipFile(new File(settings.pathToThriftJar)) 30 | 31 | private[this] val importer = 32 | ZippedFileImporter(Seq(zipfile)) 33 | 34 | private[this] val filenames = 35 | zipfile.entries.toSeq collect { 36 | case zipEntry if !zipEntry.isDirectory && zipEntry.getName.endsWith(".thrift") => 37 | zipEntry.getName 38 | } 39 | 40 | val lifter = 41 | MapLifterPool( 42 | ThriftLifter.fromImporter( 43 | importer, 44 | filenames, 45 | settings.serviceClass 46 | ) 47 | ) 48 | 49 | override def serviceFactory(serverset: String, label: String) = { 50 | val client = if (settings.enableThriftMux) { 51 | ThriftMux.client.withClientId(clientId).newClient(serverset, label).toService 52 | } else { 53 | Thrift.client.withClientId(clientId).newClient(serverset, label).toService 54 | } 55 | 56 | ThriftService(client, Resolver.eval(serverset)) 57 | } 58 | 59 | override lazy val server = { 60 | if (settings.enableThriftMux) { 61 | ThriftMux.serve( 62 | settings.servicePort, 63 | proxy map { req: Array[Byte] => new ThriftClientRequest(req, false) } 64 | ) 65 | } else { 66 | Thrift.serve( 67 | settings.servicePort, 68 | proxy map { req: Array[Byte] => new ThriftClientRequest(req, false) } 69 | ) 70 | } 71 | } 72 | 73 | override def liftRequest(req: ThriftClientRequest): Future[Message] = lifter(req.message) 74 | override def liftResponse(rep: Try[Array[Byte]]): Future[Message] = 75 | Future.const(rep) flatMap { lifter(_) } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/scrooge/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='scrooge', 3 | dependencies=[ 4 | '3rdparty/jvm/org/apache/maven:maven-model', 5 | 'scrooge/scrooge-core', 6 | 'scrooge/scrooge-generator' 7 | ], 8 | sources=globs('*.scala'), 9 | ) 10 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/scrooge/ThriftImporter.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.scrooge 2 | 3 | import com.twitter.scrooge.frontend.{DirImporter, Importer} 4 | import java.io.File 5 | import java.nio.file.Files 6 | import java.util.zip.ZipFile 7 | import scala.collection.JavaConversions._ 8 | import scala.io.Source 9 | 10 | object ZippedFileImporter { 11 | def apply(zipFiles: Seq[ZipFile]): Importer = { 12 | val thriftDir = Files.createTempDirectory("thrift-") 13 | thriftDir.toFile.deleteOnExit() 14 | 15 | zipFiles foreach { zipFile => 16 | zipFile.entries.toList.collect { 17 | case zipEntry if !zipEntry.isDirectory && zipEntry.getName.endsWith(".thrift") => 18 | val data = Source.fromInputStream(zipFile.getInputStream(zipEntry), "UTF-8").mkString 19 | 20 | val newFile = new File(thriftDir.toString + File.separator + zipEntry.getName) 21 | new File(newFile.getParent).mkdirs() 22 | 23 | Files.write(newFile.toPath, data.getBytes) 24 | } 25 | } 26 | 27 | DirImporter(thriftDir.toFile) 28 | } 29 | } 30 | 31 | object FileImporter { 32 | def apply(files: Seq[File]): Importer = { 33 | val thriftDir = Files.createTempDirectory("thrift-") 34 | thriftDir.toFile.deleteOnExit() 35 | 36 | files foreach { file => 37 | val newFile = new File(thriftDir.toString + File.separator + file.getName) 38 | Files.copy(file.toPath, newFile.toPath) 39 | } 40 | 41 | DirImporter(thriftDir.toFile) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/util/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='util', 3 | dependencies=[ 4 | '3rdparty/jvm/javax/mail', 5 | '3rdparty/jvm/org/apache/thrift:libthrift-0.5.0', 6 | 'util/util-core', 7 | 'util/util-logging' 8 | ], 9 | sources=globs('*.scala'), 10 | ) 11 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/util/EmailSender.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.util 2 | 3 | import com.twitter.logging.Logger 4 | import com.twitter.util.{FuturePool, Future} 5 | 6 | import javax.mail._ 7 | import javax.mail.internet.{InternetAddress, MimeMessage} 8 | import java.util.Properties 9 | 10 | case class SimpleMessage( 11 | from: String, 12 | to: String, 13 | bcc: String, 14 | subject: String, 15 | body: String) 16 | 17 | class EmailSender(log: Logger, send: MimeMessage => Unit = Transport.send) { 18 | private[this] val props = new Properties 19 | props.put("mail.smtp.host", "localhost") 20 | props.put("mail.smtp.auth", "false") 21 | props.put("mail.smtp.port", "25") 22 | 23 | private[this] val session = Session.getDefaultInstance(props, null) 24 | 25 | def apply(msg: SimpleMessage): Future[Unit] = 26 | FuturePool.unboundedPool { 27 | val message = new MimeMessage(session) 28 | message.setFrom(new InternetAddress(msg.from)) 29 | message.setRecipients( 30 | Message.RecipientType.TO, 31 | InternetAddress.parse(msg.to) map { _.asInstanceOf[Address]} 32 | ) 33 | message.addRecipients( 34 | Message.RecipientType.BCC, 35 | InternetAddress.parse(msg.bcc) map { _.asInstanceOf[Address]} 36 | ) 37 | message.setSubject(msg.subject) 38 | message.setContent(msg.body, "text/html; charset=utf-8") 39 | try { 40 | send(message) 41 | } catch { case e => 42 | log.error(e, "failed to send message") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/workflow/BUILD: -------------------------------------------------------------------------------- 1 | scala_library( 2 | name='workflow', 3 | dependencies=[ 4 | 'diffy/src/main/scala/com/twitter/diffy/analysis', 5 | 'diffy/src/main/scala/com/twitter/diffy/proxy', 6 | 'diffy/src/main/scala/com/twitter/diffy/util', 7 | 'finatra/http', 8 | 'util/util-core' 9 | ], 10 | sources=globs('*.scala'), 11 | ) 12 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/workflow/FunctionalReport.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.workflow 2 | 3 | import javax.inject.Inject 4 | 5 | import com.twitter.diffy.proxy.Settings 6 | import com.twitter.finagle.util.DefaultTimer 7 | import com.twitter.util.{Timer, Time} 8 | 9 | class FunctionalReport @Inject()( 10 | settings: Settings, 11 | reportGenerator: ReportGenerator, 12 | override val timer: Timer = DefaultTimer.twitter) 13 | extends Workflow 14 | { 15 | val delay = settings.emailDelay 16 | 17 | override def run(start: Time) = { 18 | log.info(s"Sending FunctionalReport at ${Time.now}") 19 | reportGenerator.sendEmail 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/workflow/ReportGenerator.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.workflow 2 | 3 | import javax.inject.Inject 4 | 5 | import com.twitter.diffy.analysis.{JoinedEndpoint, DifferencesFilterFactory, JoinedDifferences} 6 | import com.twitter.diffy.proxy.Settings 7 | import com.twitter.diffy.util.{SimpleMessage, EmailSender} 8 | import com.twitter.finatra.http.internal.marshalling.mustache.MustacheService 9 | import com.twitter.logging.Logger 10 | import com.twitter.util.{Duration, Future} 11 | 12 | case class Endpoint(endpointName: String, count: Long, fields: Iterable[Field]) 13 | case class Field(fieldName: String, relativeDifference: String, absoluteDifference: String) 14 | 15 | case class ReportData( 16 | delay: Duration, 17 | rootUrl: String, 18 | serviceName: String, 19 | criticalDiffs: Int, 20 | criticalEndpoints: Seq[Endpoint], 21 | passingEndpoints: Seq[Endpoint]) 22 | 23 | object ReportGenerator { 24 | val NormalizedUrlRegex = "(http://)?([^/]*)(/?)".r 25 | def normalizeUrl(url: String): String = url match { 26 | case NormalizedUrlRegex(_, rawUrl, _) => s"http://$rawUrl/" 27 | } 28 | } 29 | 30 | class ReportGenerator @Inject()( 31 | joinedDifferences: JoinedDifferences, 32 | settings: Settings, 33 | mustacheService: MustacheService) 34 | { 35 | import ReportGenerator._ 36 | 37 | private[this] val log: Logger = Logger(classOf[ReportGenerator]) 38 | private[this] val emailSender = new EmailSender(log) 39 | 40 | private[this] def sendReport(report: ReportData) = 41 | emailSender(buildMessage(report)) 42 | 43 | def buildSubject(serviceName: String, criticalDiffs: Int) = 44 | if (criticalDiffs == 0) { 45 | s"[diffy] ${serviceName} - All endpoints passed" 46 | } else { 47 | s"[diffy] ${serviceName} ${criticalDiffs} critical" 48 | } 49 | 50 | def buildMessage(report: ReportData) : SimpleMessage = { 51 | SimpleMessage( 52 | from = "Diffy ", 53 | to = settings.teamEmail, 54 | bcc = settings.teamEmail, 55 | subject = buildSubject(report.serviceName, report.criticalDiffs), 56 | body = new String(mustacheService.createChannelBuffer("cron_report.mustache", report).toByteBuffer.array) 57 | ) 58 | } 59 | 60 | private[this] val filter = 61 | DifferencesFilterFactory( 62 | settings.relativeThreshold, 63 | settings.absoluteThreshold 64 | ) 65 | 66 | def extractReport(endpoints: Map[String, JoinedEndpoint]): ReportData = { 67 | val endpointGrouping = 68 | endpoints.map { case ((endpoint, joinedEndpoint)) => 69 | Endpoint( 70 | endpoint, 71 | joinedEndpoint.total, 72 | joinedEndpoint.fields.collect { 73 | case ((path, field)) if filter(field) => 74 | Field(path, "%1.2f".format(field.relativeDifference), "%1.2f".format(field.absoluteDifference)) 75 | } 76 | ) 77 | }.toSeq.groupBy(_.fields.size > 0) 78 | 79 | val criticalEndpoints = endpointGrouping.getOrElse(true, Seq.empty).sortBy(_.fields.size).reverse 80 | val passingEndpoints = endpointGrouping.getOrElse(false, Seq.empty).sortBy(_.endpointName) 81 | 82 | val fieldCount = criticalEndpoints.map(_.fields.size).sum 83 | 84 | ReportData( 85 | settings.emailDelay, 86 | normalizeUrl(settings.rootUrl), 87 | settings.serviceName, 88 | fieldCount, 89 | criticalEndpoints, 90 | passingEndpoints 91 | ) 92 | } 93 | 94 | def conditionallySendReport(reportData: ReportData) = 95 | if (reportData.criticalDiffs == 0 && settings.skipEmailsWhenNoErrors) { 96 | Future.Unit 97 | } else { 98 | sendReport(reportData) 99 | } 100 | 101 | def sendEmail = { 102 | joinedDifferences.endpoints map { extractReport _ andThen conditionallySendReport } 103 | } 104 | 105 | def extractReportFromDifferences = { 106 | joinedDifferences.endpoints map extractReport 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/scala/com/twitter/diffy/workflow/Workflow.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.workflow 2 | 3 | import javax.inject.{Named, Inject} 4 | 5 | import com.twitter.diffy.analysis.{RawDifferenceCounter, DifferenceCounter, EndpointMetadata} 6 | import com.twitter.diffy.util.EmailSender 7 | import com.twitter.finagle.util.DefaultTimer 8 | import com.twitter.finagle.stats.StatsReceiver 9 | import com.twitter.logging.Logger 10 | import com.twitter.util.{Duration, Time, Memoize, Timer} 11 | import com.twitter.util.TimeConversions._ 12 | 13 | trait Workflow { 14 | val log: Logger = Logger(classOf[Workflow]) 15 | val emailSender = new EmailSender(log) 16 | val delay: Duration 17 | 18 | val timer: Timer 19 | def run(start: Time): Unit 20 | 21 | def schedule(): Unit = { 22 | val start = Time.now 23 | log.info(s"Scheduling ${getClass.getName} at $start") 24 | 25 | timer.doLater(delay) { 26 | run(start) 27 | } 28 | } 29 | } 30 | 31 | class DifferenceStatsMonitor @Inject()( 32 | diffCounter: RawDifferenceCounter, 33 | stats: StatsReceiver, 34 | override val timer: Timer = DefaultTimer.twitter) 35 | extends Workflow 36 | { 37 | val delay = 1.minute 38 | val scope = stats.scope("raw").scope("endpoints") 39 | private[this] val addGauges = Memoize[(String, EndpointMetadata), Unit] { case (endpoint, meta) => 40 | scope.scope(endpoint).provideGauge("total"){ meta.total } 41 | scope.scope(endpoint).provideGauge("differences"){ meta.differences } 42 | } 43 | 44 | override def run(start: Time) = { 45 | diffCounter.counter.endpoints foreach { endPoints => 46 | endPoints map { addGauges } 47 | } 48 | } 49 | 50 | override def schedule() = { 51 | val start = Time.now 52 | timer.schedule(delay) { run(start) } 53 | } 54 | } -------------------------------------------------------------------------------- /src/main/thrift/BUILD: -------------------------------------------------------------------------------- 1 | create_thrift_libraries( 2 | base_name='thrift', 3 | sources=["diffy.thrift"], 4 | provides_java_name='diffy-thrift-java', 5 | provides_scala_name='diffy-thrift-scala', 6 | jvm_only=True 7 | ) 8 | -------------------------------------------------------------------------------- /src/main/thrift/diffy.thrift: -------------------------------------------------------------------------------- 1 | namespace java com.twitter.diffy.thriftjava 2 | #@namespace scala com.twitter.diffy.thriftscala 3 | 4 | struct Responses { 5 | # Responses from each of the different instances in JSON format 6 | 1: string candidate 7 | 2: string primary 8 | 3: string secondary 9 | } 10 | 11 | struct DifferenceResult { 12 | # Unique id of the request, either the trace id or a randomly generated long 13 | 1: i64 id, 14 | 15 | # Trace id of the request, if present 16 | 2: optional i64 trace_id, 17 | 18 | # Endpoint e.g. "get", "get_by_id" 19 | 3: string endpoint, 20 | 21 | # Unix timestamp in milliseconds 22 | 4: i64 timestamp_msec, 23 | 24 | # Map from field -> difference JSON string 25 | # e.g. "user/profile/name" -> "{type: 'PrimitiveDifference', left:'Foo', right:'Bar'}" 26 | 5: map differences, 27 | 28 | # JSON representation of the incoming request 29 | 6: string request, 30 | 31 | # Responses from primary, secondary and candidate 32 | 7: Responses responses 33 | } 34 | 35 | service SampleService { 36 | string get( 37 | 1: string request 38 | ) 39 | } -------------------------------------------------------------------------------- /src/main/webapp/BUILD: -------------------------------------------------------------------------------- 1 | resources( 2 | name='webapp', 3 | sources=rglobs('*') 4 | ) 5 | -------------------------------------------------------------------------------- /src/main/webapp/css/source/diff.sass: -------------------------------------------------------------------------------- 1 | $dark-primary: #2b3e50 2 | $medium-primary: lighten($dark-primary, 30) 3 | $light-primary: #D2D5D9 4 | $success: #5cb85c 5 | $header: #4e5d6c 6 | $difference-header-width: 13% 7 | $contrast: #df691a 8 | $difference-background-color: #ffbb99 9 | 10 | @mixin mono 11 | font-family: 'Inconsolata', monospace 12 | @mixin roboto 13 | font-family: 'Roboto Condensed', sans-serif 14 | @mixin lobster 15 | font-family: 'Lobster', sans-serif 16 | 17 | h1, h2, h3, h4, h5, h6 18 | @include roboto 19 | 20 | hr 21 | width: 100% 22 | border: 0 23 | border-bottom: solid 1px #eee 24 | margin: 5px 0 25 | 26 | html, body 27 | overflow: hidden 28 | font-size: 10pt 29 | 30 | a 31 | cursor: pointer 32 | 33 | .btn 34 | border-radius: 3px 35 | margin-bottom: 5px 36 | 37 | .black 38 | color: #000 39 | 40 | .clearfix:after 41 | visibility: hidden 42 | display: block 43 | font-size: 0 44 | content: " " 45 | clear: both 46 | height: 0 47 | 48 | .mono 49 | @include mono 50 | 51 | .rating.poor 52 | color: #b00 53 | .rating.perfect 54 | color: #090 55 | .rating.neutral 56 | color: #B57924 57 | .rating.almost 58 | color: #7F9100 59 | 60 | .navbar 61 | position: relative 62 | .title 63 | @include lobster 64 | padding-left: 6px 65 | font-size: 18pt 66 | padding-right: 8px 67 | .settings 68 | cursor: pointer 69 | position: absolute 70 | right: 10pt 71 | top: 9pt 72 | font-size: 18pt 73 | color: #fff 74 | opacity: .3 75 | .settings:hover 76 | opacity: .5 77 | 78 | .left-panel, .right-panel 79 | padding: 5px 80 | width: 49% 81 | .left-panel 82 | float: left 83 | margin-right: 1% 84 | .left-panel.divider 85 | border-right: solid 1px #ddd 86 | .right-panel 87 | margin-left: 1% 88 | float: right 89 | 90 | .header 91 | margin-bottom: 20px 92 | .title 93 | margin-bottom: 0 94 | .subtitle 95 | opacity: .5 96 | padding-left: 2px 97 | 98 | body 99 | position: relative 100 | 101 | .popup 102 | position: fixed 103 | width: 100% 104 | height: 100% 105 | z-index: 20 106 | color: #666 107 | overflow: scroll 108 | .background 109 | background: #000 110 | opacity: .6 111 | position: fixed 112 | width: 100% 113 | height: 100% 114 | .content 115 | width: 900px 116 | border: solid 1px $light-primary 117 | margin: 20px auto 100px auto 118 | padding: 10px 119 | position: relative 120 | background: #fff 121 | h1, h2, h3, h4, h5, h6 122 | color: #333 123 | .difference 124 | color: #000 125 | background-color: $difference-background-color 126 | padding: 2px 3px 127 | 128 | .tree-field 129 | padding: 2px 130 | .children 131 | margin: 4px 0 0 10px 132 | border-left: solid 1px #ddd 133 | padding-left: 5px 134 | 135 | 136 | .highlighted 137 | box-shadow: inset 0 0 2px #888888 138 | background: lighten($medium-primary, 40) 139 | .highlighted:hover 140 | border: $medium-primary 141 | 142 | .popup.settings 143 | .server 144 | margin-left: 10px 145 | .stats 146 | @include mono 147 | hr 148 | margin: 20px 0 149 | 150 | .popup.request 151 | .responses, .request 152 | @include mono 153 | .request 154 | padding: 5px 155 | overflow-x: scroll 156 | .time 157 | position: absolute 158 | text-align: right 159 | right: 12px 160 | top: 20px 161 | .responses 162 | .request 163 | overflow-x: scroll 164 | padding: 5px 165 | .left, .right 166 | width: 49% 167 | .left 168 | float: left 169 | margin-right: 1% 170 | .right 171 | margin-left: 1% 172 | float: right 173 | 174 | 175 | .endpoints-column 176 | background: $dark-primary 177 | color: #eee 178 | border-right: solid 1px $light-primary 179 | .dashboard-links a 180 | display: inline-block 181 | text-align: center 182 | color: $medium-primary 183 | background: darken($dark-primary, 10) 184 | padding: 5px 185 | .row 186 | display: block 187 | padding: 6px 8px 188 | margin-bottom: 0 189 | color: #bbb 190 | font-size: 10pt 191 | position: relative 192 | text-decoration: none 193 | border-top: solid 1px lighten($dark-primary, 10) 194 | .row.last 195 | border-bottom: solid 1px lighten($dark-primary, 10) 196 | .row.info 197 | color: lighten($dark-primary, 40) 198 | background: lighten($dark-primary, 6) 199 | padding: 10px 8px 200 | margin-bottom: 20px 201 | font-size: .9em 202 | .row.endpoint 203 | cursor: pointer 204 | .title 205 | .name 206 | // font-weight: bold 207 | color: #fff 208 | .failing 209 | @include roboto 210 | text-align: right 211 | position: absolute 212 | right: 7px 213 | top: 6px 214 | display: box 215 | .number 216 | font-size: 13pt 217 | .label 218 | padding: 0 219 | color: lighten($dark-primary, 20) 220 | font-size: 8pt 221 | .subtitle 222 | color: #888 223 | .row.endpoint:hover:not(.btn-primary) 224 | background: darken($dark-primary, 5) 225 | .row.endpoint.btn-primary 226 | color: #fff 227 | .failing 228 | .number 229 | color: lighten($contrast, 40) 230 | .label 231 | color: lighten($contrast, 40) 232 | .subtitle 233 | color: lighten($contrast, 30) 234 | 235 | .endpoints-column, .fields-column, .requests-column 236 | position: relative 237 | height: 100% 238 | overflow: scroll 239 | overflow-x: hidden 240 | 241 | .fields-column, .requests-column 242 | color: #333 243 | 244 | .fields-column 245 | padding-bottom: 40px 246 | background: $light-primary 247 | hr 248 | margin: 20px 0 249 | border-bottom: 1px solid darken($light-primary, 10) 250 | .subtle 251 | font-weight: bold 252 | color: #999 253 | .noshow 254 | padding: 20px 10px 255 | .header 256 | z-index: 1 257 | background: $light-primary 258 | position: fixed 259 | width: inherit 260 | margin: -15px 261 | padding: 10px 262 | height: 70px 263 | border-bottom: 1px solid darken($light-primary, 10) 264 | .title 265 | float: left 266 | .stats 267 | @include roboto 268 | background: #d2d5d9 269 | position: absolute 270 | right: 10px 271 | box-shadow: 0 0 5px 5px #d2d5d9 272 | margin-top: 8px 273 | padding-left: 0 274 | text-align: right 275 | li 276 | display: inline 277 | float: left 278 | margin-left: 20px 279 | li:first 280 | margin-left: 5px 281 | .percent 282 | font-size: 16pt 283 | margin: 5px 0 -5px 0 284 | .description 285 | font-size: 8pt 286 | color: #999 287 | .subtle 288 | color: #999 289 | .fields-container 290 | margin-top: 70px 291 | a 292 | color: #444 293 | text-decoration: none 294 | .field.terminal 295 | .title 296 | padding-left: 0 297 | .name 298 | color: #900 299 | .subtle 300 | color: #999 301 | .details 302 | font-size: 9pt 303 | margin-top: -3px 304 | color: #777 305 | .value 306 | font-weight: bold 307 | .field.excluded 308 | a, .name, .details, .rating 309 | color: #a1a1a1 310 | .details 311 | color: #999 312 | .toggle 313 | color: #999 314 | 315 | .field 316 | .toggle 317 | float: right 318 | cursor: pointer 319 | height: 20px 320 | width: 20px 321 | color: #333 322 | i 323 | margin: 3px 0 0 2px 324 | padding: 0px 0 0 2px 325 | .toggle:hover 326 | background: darken($light-primary, 3) 327 | .action.nohover:hover 328 | background: none 329 | .action 330 | float: left 331 | width: 18px 332 | height: 20px 333 | display: block 334 | .arrow 335 | padding: 4px 0 0 6px 336 | .checkbox 337 | margin: 4px 0 0 3px 338 | .title.selected, .title.selected:hover 339 | background: lighten($light-primary, 5) 340 | .title 341 | cursor: pointer 342 | padding: 1px 0 0 20px 343 | display: block 344 | .title:hover 345 | background: darken($light-primary, 3) 346 | .action:hover 347 | background: darken($light-primary, 8) 348 | .badge 349 | margin-left: 5px 350 | background: darken($light-primary, 5) 351 | position: absolute 352 | font-size: 9pt 353 | color: $dark-primary 354 | .name 355 | @include mono 356 | margin-bottom: 2px 357 | font-size: 10pt 358 | .children 359 | margin-left: 15px 360 | 361 | 362 | .requests-column 363 | padding-top: 10px 364 | background: #eee 365 | ins 366 | color: #090 367 | del 368 | color: #900 369 | strong 370 | color: $medium-primary 371 | font-weight: normal 372 | text-decoration: underline 373 | .pairpair 374 | .pair 375 | float: left 376 | width: 50% 377 | dt 378 | width: $difference-header-width * 2 379 | dd 380 | width: 100 - $difference-header-width * 2 381 | .pair 382 | padding: 4px 383 | .pair.expected, .pair.actual 384 | background: #f9f9f9 385 | .pair:hover 386 | dt 387 | color: darken($light-primary, 20) 388 | dd 389 | color: #333 390 | .show-more 391 | margin-top: 3px 392 | padding: 5px 393 | display: block 394 | text-align: center 395 | color: #999 396 | .show-more:hover 397 | background: #eee 398 | color: #000 399 | dt 400 | @include roboto 401 | width: $difference-header-width 402 | float: left 403 | font-size: 9pt 404 | text-transform: uppercase 405 | padding-top: 1pt 406 | font-weight: normal 407 | color: darken($light-primary, 10) 408 | dd 409 | @include mono 410 | float: left 411 | word-wrap: break-word 412 | width: 100 - $difference-header-width 413 | color: #666 414 | .field, .field:hover 415 | background: lighten($medium-primary, 40) 416 | dt 417 | color: darken($light-primary, 30) 418 | .open 419 | position: absolute 420 | right: 15px 421 | top: 15px 422 | .difference 423 | .path-item .separator 424 | color: #ccc 425 | .path-item:last-child 426 | color: $medium-primary 427 | .separator 428 | display: none 429 | .request 430 | position: relative 431 | margin-top: 10px 432 | padding: 10px 10px 433 | background: #fff 434 | border: solid 1px #888 435 | border-top-color: #ddd 436 | border-left-color: #ddd 437 | 438 | // iphone toggle 439 | .checkbox-iphone 440 | position: relative 441 | margin-top: 5px 442 | line-height: 30px 443 | input 444 | display: block 445 | position: absolute 446 | top: 0 447 | right: 0 448 | bottom: 0 449 | left: 0 450 | width: 0% 451 | height: 0% 452 | margin: 0 0 453 | cursor: pointer 454 | zoom: 1 455 | -webkit-opacity: 0 456 | -moz-opacity: 0 457 | opacity: 0 458 | filter: alpha(opacity=0) 459 | + span 460 | cursor: pointer 461 | -webkit-user-select: none 462 | -moz-user-select: none 463 | -ms-user-select: none 464 | user-select: none 465 | padding-right: 60px 466 | &:before 467 | position: absolute 468 | right: 0px 469 | display: inline-block 470 | content: "" 471 | height: 30px 472 | width: 60px 473 | border-radius: 30px 474 | line-height: 30px 475 | background: rgba(100, 100, 100, 0.2) 476 | box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.8) 477 | transition: background 0.2s ease-out 478 | &:after 479 | position: absolute 480 | width: 30px 481 | height: 30px 482 | right: 0 483 | top: 0 484 | margin-right: 30px 485 | border-radius: 30px 486 | line-height: 30px 487 | display: block 488 | background: #ffffff 489 | transition: margin-right 0.1s ease-in-out 490 | text-align: center 491 | font-weight: bold 492 | content: "" 493 | border: solid transparent 2px 494 | background-clip: padding-box 495 | vertical-align: middle 496 | input:checked 497 | + span 498 | &:before 499 | transition: background 0.2s ease-in 500 | background: #df691a 501 | &:after 502 | margin-right: 0 503 | content: "" 504 | border: solid transparent 2px 505 | background-clip: padding-box 506 | -webkit-animation: popIn ease-in 0.3s normal 507 | animation: popIn ease-in 0.3s normal 508 | input:not(:checked) 509 | + span 510 | &:after 511 | -webkit-animation: popOut ease-in 0.3s normal 512 | animation: popOut ease-in 0.3s normal 513 | input:disabled 514 | + span 515 | color: #777777 516 | &:after 517 | border: solid transparent 2px 518 | border-radius: 40px 519 | &:before 520 | box-shadow: 0 0 0 black 521 | -------------------------------------------------------------------------------- /src/main/webapp/scripts/angular-bindonce.min.js: -------------------------------------------------------------------------------- 1 | !function(){"use strict";var e=angular.module("pasvaz.bindonce",[]);e.directive("bindonce",function(){var e=function(e){if(e&&0!==e.length){var t=angular.lowercase(""+e);e=!("f"===t||"0"===t||"false"===t||"no"===t||"n"===t||"[]"===t)}else e=!1;return e},t=parseInt((/msie (\d+)/.exec(angular.lowercase(navigator.userAgent))||[])[1],10);isNaN(t)&&(t=parseInt((/trident\/.*; rv:(\d+)/.exec(angular.lowercase(navigator.userAgent))||[])[1],10));var r={restrict:"AM",controller:["$scope","$element","$attrs","$interpolate",function(r,a,i,n){var c=function(t,r,a){var i="show"===r?"":"none",n="hide"===r?"":"none";t.css("display",e(a)?i:n)},o=function(e,t){if(angular.isObject(t)&&!angular.isArray(t)){var r=[];angular.forEach(t,function(e,t){e&&r.push(t)}),t=r}t&&e.addClass(angular.isArray(t)?t.join(" "):t)},s=function(e,t){e.transclude(t,function(t){var r=e.element.parent(),a=e.element&&e.element[e.element.length-1],i=r&&r[0]||a&&a.parentNode,n=a&&a.nextSibling||null;angular.forEach(t,function(e){i.insertBefore(e,n)})})},l={watcherRemover:void 0,binders:[],group:i.boName,element:a,ran:!1,addBinder:function(e){this.binders.push(e),this.ran&&this.runBinders()},setupWatcher:function(e){var t=this;this.watcherRemover=r.$watch(e,function(e){void 0!==e&&(t.removeWatcher(),t.checkBindonce(e))},!0)},checkBindonce:function(e){var t=this,r=e.$promise?e.$promise.then:e.then;"function"==typeof r?r(function(){t.runBinders()}):t.runBinders()},removeWatcher:function(){void 0!==this.watcherRemover&&(this.watcherRemover(),this.watcherRemover=void 0)},runBinders:function(){for(;this.binders.length>0;){var r=this.binders.shift();if(!this.group||this.group==r.group){var a=r.scope.$eval(r.interpolate?n(r.value):r.value);switch(r.attr){case"boIf":e(a)&&s(r,r.scope.$new());break;case"boSwitch":var i,l=r.controller[0];(i=l.cases["!"+a]||l.cases["?"])&&(r.scope.$eval(r.attrs.change),angular.forEach(i,function(e){s(e,r.scope.$new())}));break;case"boSwitchWhen":var u=r.controller[0];u.cases["!"+r.attrs.boSwitchWhen]=u.cases["!"+r.attrs.boSwitchWhen]||[],u.cases["!"+r.attrs.boSwitchWhen].push({transclude:r.transclude,element:r.element});break;case"boSwitchDefault":var u=r.controller[0];u.cases["?"]=u.cases["?"]||[],u.cases["?"].push({transclude:r.transclude,element:r.element});break;case"hide":case"show":c(r.element,r.attr,a);break;case"class":o(r.element,a);break;case"text":r.element.text(a);break;case"html":r.element.html(a);break;case"style":r.element.css(a);break;case"src":r.element.attr(r.attr,a),t&&r.element.prop("src",a);break;case"attr":angular.forEach(r.attrs,function(e,t){var a,i;t.match(/^boAttr./)&&r.attrs[t]&&(a=t.replace(/^boAttr/,"").replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),i=r.scope.$eval(r.attrs[t]),r.element.attr(a,i))});break;case"href":case"alt":case"title":case"id":case"value":r.element.attr(r.attr,a)}}}this.ran=!0}};return l}],link:function(e,t,r,a){var i=r.bindonce&&e.$eval(r.bindonce);void 0!==i?a.checkBindonce(i):(a.setupWatcher(r.bindonce),t.bind("$destroy",a.removeWatcher))}};return r}),angular.forEach([{directiveName:"boShow",attribute:"show"},{directiveName:"boHide",attribute:"hide"},{directiveName:"boClass",attribute:"class"},{directiveName:"boText",attribute:"text"},{directiveName:"boBind",attribute:"text"},{directiveName:"boHtml",attribute:"html"},{directiveName:"boSrcI",attribute:"src",interpolate:!0},{directiveName:"boSrc",attribute:"src"},{directiveName:"boHrefI",attribute:"href",interpolate:!0},{directiveName:"boHref",attribute:"href"},{directiveName:"boAlt",attribute:"alt"},{directiveName:"boTitle",attribute:"title"},{directiveName:"boId",attribute:"id"},{directiveName:"boStyle",attribute:"style"},{directiveName:"boValue",attribute:"value"},{directiveName:"boAttr",attribute:"attr"},{directiveName:"boIf",transclude:"element",terminal:!0,priority:1e3},{directiveName:"boSwitch",require:"boSwitch",controller:function(){this.cases={}}},{directiveName:"boSwitchWhen",transclude:"element",priority:800,require:"^boSwitch"},{directiveName:"boSwitchDefault",transclude:"element",priority:800,require:"^boSwitch"}],function(t){var r=200;return e.directive(t.directiveName,function(){var e={priority:t.priority||r,transclude:t.transclude||!1,terminal:t.terminal||!1,require:["^bindonce"].concat(t.require||[]),controller:t.controller,compile:function(e,r,a){return function(e,r,i,n){var c=n[0],o=i.boParent;if(o&&c.group!==o){var s=c.element.parent();c=void 0;for(var l;9!==s[0].nodeType&&s.length;){if((l=s.data("$bindonceController"))&&l.group===o){c=l;break}s=s.parent()}if(!c)throw new Error("No bindonce controller: "+o)}c.addBinder({element:r,attr:t.attribute||t.directiveName,attrs:i,value:i[t.directiveName],interpolate:t.interpolate,group:o,transclude:a,controller:n.slice(1),scope:e})}}};return e})})}(); -------------------------------------------------------------------------------- /src/main/webapp/scripts/deep-diff.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * deep-diff. 3 | * Licensed under the MIT License. 4 | */ 5 | (function(e,t){"use strict";if(typeof define==="function"&&define.amd){define([],t)}else if(typeof exports==="object"){module.exports=t()}else{e.DeepDiff=t()}})(this,function(e){"use strict";var t,n,r=[];if(typeof global==="object"&&global){t=global}else if(typeof window!=="undefined"){t=window}else{t={}}n=t.DeepDiff;if(n){r.push(function(){if("undefined"!==typeof n&&t.DeepDiff===p){t.DeepDiff=n;n=e}})}function a(e,t){e.super_=t;e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:false,writable:true,configurable:true}})}function i(e,t){Object.defineProperty(this,"kind",{value:e,enumerable:true});if(t&&t.length){Object.defineProperty(this,"path",{value:t,enumerable:true})}}function f(e,t,n){f.super_.call(this,"E",e);Object.defineProperty(this,"lhs",{value:t,enumerable:true});Object.defineProperty(this,"rhs",{value:n,enumerable:true})}a(f,i);function u(e,t){u.super_.call(this,"N",e);Object.defineProperty(this,"rhs",{value:t,enumerable:true})}a(u,i);function l(e,t){l.super_.call(this,"D",e);Object.defineProperty(this,"lhs",{value:t,enumerable:true})}a(l,i);function s(e,t,n){s.super_.call(this,"A",e);Object.defineProperty(this,"index",{value:t,enumerable:true});Object.defineProperty(this,"item",{value:n,enumerable:true})}a(s,i);function h(e,t,n){var r=e.slice((n||t)+1||e.length);e.length=t<0?e.length+t:t;e.push.apply(e,r);return e}function c(e){var t=typeof e;if(t!=="object"){return t}if(e===Math){return"math"}else if(e===null){return"null"}else if(Array.isArray(e)){return"array"}else if(e instanceof Date){return"date"}else if(/^\/.*\//.test(e.toString())){return"regexp"}return"object"}function o(t,n,r,a,i,p,b){i=i||[];var d=i.slice(0);if(typeof p!=="undefined"){if(a&&a(d,p,{lhs:t,rhs:n})){return}d.push(p)}var v=typeof t;var y=typeof n;if(v==="undefined"){if(y!=="undefined"){r(new u(d,n))}}else if(y==="undefined"){r(new l(d,t))}else if(c(t)!==c(n)){r(new f(d,t,n))}else if(t instanceof Date&&n instanceof Date&&t-n!==0){r(new f(d,t,n))}else if(v==="object"&&t!==null&&n!==null){b=b||[];if(b.indexOf(t)<0){b.push(t);if(Array.isArray(t)){var k,m=t.length;for(k=0;k=n.length){r(new s(d,k,new l(e,t[k])))}else{o(t[k],n[k],r,a,d,k,b)}}while(k=0){o(t[i],n[i],r,a,d,i,b);w=h(w,u)}else{o(t[i],e,r,a,d,i,b)}});w.forEach(function(t){o(e,n[t],r,a,d,t,b)})}b.length=b.length-1}}else if(t!==n){if(!(v==="number"&&isNaN(t)&&isNaN(n))){r(new f(d,t,n))}}}function p(t,n,r,a){a=a||[];o(t,n,function(e){if(e){a.push(e)}},r);return a.length?a:e}function b(e,t,n){if(n.path&&n.path.length){var r=e[t],a,i=n.path.length-1;for(a=0;an;++n)t.call(e,this[n],n,this)}.call(t,e)},this.trigger=function(e){var n=Array.prototype.slice.call(arguments,1) 15 | return this.events[e]&&this._forEach(this.events[e],function(e){e.apply(t,n)}),this},"function"==typeof window.onhashchange&&this.on("hashchange",window.onhashchange),window.onhashchange=function(){t.trigger("hashchange")},this}e.regexRoute=function(t,e,n,r){return t instanceof RegExp?t:(t instanceof Array&&(t="("+t.join("|")+")"),t=t.concat(r?"":"/?").replace(/\/\(/g,"(?:/").replace(/\+/g,"__plus__").replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g,function(t,n,r,a,o,i){return e.push({name:a,optional:!!i}),n=n||"",""+(i?"":n)+"(?:"+(i?n:"")+(r||"")+(o||r&&"([^/.]+?)"||"([^/]+?)")+")"+(i||"")}).replace(/([\/.])/g,"\\$1").replace(/__plus__/g,"(.+)").replace(/\*/g,"(.*)"),RegExp("^"+t+"$",n?"":"i"))},e.prototype.get=e.prototype.add=function(t,n){var r=this,a=[],o=e.regexRoute(t,a),i=function(){var e=r.anchor.get().match(o) 16 | if(e){var i={params:{},keys:a,matches:e.slice(1)} 17 | r._forEach(i.matches,function(t,e){var n=a[e]&&a[e].name?a[e].name:e 18 | i.params[n]=t?decodeURIComponent(t):void 0}) 19 | var c={route:t,value:r.anchor.get(),params:i.params,regex:e,propagateEvent:!0,previousState:r.state,preventDefault:function(){this.propagateEvent=!1},callback:function(){n.call(r,i,c)}} 20 | if(r.trigger("match",c),!c.propagateEvent)return r 21 | r.state=c,c.callback()}return r} 22 | return i().on("hashchange",i)},e.prototype.on=e.prototype.bind=function(t,e){var n=this,r=t.split(" ") 23 | return this._forEach(r,function(t){n.events[t]?n.events[t].push(e):n.events[t]=[e]}),this},e.Router=e.prototype.router=e,e.prototype.context=function(t){var e=this 24 | return function(n,r){var a="/"!==t.slice(-1)?t+"/":t,o=a+n 25 | return e.get.call(e,o,r)}},e.listen=function(t){return function(){for(var e in t)this.get.call(this,e,t[e]) 26 | return this}.call(new e)},"function"==typeof t.define?t.define(function(){return e}):"object"==typeof exports?exports.Grapnel=e:t.Grapnel=e}).call({},window) -------------------------------------------------------------------------------- /src/main/webapp/scripts/source/diff.coffee: -------------------------------------------------------------------------------- 1 | module = angular.module 'diffy', [], ($interpolateProvider, $locationProvider) -> 2 | $interpolateProvider.startSymbol '[[' 3 | $interpolateProvider.endSymbol ']]' 4 | $locationProvider.html5Mode false 5 | 6 | module.service 'router', ($timeout, $rootScope) -> 7 | router = new Grapnel() 8 | routes = [] 9 | (route, callback) -> 10 | router.add route, (req) -> 11 | $timeout -> 12 | callback req 13 | $rootScope.$apply() 14 | 15 | module.factory 'endpointInclusions', -> {} 16 | module.factory 'info', -> {} 17 | module.factory 'percentageRating', -> 18 | (percent) -> 19 | if percent < .1 then "perfect" 20 | else if percent < 5 then "almost" 21 | else if percent < 50 then "neutral" 22 | else "poor" 23 | 24 | module.filter 'ago', -> 25 | (input) -> 26 | if input <= 0 then "forever" else moment(input).fromNow() 27 | 28 | module.filter 'formatDate', -> 29 | (input) -> 30 | return "-" if input <= 0 31 | moment(input).format("lll") 32 | 33 | module.directive 'ngOnEsc', -> 34 | (scope, element, attrs) -> 35 | $(document).bind 'keydown keypress', (event) -> 36 | if event.keyCode == 27 37 | scope.$eval(attrs.ngOnEsc) 38 | scope.$apply() 39 | event.preventDefault() 40 | 41 | module.directive 'deepLink', ($rootScope) -> 42 | (scope, element, attrs) -> 43 | element.bind 'click', (event) -> 44 | link = scope.$eval attrs.deepLink 45 | link = link.join("/") if $.isArray link 46 | window.location = '#/' + link 47 | 48 | module.directive 'requestTreeHighlight', ($rootScope, $timeout) -> 49 | (scope, element, attrs) -> 50 | scope.$root.$on "highlight_differences", (event, pathSelectors) -> 51 | $timeout -> 52 | $rootScope.$apply() 53 | for i, path of pathSelectors 54 | jQuery(path + " strong").addClass('difference') 55 | , 0 56 | 57 | module.service 'api', ($rootScope) -> 58 | (url, params, success) -> 59 | params = $.extend params, {} 60 | $.get apiRoot + "/api/1/" + url, params, -> 61 | success.apply(this, arguments) 62 | $rootScope.$apply() 63 | .fail -> 64 | console.log arguments 65 | 66 | class Metadata 67 | constructor: -> 68 | @metadata = {} 69 | @defaults = {} 70 | @prefix = -> "default" 71 | get: (key, prefix = @prefix()) -> 72 | @metadata[prefix] = {} if @metadata[prefix] == undefined 73 | @metadata[prefix][key] = ($.extend true, {}, @defaults) if @metadata[prefix][key] == undefined 74 | # create a copy of defaults 75 | @metadata[prefix][key] 76 | each: (func) -> 77 | for key, value of @metadata[@prefix()] 78 | func(key, value) 79 | 80 | module.factory 'globalFields', -> {} 81 | module.factory 'globalMetadata', -> new Metadata() 82 | 83 | module.controller 'EndpointController', ($scope, $interval, api, router, info, globalMetadata, globalFields, endpointInclusions, percentageRating) -> 84 | $scope.globalExclusion = false 85 | $scope.endpoints = {} 86 | $scope.sortedEndpoints = [] 87 | $scope.metadata = new Metadata() 88 | $scope.percentageRating = percentageRating 89 | 90 | $scope.inclusionPercentage = (ep) -> 91 | if ep of endpointInclusions then endpointInclusions[ep] 92 | else 1 93 | 94 | $scope.info = info 95 | metadata = $scope.metadata 96 | metadata.defaults.selected = false 97 | 98 | router '/ep/:endpoint/:path?/:id?', (req) -> 99 | metadata.each (key, item) -> 100 | item.selected = false if item.selected 101 | metadata.get(req.params.endpoint).selected = true 102 | 103 | $scope.size = Object.size 104 | 105 | $scope.$root.$on 'exclusions_updated', (event) -> 106 | sortEndpoints() 107 | 108 | $scope.showSettings = -> 109 | $scope.$root.$emit 'show_settings' 110 | 111 | $scope.loadEndpoints = -> 112 | api "endpoints", {}, (endpoints) -> 113 | if !_.isEqual(endpoints, $scope.endpoints) 114 | endpointObjs = _.map endpoints, (stats, endpoint) -> 115 | { 116 | name: endpoint 117 | failureRate: stats.differences / stats.total 118 | inclusion: $scope.inclusionPercentage(endpoint) 119 | stats: stats 120 | } 121 | $scope.endpoints = endpointObjs 122 | sortEndpoints() 123 | 124 | sortEndpoints = -> 125 | $scope.endpoints.sort (a, b) -> 126 | aInclusion = $scope.inclusionPercentage(a.name) 127 | bInclusion = $scope.inclusionPercentage(b.name) 128 | if (a.failureRate * aInclusion > b.failureRate * bInclusion) then -1 129 | else if (a.failureRate * aInclusion < b.failureRate * bInclusion) then 1 130 | else if (a.name < b.name) then -1 131 | else 1 132 | 133 | $scope.excludeAll = -> 134 | endpoints = [] 135 | for i, endpoint of $scope.endpoints 136 | endpoints.push(endpoint.name) 137 | 138 | $scope.$root.$emit 'toggle_global_exclusion', endpoints, $scope.globalExclusion 139 | 140 | $scope.apiInterval = $interval $scope.loadEndpoints, 2000 141 | $scope.loadEndpoints() 142 | 143 | 144 | module.controller 'FieldsController', ($scope, api, router, globalMetadata, globalFields, endpointInclusions, percentageRating) -> 145 | $scope.loading = false 146 | $scope.percentageRating = percentageRating 147 | 148 | metadata = globalMetadata 149 | metadata.defaults.included = true 150 | 151 | metadata.prefix = -> $scope.endpointName 152 | 153 | router '/ep/:endpoint/:path?/:id?', (req) -> 154 | if $scope.endpointName != req.params.endpoint 155 | clearInterval $scope.loadFieldsInterval 156 | $scope.endpointName = req.params.endpoint 157 | $scope.loadFields false 158 | $scope.loadFieldsInterval = setInterval -> 159 | $scope.loadFields true 160 | , 5000 161 | 162 | $scope.expandFields = (fields, endpointName) -> 163 | obj = {} 164 | for path, item of fields 165 | $scope.addField obj, path, item, endpointName 166 | obj 167 | 168 | $scope.iterateFields = (field, func, endpointName) -> 169 | iterate = (children) -> 170 | for name, child of children 171 | func child, metadata.get(child.path, endpointName) 172 | iterate child.children if child.children 173 | iterate field 174 | 175 | $scope.addField = (obj, path, item, endpointName) -> 176 | path = path.split "." if typeof path == "string" 177 | currentObj = obj 178 | currentPath = "" 179 | for fieldName, i in path 180 | currentPath += fieldName 181 | meta = metadata.get(currentPath, endpointName) 182 | if i < path.length - 1 183 | currentObj[fieldName] = currentObj[fieldName] || {path: currentPath, children: {}} 184 | meta.differences = 0 if !meta.differences 185 | currentObj[fieldName].path = currentPath 186 | currentObj = currentObj[fieldName].children 187 | else 188 | currentObj[fieldName] = $.extend currentObj[fieldName], { path: currentPath, terminal: true }, item 189 | meta.differences = item.differences 190 | meta.weight = item.weight 191 | meta.includedWeights = item.weight 192 | meta.noise = item.noise 193 | currentPath += "." 194 | 195 | $scope.expandAll = -> 196 | $scope.iterateFields $scope.fields, (field, meta) -> 197 | meta.collapsed = false 198 | 199 | $scope.clearExclusions = (endpointName) -> 200 | $scope.iterateFields globalFields[endpointName], (field, meta) -> 201 | meta.included = true 202 | , endpointName 203 | $scope.traverseFields(globalFields[endpointName], endpointName) 204 | 205 | $scope.inspectFields = (children, path, endpointName) -> 206 | meta = metadata.get(path, endpointName) 207 | meta.weight = 0 208 | meta.includedWeights = 0 209 | meta.childrenIncluded = false 210 | childrenHaveChildren = false 211 | for name, child of children 212 | childMeta = metadata.get(child.path, endpointName) 213 | if child.children 214 | childrenHaveChildren = true 215 | $scope.inspectFields(child.children, child.path, endpointName) if !childMeta.terminal 216 | meta.childrenIncluded = true if childMeta.childrenIncluded && childMeta.included 217 | else 218 | meta.childrenIncluded = true if childMeta.included 219 | meta.weight += childMeta.weight 220 | meta.includedWeights += childMeta.includedWeights if childMeta.included 221 | meta.collapsed = !childrenHaveChildren if meta.collapsed == undefined 222 | 223 | $scope.traverseFields = (field, endpointName) -> 224 | $scope.inspectFields(field, undefined, endpointName) 225 | weight = 0 226 | includedWeights = 0 227 | for name, child of field 228 | meta = metadata.get(name, endpointName) 229 | weight += meta.weight 230 | includedWeights += meta.includedWeights if meta.included 231 | 232 | inclusionsPercentage = 0 233 | if includedWeights && weight 234 | inclusionsPercentage = (includedWeights) / (weight) 235 | if endpointName == $scope.endpointName 236 | $scope.diffs = Math.ceil(inclusionsPercentage * $scope.endpoint.differences) 237 | $scope.percentage = ((inclusionsPercentage * $scope.endpoint.differences) / $scope.endpoint.total) * 100 238 | if endpointInclusions[endpointName] != inclusionsPercentage 239 | endpointInclusions[endpointName] = inclusionsPercentage 240 | $scope.$root.$emit 'exclusions_updated' 241 | 242 | $scope.autoExclude = (endpointName) -> 243 | $scope.iterateFields globalFields[endpointName], (field, meta) -> 244 | if field.terminal 245 | meta.included = field.differences > field.noise && field.relative_difference > relativeThreshold && field.absolute_difference > absoluteThreshold 246 | , endpointName 247 | $scope.traverseFields(globalFields[endpointName], endpointName) 248 | 249 | $scope.collapseExcluded = -> 250 | $scope.iterateFields $scope.fields, (field, meta) -> 251 | if !meta.included || !meta.childrenIncluded 252 | meta.collapsed = true 253 | $scope.traverseFields($scope.fields, $scope.endpointName) 254 | 255 | $scope.size = Object.size 256 | $scope.pathSelected = (path) -> 257 | metadata.each (key, item) -> 258 | item.selected = false if item.selected 259 | metadata.get(path).selected = true 260 | $scope.$root.$emit 'load_path', $scope.endpointName, path 261 | $scope.hasFields = -> 262 | Object.size($scope.rawFields) > 0 263 | 264 | $scope.fields = null 265 | $scope.rawFields = null 266 | 267 | $scope.loadFields = (hideLoader) -> 268 | $scope.loading = !hideLoader 269 | if !hideLoader 270 | $scope.fields = null 271 | $scope.rawFields = null 272 | api 'endpoints/' + $scope.endpointName + '/stats', { "include_weights" : true, "exclude_noise" : excludeNoise }, (response) -> 273 | $scope.endpoint = response.endpoint 274 | $scope.loading = false 275 | if !_.isEqual response.fields, $scope.rawFields 276 | $scope.fields = $scope.expandFields response.fields, $scope.endpointName 277 | globalFields[$scope.endpointName] = $scope.fields 278 | $scope.traverseFields($scope.fields, $scope.endpointName) 279 | $scope.rawFields = response.fields 280 | 281 | $scope.getGlobalMetadata = (path) -> 282 | globalMetadata.get(path) 283 | 284 | $scope.$root.$on 'toggle_global_exclusion', (event, endpoints, globalExclusion) -> 285 | exclude = (endpointName) -> 286 | if globalExclusion 287 | $scope.autoExclude endpointName 288 | else 289 | $scope.clearExclusions endpointName 290 | 291 | loadAndExclude = (endpointName) -> 292 | api 'endpoints/' + endpointName + '/stats', { "include_weights" : true, "exclude_noise" : excludeNoise }, (response) -> 293 | globalFields[endpointName] = $scope.expandFields response.fields, endpointName 294 | $scope.traverseFields(globalFields[endpointName], endpointName) 295 | exclude(endpointName) 296 | 297 | for i, endpoint of endpoints 298 | if globalFields[endpoint] == undefined 299 | loadAndExclude(endpoint) 300 | else 301 | exclude(endpoint) 302 | 303 | 304 | module.controller 'RequestsController', ($scope, api, router) -> 305 | $scope.loading = false 306 | 307 | router '/ep/:endpoint/:path/:id?', (req) -> 308 | if $scope.endpointName != req.params.endpoint || $scope.path != req.params.path 309 | $scope.path = req.params.path 310 | $scope.endpointName = req.params.endpoint 311 | $scope.loadPath req.params.endpoint, req.params.path, false 312 | 313 | $scope.loadPath = (endpoint, path, hideLoader) -> 314 | $scope.loading = !hideLoader 315 | $scope.requests = false 316 | $scope.path = path 317 | $scope.endpoint = endpoint 318 | api 'endpoints/' + endpoint + '/fields/' + path + '/results', {}, (response) -> 319 | $scope.loading = false 320 | for request in response.requests 321 | for path, difference of request.differences 322 | difference.collapsed = (path.indexOf $scope.path) != 0 323 | $scope.requests = response.requests 324 | 325 | $scope.requestSelected = (id) -> 326 | $scope.$root.$emit 'load_request', id 327 | 328 | $scope.size = Object.size 329 | $scope.countCollapsed = (diffs) -> 330 | i = 0 331 | for path, diff of diffs 332 | i++ if diff.collapsed 333 | i 334 | $scope.uncollapse = (diffs) -> 335 | for path, diff of diffs 336 | diff.collapsed = false 337 | 338 | module.controller 'RequestController', ($scope, api, router) -> 339 | $scope.size = Object.size 340 | $scope.loading = false 341 | $scope.isObject = $.isPlainObject 342 | $scope.isArray = $.isArray 343 | $scope.requestId = false 344 | 345 | $scope.back = -> 346 | window.location = '#/ep/' + [$scope.endpointName, $scope.path].join('/') 347 | 348 | router '/ep/:endpoint/:path', (req) -> 349 | $scope.request = null 350 | 351 | router '/ep/:endpoint/:path/:id', (req) -> 352 | $scope.loading = true 353 | $scope.requestId = req.params.id 354 | $scope.endpointName = req.params.endpoint 355 | $scope.path = req.params.path 356 | api 'requests/' + $scope.requestId, {}, (response) -> 357 | $scope.loading = false 358 | $scope.request = response 359 | 360 | diffs = DeepDiff($scope.request.left, $scope.request.right) 361 | pathSelectors = [] 362 | 363 | for i, diff of diffs 364 | pathSelectorArray = [] 365 | for j, p of diff.path 366 | pathSelectorArray.push("[data-path='" + p + "']") 367 | pathSelector = pathSelectorArray.join(" ") 368 | pathSelectors.push(pathSelector) 369 | 370 | $scope.$root.$emit 'highlight_differences', pathSelectors 371 | 372 | module.controller 'SettingsController', ($scope, $timeout, $interval, api, router, info) -> 373 | loadInfo = -> 374 | api 'info', {}, (response) -> 375 | $.extend info, response 376 | 377 | apiInterval = $interval loadInfo, 10000 378 | loadInfo() 379 | 380 | $scope.info = info 381 | 382 | $scope.visible = false 383 | $scope.$root.$on 'show_settings', (event) -> 384 | $scope.visible = true 385 | $scope.loading = true 386 | 387 | $scope.clear = -> 388 | $scope.clearLoading = true 389 | if confirm("This will reset all counters and clear all the saved requests/responses, are you sure?") 390 | api 'clear', {}, (response) -> 391 | $scope.clearLoading = true 392 | $timeout -> 393 | $scope.clearLoading = false 394 | , 2000 395 | 396 | Object.size = (obj) -> 397 | size = 0 398 | for key of obj 399 | size++ if obj.hasOwnProperty(key) 400 | size 401 | -------------------------------------------------------------------------------- /src/main/webapp/scripts/underscore.min.js: -------------------------------------------------------------------------------- 1 | // Underscore.js 1.6.0 2 | // http://underscorejs.org 3 | // (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors 4 | // Underscore may be freely distributed under the MIT license. 5 | (function(){var n=this,t=n._,r={},e=Array.prototype,u=Object.prototype,i=Function.prototype,a=e.push,o=e.slice,c=e.concat,l=u.toString,f=u.hasOwnProperty,s=e.forEach,p=e.map,h=e.reduce,v=e.reduceRight,g=e.filter,d=e.every,m=e.some,y=e.indexOf,b=e.lastIndexOf,x=Array.isArray,w=Object.keys,_=i.bind,j=function(n){return n instanceof j?n:this instanceof j?void(this._wrapped=n):new j(n)};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=j),exports._=j):n._=j,j.VERSION="1.6.0";var A=j.each=j.forEach=function(n,t,e){if(null==n)return n;if(s&&n.forEach===s)n.forEach(t,e);else if(n.length===+n.length){for(var u=0,i=n.length;i>u;u++)if(t.call(e,n[u],u,n)===r)return}else for(var a=j.keys(n),u=0,i=a.length;i>u;u++)if(t.call(e,n[a[u]],a[u],n)===r)return;return n};j.map=j.collect=function(n,t,r){var e=[];return null==n?e:p&&n.map===p?n.map(t,r):(A(n,function(n,u,i){e.push(t.call(r,n,u,i))}),e)};var O="Reduce of empty array with no initial value";j.reduce=j.foldl=j.inject=function(n,t,r,e){var u=arguments.length>2;if(null==n&&(n=[]),h&&n.reduce===h)return e&&(t=j.bind(t,e)),u?n.reduce(t,r):n.reduce(t);if(A(n,function(n,i,a){u?r=t.call(e,r,n,i,a):(r=n,u=!0)}),!u)throw new TypeError(O);return r},j.reduceRight=j.foldr=function(n,t,r,e){var u=arguments.length>2;if(null==n&&(n=[]),v&&n.reduceRight===v)return e&&(t=j.bind(t,e)),u?n.reduceRight(t,r):n.reduceRight(t);var i=n.length;if(i!==+i){var a=j.keys(n);i=a.length}if(A(n,function(o,c,l){c=a?a[--i]:--i,u?r=t.call(e,r,n[c],c,l):(r=n[c],u=!0)}),!u)throw new TypeError(O);return r},j.find=j.detect=function(n,t,r){var e;return k(n,function(n,u,i){return t.call(r,n,u,i)?(e=n,!0):void 0}),e},j.filter=j.select=function(n,t,r){var e=[];return null==n?e:g&&n.filter===g?n.filter(t,r):(A(n,function(n,u,i){t.call(r,n,u,i)&&e.push(n)}),e)},j.reject=function(n,t,r){return j.filter(n,function(n,e,u){return!t.call(r,n,e,u)},r)},j.every=j.all=function(n,t,e){t||(t=j.identity);var u=!0;return null==n?u:d&&n.every===d?n.every(t,e):(A(n,function(n,i,a){return(u=u&&t.call(e,n,i,a))?void 0:r}),!!u)};var k=j.some=j.any=function(n,t,e){t||(t=j.identity);var u=!1;return null==n?u:m&&n.some===m?n.some(t,e):(A(n,function(n,i,a){return u||(u=t.call(e,n,i,a))?r:void 0}),!!u)};j.contains=j.include=function(n,t){return null==n?!1:y&&n.indexOf===y?n.indexOf(t)!=-1:k(n,function(n){return n===t})},j.invoke=function(n,t){var r=o.call(arguments,2),e=j.isFunction(t);return j.map(n,function(n){return(e?t:n[t]).apply(n,r)})},j.pluck=function(n,t){return j.map(n,j.property(t))},j.where=function(n,t){return j.filter(n,j.matches(t))},j.findWhere=function(n,t){return j.find(n,j.matches(t))},j.max=function(n,t,r){if(!t&&j.isArray(n)&&n[0]===+n[0]&&n.length<65535)return Math.max.apply(Math,n);var e=-1/0,u=-1/0;return A(n,function(n,i,a){var o=t?t.call(r,n,i,a):n;o>u&&(e=n,u=o)}),e},j.min=function(n,t,r){if(!t&&j.isArray(n)&&n[0]===+n[0]&&n.length<65535)return Math.min.apply(Math,n);var e=1/0,u=1/0;return A(n,function(n,i,a){var o=t?t.call(r,n,i,a):n;u>o&&(e=n,u=o)}),e},j.shuffle=function(n){var t,r=0,e=[];return A(n,function(n){t=j.random(r++),e[r-1]=e[t],e[t]=n}),e},j.sample=function(n,t,r){return null==t||r?(n.length!==+n.length&&(n=j.values(n)),n[j.random(n.length-1)]):j.shuffle(n).slice(0,Math.max(0,t))};var E=function(n){return null==n?j.identity:j.isFunction(n)?n:j.property(n)};j.sortBy=function(n,t,r){return t=E(t),j.pluck(j.map(n,function(n,e,u){return{value:n,index:e,criteria:t.call(r,n,e,u)}}).sort(function(n,t){var r=n.criteria,e=t.criteria;if(r!==e){if(r>e||r===void 0)return 1;if(e>r||e===void 0)return-1}return n.index-t.index}),"value")};var F=function(n){return function(t,r,e){var u={};return r=E(r),A(t,function(i,a){var o=r.call(e,i,a,t);n(u,o,i)}),u}};j.groupBy=F(function(n,t,r){j.has(n,t)?n[t].push(r):n[t]=[r]}),j.indexBy=F(function(n,t,r){n[t]=r}),j.countBy=F(function(n,t){j.has(n,t)?n[t]++:n[t]=1}),j.sortedIndex=function(n,t,r,e){r=E(r);for(var u=r.call(e,t),i=0,a=n.length;a>i;){var o=i+a>>>1;r.call(e,n[o])t?[]:o.call(n,0,t)},j.initial=function(n,t,r){return o.call(n,0,n.length-(null==t||r?1:t))},j.last=function(n,t,r){return null==n?void 0:null==t||r?n[n.length-1]:o.call(n,Math.max(n.length-t,0))},j.rest=j.tail=j.drop=function(n,t,r){return o.call(n,null==t||r?1:t)},j.compact=function(n){return j.filter(n,j.identity)};var M=function(n,t,r){return t&&j.every(n,j.isArray)?c.apply(r,n):(A(n,function(n){j.isArray(n)||j.isArguments(n)?t?a.apply(r,n):M(n,t,r):r.push(n)}),r)};j.flatten=function(n,t){return M(n,t,[])},j.without=function(n){return j.difference(n,o.call(arguments,1))},j.partition=function(n,t){var r=[],e=[];return A(n,function(n){(t(n)?r:e).push(n)}),[r,e]},j.uniq=j.unique=function(n,t,r,e){j.isFunction(t)&&(e=r,r=t,t=!1);var u=r?j.map(n,r,e):n,i=[],a=[];return A(u,function(r,e){(t?e&&a[a.length-1]===r:j.contains(a,r))||(a.push(r),i.push(n[e]))}),i},j.union=function(){return j.uniq(j.flatten(arguments,!0))},j.intersection=function(n){var t=o.call(arguments,1);return j.filter(j.uniq(n),function(n){return j.every(t,function(t){return j.contains(t,n)})})},j.difference=function(n){var t=c.apply(e,o.call(arguments,1));return j.filter(n,function(n){return!j.contains(t,n)})},j.zip=function(){for(var n=j.max(j.pluck(arguments,"length").concat(0)),t=new Array(n),r=0;n>r;r++)t[r]=j.pluck(arguments,""+r);return t},j.object=function(n,t){if(null==n)return{};for(var r={},e=0,u=n.length;u>e;e++)t?r[n[e]]=t[e]:r[n[e][0]]=n[e][1];return r},j.indexOf=function(n,t,r){if(null==n)return-1;var e=0,u=n.length;if(r){if("number"!=typeof r)return e=j.sortedIndex(n,t),n[e]===t?e:-1;e=0>r?Math.max(0,u+r):r}if(y&&n.indexOf===y)return n.indexOf(t,r);for(;u>e;e++)if(n[e]===t)return e;return-1},j.lastIndexOf=function(n,t,r){if(null==n)return-1;var e=null!=r;if(b&&n.lastIndexOf===b)return e?n.lastIndexOf(t,r):n.lastIndexOf(t);for(var u=e?r:n.length;u--;)if(n[u]===t)return u;return-1},j.range=function(n,t,r){arguments.length<=1&&(t=n||0,n=0),r=arguments[2]||1;for(var e=Math.max(Math.ceil((t-n)/r),0),u=0,i=new Array(e);e>u;)i[u++]=n,n+=r;return i};var R=function(){};j.bind=function(n,t){var r,e;if(_&&n.bind===_)return _.apply(n,o.call(arguments,1));if(!j.isFunction(n))throw new TypeError;return r=o.call(arguments,2),e=function(){if(!(this instanceof e))return n.apply(t,r.concat(o.call(arguments)));R.prototype=n.prototype;var u=new R;R.prototype=null;var i=n.apply(u,r.concat(o.call(arguments)));return Object(i)===i?i:u}},j.partial=function(n){var t=o.call(arguments,1);return function(){for(var r=0,e=t.slice(),u=0,i=e.length;i>u;u++)e[u]===j&&(e[u]=arguments[r++]);for(;r=f?(clearTimeout(a),a=null,o=l,i=n.apply(e,u),e=u=null):a||r.trailing===!1||(a=setTimeout(c,f)),i}},j.debounce=function(n,t,r){var e,u,i,a,o,c=function(){var l=j.now()-a;t>l?e=setTimeout(c,t-l):(e=null,r||(o=n.apply(i,u),i=u=null))};return function(){i=this,u=arguments,a=j.now();var l=r&&!e;return e||(e=setTimeout(c,t)),l&&(o=n.apply(i,u),i=u=null),o}},j.once=function(n){var t,r=!1;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},j.wrap=function(n,t){return j.partial(t,n)},j.compose=function(){var n=arguments;return function(){for(var t=arguments,r=n.length-1;r>=0;r--)t=[n[r].apply(this,t)];return t[0]}},j.after=function(n,t){return function(){return--n<1?t.apply(this,arguments):void 0}},j.keys=function(n){if(!j.isObject(n))return[];if(w)return w(n);var t=[];for(var r in n)j.has(n,r)&&t.push(r);return t},j.values=function(n){for(var t=j.keys(n),r=t.length,e=new Array(r),u=0;r>u;u++)e[u]=n[t[u]];return e},j.pairs=function(n){for(var t=j.keys(n),r=t.length,e=new Array(r),u=0;r>u;u++)e[u]=[t[u],n[t[u]]];return e},j.invert=function(n){for(var t={},r=j.keys(n),e=0,u=r.length;u>e;e++)t[n[r[e]]]=r[e];return t},j.functions=j.methods=function(n){var t=[];for(var r in n)j.isFunction(n[r])&&t.push(r);return t.sort()},j.extend=function(n){return A(o.call(arguments,1),function(t){if(t)for(var r in t)n[r]=t[r]}),n},j.pick=function(n){var t={},r=c.apply(e,o.call(arguments,1));return A(r,function(r){r in n&&(t[r]=n[r])}),t},j.omit=function(n){var t={},r=c.apply(e,o.call(arguments,1));for(var u in n)j.contains(r,u)||(t[u]=n[u]);return t},j.defaults=function(n){return A(o.call(arguments,1),function(t){if(t)for(var r in t)n[r]===void 0&&(n[r]=t[r])}),n},j.clone=function(n){return j.isObject(n)?j.isArray(n)?n.slice():j.extend({},n):n},j.tap=function(n,t){return t(n),n};var S=function(n,t,r,e){if(n===t)return 0!==n||1/n==1/t;if(null==n||null==t)return n===t;n instanceof j&&(n=n._wrapped),t instanceof j&&(t=t._wrapped);var u=l.call(n);if(u!=l.call(t))return!1;switch(u){case"[object String]":return n==String(t);case"[object Number]":return n!=+n?t!=+t:0==n?1/n==1/t:n==+t;case"[object Date]":case"[object Boolean]":return+n==+t;case"[object RegExp]":return n.source==t.source&&n.global==t.global&&n.multiline==t.multiline&&n.ignoreCase==t.ignoreCase}if("object"!=typeof n||"object"!=typeof t)return!1;for(var i=r.length;i--;)if(r[i]==n)return e[i]==t;var a=n.constructor,o=t.constructor;if(a!==o&&!(j.isFunction(a)&&a instanceof a&&j.isFunction(o)&&o instanceof o)&&"constructor"in n&&"constructor"in t)return!1;r.push(n),e.push(t);var c=0,f=!0;if("[object Array]"==u){if(c=n.length,f=c==t.length)for(;c--&&(f=S(n[c],t[c],r,e)););}else{for(var s in n)if(j.has(n,s)&&(c++,!(f=j.has(t,s)&&S(n[s],t[s],r,e))))break;if(f){for(s in t)if(j.has(t,s)&&!c--)break;f=!c}}return r.pop(),e.pop(),f};j.isEqual=function(n,t){return S(n,t,[],[])},j.isEmpty=function(n){if(null==n)return!0;if(j.isArray(n)||j.isString(n))return 0===n.length;for(var t in n)if(j.has(n,t))return!1;return!0},j.isElement=function(n){return!(!n||1!==n.nodeType)},j.isArray=x||function(n){return"[object Array]"==l.call(n)},j.isObject=function(n){return n===Object(n)},A(["Arguments","Function","String","Number","Date","RegExp"],function(n){j["is"+n]=function(t){return l.call(t)=="[object "+n+"]"}}),j.isArguments(arguments)||(j.isArguments=function(n){return!(!n||!j.has(n,"callee"))}),"function"!=typeof/./&&(j.isFunction=function(n){return"function"==typeof n}),j.isFinite=function(n){return isFinite(n)&&!isNaN(parseFloat(n))},j.isNaN=function(n){return j.isNumber(n)&&n!=+n},j.isBoolean=function(n){return n===!0||n===!1||"[object Boolean]"==l.call(n)},j.isNull=function(n){return null===n},j.isUndefined=function(n){return n===void 0},j.has=function(n,t){return f.call(n,t)},j.noConflict=function(){return n._=t,this},j.identity=function(n){return n},j.constant=function(n){return function(){return n}},j.property=function(n){return function(t){return t[n]}},j.matches=function(n){return function(t){if(t===n)return!0;for(var r in n)if(n[r]!==t[r])return!1;return!0}},j.times=function(n,t,r){for(var e=Array(Math.max(0,n)),u=0;n>u;u++)e[u]=t.call(r,u);return e},j.random=function(n,t){return null==t&&(t=n,n=0),n+Math.floor(Math.random()*(t-n+1))},j.now=Date.now||function(){return(new Date).getTime()};var T={escape:{"&":"&","<":"<",">":">",'"':""","'":"'"}};T.unescape=j.invert(T.escape);var I={escape:new RegExp("["+j.keys(T.escape).join("")+"]","g"),unescape:new RegExp("("+j.keys(T.unescape).join("|")+")","g")};j.each(["escape","unescape"],function(n){j[n]=function(t){return null==t?"":(""+t).replace(I[n],function(t){return T[n][t]})}}),j.result=function(n,t){if(null==n)return void 0;var r=n[t];return j.isFunction(r)?r.call(n):r},j.mixin=function(n){A(j.functions(n),function(t){var r=j[t]=n[t];j.prototype[t]=function(){var n=[this._wrapped];return a.apply(n,arguments),z.call(this,r.apply(j,n))}})};var N=0;j.uniqueId=function(n){var t=++N+"";return n?n+t:t},j.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var q=/(.)^/,B={"'":"'","\\":"\\","\r":"r","\n":"n"," ":"t","\u2028":"u2028","\u2029":"u2029"},D=/\\|'|\r|\n|\t|\u2028|\u2029/g;j.template=function(n,t,r){var e;r=j.defaults({},r,j.templateSettings);var u=new RegExp([(r.escape||q).source,(r.interpolate||q).source,(r.evaluate||q).source].join("|")+"|$","g"),i=0,a="__p+='";n.replace(u,function(t,r,e,u,o){return a+=n.slice(i,o).replace(D,function(n){return"\\"+B[n]}),r&&(a+="'+\n((__t=("+r+"))==null?'':_.escape(__t))+\n'"),e&&(a+="'+\n((__t=("+e+"))==null?'':__t)+\n'"),u&&(a+="';\n"+u+"\n__p+='"),i=o+t.length,t}),a+="';\n",r.variable||(a="with(obj||{}){\n"+a+"}\n"),a="var __t,__p='',__j=Array.prototype.join,"+"print=function(){__p+=__j.call(arguments,'');};\n"+a+"return __p;\n";try{e=new Function(r.variable||"obj","_",a)}catch(o){throw o.source=a,o}if(t)return e(t,j);var c=function(n){return e.call(this,n,j)};return c.source="function("+(r.variable||"obj")+"){\n"+a+"}",c},j.chain=function(n){return j(n).chain()};var z=function(n){return this._chain?j(n).chain():n};j.mixin(j),A(["pop","push","reverse","shift","sort","splice","unshift"],function(n){var t=e[n];j.prototype[n]=function(){var r=this._wrapped;return t.apply(r,arguments),"shift"!=n&&"splice"!=n||0!==r.length||delete r[0],z.call(this,r)}}),A(["concat","join","slice"],function(n){var t=e[n];j.prototype[n]=function(){return z.call(this,t.apply(this._wrapped,arguments))}}),j.extend(j.prototype,{chain:function(){return this._chain=!0,this},value:function(){return this._wrapped}}),"function"==typeof define&&define.amd&&define("underscore",[],function(){return j})}).call(this); -------------------------------------------------------------------------------- /src/test/scala/BUILD: -------------------------------------------------------------------------------- 1 | junit_tests( 2 | name='scala', 3 | dependencies=[ 4 | '3rdparty/jvm/junit', 5 | '3rdparty/jvm/org/mockito:mockito-all', 6 | '3rdparty/jvm/org/scalatest', 7 | 'diffy/src/main/scala', 8 | 'finatra/http:test-deps', 9 | 'finatra/inject/inject-server:test-deps', 10 | 'util/util-core', 11 | ], 12 | sources=rglobs('*.scala'), 13 | ) 14 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/ParentSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import org.mockito.Matchers.{eq => _eq} 4 | import org.mockito.Mockito.RETURNS_SMART_NULLS 5 | import org.scalatest.mock.MockitoSugar 6 | import org.scalatest.{FunSpec, MustMatchers, OneInstancePerTest} 7 | 8 | /** 9 | * Base trait for ScalaTest implementations that includes commonly used 10 | * mixins 11 | */ 12 | trait ParentSpec 13 | extends FunSpec 14 | with MustMatchers 15 | with MockitoSugar 16 | with OneInstancePerTest 17 | { 18 | def smartMock[T <: AnyRef](implicit m: Manifest[T]): T = mock[T](RETURNS_SMART_NULLS) 19 | def argEq[T](value: T) = _eq(value) 20 | } -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/StartupFeatureTest.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import com.google.inject.Stage 4 | import com.twitter.finatra.http.test.EmbeddedHttpServer 5 | import com.twitter.inject.Test 6 | 7 | class StartupFeatureTest extends Test { 8 | 9 | val server = new EmbeddedHttpServer( 10 | stage = Stage.PRODUCTION, 11 | twitterServer = new MainService { 12 | }, 13 | extraArgs = Seq( 14 | "-proxy.port=:0", 15 | "-candidate=localhost:80", 16 | "-master.primary=localhost:80", 17 | "-master.secondary=localhost:80", 18 | "-service.protocol=http")) 19 | 20 | "verify startup" in { 21 | server.assertHealthy() 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/TestHelper.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy 2 | 3 | import java.net.InetSocketAddress 4 | 5 | import com.twitter.diffy.proxy._ 6 | import com.twitter.util.TimeConversions._ 7 | import org.scalatest.mock.MockitoSugar 8 | import com.twitter.diffy.analysis._ 9 | import com.twitter.diffy.compare.Difference 10 | import com.twitter.diffy.proxy.ResponseMode.EmptyResponse 11 | 12 | object TestHelper extends MockitoSugar { 13 | lazy val testSettings = Settings( 14 | datacenter = "test", 15 | servicePort = new InetSocketAddress(9999), 16 | candidate = mock[Target], 17 | primary = mock[Target], 18 | secondary = mock[Target], 19 | protocol = "test", 20 | clientId = "test", 21 | pathToThriftJar = "test", 22 | serviceClass = "test", 23 | serviceName = "test", 24 | apiRoot = "test", 25 | enableThriftMux = false, 26 | relativeThreshold = 0.0, 27 | absoluteThreshold = 0.0, 28 | teamEmail = "test", 29 | emailDelay = 0.seconds, 30 | rootUrl = "test", 31 | allowHttpSideEffects = true, 32 | responseMode = EmptyResponse, 33 | excludeHttpHeadersComparison = true, 34 | skipEmailsWhenNoErrors = false, 35 | httpsPort = "443" 36 | ) 37 | 38 | def makeEmptyJoinedDifferences = { 39 | val rawCounter = RawDifferenceCounter(new InMemoryDifferenceCounter()) 40 | val noiseCounter = NoiseDifferenceCounter(new InMemoryDifferenceCounter()) 41 | JoinedDifferences(rawCounter, noiseCounter) 42 | } 43 | 44 | def makePopulatedJoinedDifferences(endpoint : String, diffs : Map[String, Difference]) = { 45 | val rawCounter = RawDifferenceCounter(new InMemoryDifferenceCounter()) 46 | val noiseCounter = NoiseDifferenceCounter(new InMemoryDifferenceCounter()) 47 | val data = new InMemoryEndpointMetadata() 48 | data.add(diffs) 49 | rawCounter.counter.asInstanceOf[InMemoryDifferenceCounter].endpointsMap += (endpoint -> data) 50 | 51 | JoinedDifferences(rawCounter, noiseCounter) 52 | } 53 | } -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/compare/DifferenceSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.compare 2 | 3 | import com.twitter.diffy.lifter.JsonLifter 4 | import java.nio.ByteBuffer 5 | import org.junit.runner.RunWith 6 | import org.scalatest.FunSpec 7 | import org.scalatest.junit.JUnitRunner 8 | import org.scalatest.matchers.MustMatchers 9 | import org.scalatest.mock.MockitoSugar 10 | 11 | @RunWith(classOf[JUnitRunner]) 12 | class DifferenceSpec 13 | extends FunSpec 14 | with MustMatchers 15 | with MockitoSugar 16 | { 17 | case class Name(first: String, last: String) 18 | case class User(id: Long, name: Name) 19 | case class UserResult(found: Map[Long, User], notFound: Seq[Long]) 20 | 21 | describe("Difference") { 22 | it("should detect when there is no difference between two objects") { 23 | val testCases = 24 | Seq( 25 | (true, true), 26 | (1, 1), 27 | ("hello", "hello"), 28 | (Seq(1,2,3), Seq(1,2,3)), 29 | (Array(1,2,3), Array(1,2,3)), 30 | (Set(1,2,3), Set(1,2,3)), 31 | (Map(1->"1", 2-> "2"), Map(1->"1", 2->"2")), 32 | (Name("puneet", "khanduri"), Name("puneet", "khanduri")) 33 | ) 34 | testCases foreach { case (left, right) => 35 | Difference(left, right) must be(NoDifference(Difference.lift(left))) 36 | Difference(JsonLifter(left), JsonLifter(right)) must be(NoDifference(Difference.lift(JsonLifter(left)))) 37 | } 38 | } 39 | 40 | it("should detect when two primitives are not equal") { 41 | val testCases = 42 | Seq( 43 | (true, false), 44 | (1, 2), 45 | ("hello", "world"), 46 | (1.8, 3.9) 47 | ) 48 | testCases foreach { case (left, right) => 49 | Difference(left, right) must be(PrimitiveDifference(left,right)) 50 | } 51 | } 52 | 53 | it("should throw TypeDifference when 2 primitives are of different types") { 54 | val testCases = Seq( 55 | (true, 1), 56 | (1, "hello"), 57 | ("hello", 1.2), 58 | (2, 3L), 59 | (3.2, 2), 60 | ("hello", false) 61 | ) 62 | testCases foreach { case (left, right) => 63 | Difference(left, right) must be(TypeDifference(left, right)) 64 | } 65 | } 66 | 67 | it("should detect a difference between JsonNull and other objects") { 68 | val testCases = Seq(true, 1, "hello", Seq(1, 2, 3)) 69 | 70 | testCases foreach { testCase => 71 | Difference(JsonLifter.JsonNull, testCase) must 72 | be(TypeDifference(JsonLifter.JsonNull, testCase)) 73 | } 74 | } 75 | 76 | it("should detect no difference when two JsonNull objects are compared") { 77 | Difference(JsonLifter.JsonNull, JsonLifter.JsonNull) must be(NoDifference(JsonLifter.JsonNull)) 78 | } 79 | 80 | it("should return an OrderingDifference when two Seqs have the same elements but different order") { 81 | Difference(Seq("a", "b", "c"), Seq("c", "b", "a")) must be( 82 | OrderingDifference(leftPattern = Seq(0, 1, 2), rightPattern = Seq(2, 1, 0)) 83 | ) 84 | } 85 | 86 | it("should return a SeqSizeDifference when two Seqs are of different size") { 87 | Difference(Seq("a", "a", "b", "b"), Seq("a", "b", "b", "c", "c")) must be( 88 | SeqSizeDifference( 89 | leftNotRight = Seq("a"), 90 | rightNotLeft = Seq("c", "c") 91 | ) 92 | ) 93 | } 94 | 95 | it("should return a IndexedDifference when two Seqs are of the same size but have different elements") { 96 | Difference(Seq("a", "b", "c"), Seq("a", "b", "d")) must be( 97 | IndexedDifference( 98 | indexedDiffs = 99 | Seq( 100 | NoDifference("a"), 101 | NoDifference("b"), 102 | PrimitiveDifference("c", "d") 103 | ) 104 | ) 105 | ) 106 | } 107 | 108 | it("should detect when two Sets are not equal") { 109 | Difference(Set(1, 2, 3), Set(2, 3, 4)) must be( 110 | SetDifference(leftNotRight = Set(1), rightNotLeft = Set(4)) 111 | ) 112 | } 113 | 114 | it("should detect when two Maps are not equal") { 115 | Difference(Map(1 -> 2, 2 -> 3, 3 -> 4), Map(1 -> 2, 2 -> 4, 4 -> 5)) must be( 116 | MapDifference( 117 | keys = SetDifference(leftNotRight = Set(3), rightNotLeft = Set(4)), 118 | values = Map(1 -> NoDifference(2), 2 -> PrimitiveDifference(3, 4)) 119 | ) 120 | ) 121 | } 122 | 123 | it("should detect when two case class instances are not equal") { 124 | Difference(Name("puneet", "khanduri"), Name("prashant", "khanduri")) must be( 125 | ObjectDifference(MapDifference( 126 | keys = NoDifference(Set("first", "last")), 127 | values = 128 | Map( 129 | "first" -> PrimitiveDifference("puneet", "prashant"), 130 | "last" -> NoDifference("khanduri") 131 | ) 132 | )) 133 | ) 134 | } 135 | 136 | it("should detect difference between binaries embedded within thrift structs") { 137 | val left = ByteBuffer.wrap("leftBuffer".getBytes) 138 | val right = ByteBuffer.wrap("rightBuffer".getBytes) 139 | Difference(left,right) must be ( 140 | PrimitiveDifference("leftBuffer", "rightBuffer") 141 | ) 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/lifter/HtmlLifterSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.twitter.diffy.ParentSpec 4 | import com.twitter.diffy.compare.{Difference, PrimitiveDifference} 5 | import org.jsoup.Jsoup 6 | import org.junit.runner.RunWith 7 | import org.scalatest.junit.JUnitRunner 8 | 9 | @RunWith(classOf[JUnitRunner]) 10 | class HtmlLifterSpec extends ParentSpec { 11 | describe("HtmlLifter"){ 12 | val simpleActualHtml = """Sample HTML

Hello World

Lorem ipsum dolor sit amet.

""" 13 | val simpleExpectedHtml = """Sample HTML

Hello World

Lorem ipsum dolor sit amet.

""" 14 | 15 | val simpleActualDoc = Jsoup.parse(simpleActualHtml) 16 | val simpleExpectedDoc = Jsoup.parse(simpleExpectedHtml) 17 | 18 | it("should return a FieldMap") { 19 | HtmlLifter.lift(simpleActualDoc) mustBe a [FieldMap[_]] 20 | } 21 | 22 | it("should return a Primitive Difference") { 23 | Difference(HtmlLifter.lift(simpleActualDoc), HtmlLifter.lift(simpleExpectedDoc)).flattened must be (FieldMap(Map("body.children.children.attributes.class.PrimitiveDifference" -> PrimitiveDifference("box","round")))) 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/lifter/HttpLifterSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.fasterxml.jackson.databind.JsonNode 4 | import com.google.common.net.MediaType 5 | import com.twitter.diffy.ParentSpec 6 | import com.twitter.diffy.lifter.HttpLifter.MalformedJsonContentException 7 | import com.twitter.io.Charsets 8 | import com.twitter.util.{Await, Throw, Try} 9 | import org.jboss.netty.buffer.ChannelBuffers 10 | import org.jboss.netty.handler.codec.http._ 11 | import org.junit.runner.RunWith 12 | import org.scalatest.Matchers._ 13 | import org.scalatest.OptionValues._ 14 | import org.scalatest.junit.JUnitRunner 15 | 16 | import scala.collection.mutable.ArrayBuffer 17 | 18 | @RunWith(classOf[JUnitRunner]) 19 | class HttpLifterSpec extends ParentSpec { 20 | object Fixture { 21 | val reqUri = "/0/accounts" 22 | 23 | val jsonContentType = MediaType.JSON_UTF_8.toString 24 | val textContentType = MediaType.PLAIN_TEXT_UTF_8.toString 25 | val htmlContentType = MediaType.HTML_UTF_8.toString 26 | 27 | val controllerEndpoint = "account/index" 28 | 29 | val validJsonBody = 30 | "{" + 31 | "\"data_type\": \"account\"," + 32 | "\"data\": [" + 33 | "{" + 34 | "\"name\": \"Account 1\"," + 35 | "\"deleted\": false" + 36 | "}," + 37 | "{" + 38 | "\"name\": \"Account 2\"," + 39 | "\"deleted\": true" + 40 | "}" + 41 | "]," + 42 | "\"total_count\": 2," + 43 | "\"next_cursor\": null" + 44 | "}" 45 | 46 | val invalidJsonBody = "invalid" 47 | 48 | val validHtmlBody = """Sample HTML

Hello World

Lorem ipsum dolor sit amet.

""" 49 | 50 | val testException = new Exception("test exception") 51 | 52 | def request(method: HttpMethod, uri: String, body: Option[String] = None): HttpRequest = { 53 | val req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, uri) 54 | body foreach { b => req.setContent(ChannelBuffers.wrappedBuffer(b.getBytes(Charsets.Utf8)))} 55 | req 56 | } 57 | 58 | def response(status: HttpResponseStatus, body: String): HttpResponse = { 59 | val resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status) 60 | resp.headers() 61 | .add(HttpHeaders.Names.CONTENT_LENGTH, body.length) 62 | .add(HttpHeaders.Names.CONTENT_TYPE, jsonContentType) 63 | .add(HttpLifter.ControllerEndpointHeaderName, controllerEndpoint) 64 | resp.setContent(ChannelBuffers.wrappedBuffer(body.getBytes(Charsets.Utf8))) 65 | resp 66 | } 67 | } 68 | 69 | describe("HttpLifter") { 70 | import Fixture._ 71 | describe("LiftRequest") { 72 | it("lift simple Get request") { 73 | val lifter = new HttpLifter(false) 74 | val req = request(HttpMethod.GET, reqUri) 75 | req.headers().add("Canonical-Resource", "endpoint") 76 | 77 | val msg = Await.result(lifter.liftRequest(req)) 78 | val resultFieldMap = msg.result.asInstanceOf[FieldMap[String]] 79 | 80 | msg.endpoint.get should equal ("endpoint") 81 | resultFieldMap.get("request").get should equal (req.toString) 82 | } 83 | 84 | it("lift simple Post request") { 85 | val lifter = new HttpLifter(false) 86 | val requestBody = "request_body" 87 | val req = request(HttpMethod.POST, reqUri, Some(requestBody)) 88 | req.headers().add("Canonical-Resource", "endpoint") 89 | 90 | val msg = Await.result(lifter.liftRequest(req)) 91 | val resultFieldMap = msg.result.asInstanceOf[FieldMap[String]] 92 | 93 | msg.endpoint.get should equal ("endpoint") 94 | resultFieldMap.get("request").get should equal (req.toString) 95 | resultFieldMap.get("body").get should equal (requestBody) 96 | } 97 | } 98 | 99 | describe("LiftResponse") { 100 | it("lift simple Json response") { 101 | checkJsonContentTypeIsLifted(MediaType.JSON_UTF_8.toString) 102 | } 103 | 104 | it("lift simple Json response when the charset is not set") { 105 | checkJsonContentTypeIsLifted(MediaType.JSON_UTF_8.withoutParameters().toString) 106 | } 107 | 108 | it("exclude header in response map if excludeHttpHeadersComparison flag is off") { 109 | val lifter = new HttpLifter(true) 110 | val resp = response(HttpResponseStatus.OK, validJsonBody) 111 | 112 | val msg = Await.result(lifter.liftResponse(Try(resp))) 113 | val resultFieldMap = msg.result 114 | 115 | resultFieldMap.get("headers") should be (None) 116 | } 117 | 118 | it("throw MalformedJsonContentException when json body is malformed") { 119 | val lifter = new HttpLifter(false) 120 | val resp = response(HttpResponseStatus.OK, invalidJsonBody) 121 | val thrown = the [MalformedJsonContentException] thrownBy { 122 | Await.result(lifter.liftResponse(Try(resp))) 123 | } 124 | 125 | thrown.getCause should not be (null) 126 | } 127 | 128 | it("only compare headers when ContentType header was not set") { 129 | val lifter = new HttpLifter(false) 130 | val resp = response(HttpResponseStatus.OK, validJsonBody) 131 | resp.headers.remove(HttpHeaders.Names.CONTENT_TYPE) 132 | 133 | val msg = Await.result(lifter.liftResponse(Try(resp))) 134 | val resultFieldMap = msg.result 135 | 136 | resultFieldMap.get("headers") should not be (None) 137 | } 138 | 139 | it("returns FieldMap when ContentType header is Html") { 140 | val lifter = new HttpLifter(false) 141 | val resp = response(HttpResponseStatus.OK, validHtmlBody) 142 | resp.headers.set(HttpHeaders.Names.CONTENT_TYPE, MediaType.HTML_UTF_8.toString) 143 | 144 | val msg = Await.result(lifter.liftResponse(Try(resp))) 145 | val resultFieldMap = msg.result 146 | 147 | resultFieldMap shouldBe a [FieldMap[_]] 148 | } 149 | 150 | it("throw ContentTypeNotSupportedException when ContentType header is not Json or Html") { 151 | val lifter = new HttpLifter(false) 152 | val resp = response(HttpResponseStatus.OK, validJsonBody) 153 | resp.headers.remove(HttpHeaders.Names.CONTENT_TYPE) 154 | .add(HttpHeaders.Names.CONTENT_TYPE, textContentType) 155 | 156 | val thrown = the [Exception] thrownBy { 157 | Await.result(lifter.liftResponse(Try(resp))) 158 | } 159 | 160 | thrown.getMessage should be (HttpLifter.contentTypeNotSupportedException(MediaType.PLAIN_TEXT_UTF_8.toString).getMessage) 161 | } 162 | 163 | it("return None as controller endpoint when action header was not set") { 164 | val lifter = new HttpLifter(false) 165 | val resp = response(HttpResponseStatus.OK, validJsonBody) 166 | resp.headers.remove(HttpLifter.ControllerEndpointHeaderName) 167 | val msg = Await.result(lifter.liftResponse(Try(resp))) 168 | 169 | msg.endpoint should equal (None) 170 | } 171 | 172 | it("propagate exception if response try failed") { 173 | val lifter = new HttpLifter(false) 174 | val thrown = the [Exception] thrownBy { 175 | Await.result(lifter.liftResponse(Throw(testException))) 176 | } 177 | 178 | thrown should be (testException) 179 | } 180 | 181 | it("only compares header when Content-Length is zero") { 182 | val lifter = new HttpLifter(false) 183 | val resp = response(HttpResponseStatus.OK, "") 184 | resp.headers.set(HttpHeaders.Names.CONTENT_TYPE, MediaType.GIF.toString) 185 | 186 | val msg = Await.result(lifter.liftResponse(Try(resp))) 187 | val resultFieldMap = msg.result 188 | resultFieldMap.get("headers") should not be (None) 189 | } 190 | 191 | def checkJsonContentTypeIsLifted(contentType: String): Unit = { 192 | val lifter = new HttpLifter(false) 193 | val resp = response(HttpResponseStatus.OK, validJsonBody) 194 | resp.headers.set(HttpHeaders.Names.CONTENT_TYPE, contentType) 195 | 196 | val msg = Await.result(lifter.liftResponse(Try(resp))) 197 | val resultFieldMap = msg.result.asInstanceOf[FieldMap[Map[String, Any]]] 198 | val status = resultFieldMap.keySet.headOption.value 199 | val headers = resultFieldMap.get(status).value 200 | .get("headers").value.asInstanceOf[FieldMap[Any]] 201 | val content = resultFieldMap.get(status).value.get("content").value.asInstanceOf[JsonNode] 202 | 203 | msg.endpoint.get should equal(controllerEndpoint) 204 | status should equal(HttpResponseStatus.OK.getCode.toString) 205 | headers.get(HttpLifter.ControllerEndpointHeaderName).get should equal( 206 | ArrayBuffer(controllerEndpoint)) 207 | headers.get(HttpHeaders.Names.CONTENT_TYPE).get should equal( 208 | ArrayBuffer(contentType)) 209 | headers.get(HttpHeaders.Names.CONTENT_LENGTH).get should equal( 210 | ArrayBuffer(validJsonBody.length.toString)) 211 | content.get("data_type").asText should equal("account") 212 | content.get("total_count").asInt should equal(2) 213 | content.get("next_cursor").isNull should be(true) 214 | val data = content.get("data") 215 | data should have size (2) 216 | data.get(0).get("name").asText should equal("Account 1") 217 | data.get(0).get("deleted").asBoolean should equal(false) 218 | data.get(1).get("name").asText should equal("Account 2") 219 | data.get(1).get("deleted").asBoolean should equal(true) 220 | } 221 | } 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/lifter/JsonLifterSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.twitter.diffy.ParentSpec 4 | import org.junit.runner.RunWith 5 | import org.scalatest.junit.JUnitRunner 6 | 7 | @RunWith(classOf[JUnitRunner]) 8 | class JsonLifterSpec extends ParentSpec { 9 | describe("JsonLifter"){ 10 | it("should correctly lift maps when keys are invalid identifier prefixes") { 11 | JsonLifter.lift(JsonLifter.decode("""{"1":1}""")) mustBe a [Map[_, _]] 12 | } 13 | 14 | it("should correctly lift objects when keys are valid identifier prefixes") { 15 | JsonLifter.lift(JsonLifter.decode("""{"a":1}""")) mustBe a [FieldMap[_]] 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/lifter/StringLifterSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.lifter 2 | 3 | import com.twitter.diffy.ParentSpec 4 | import org.junit.runner.RunWith 5 | import org.scalatest.junit.JUnitRunner 6 | 7 | @RunWith(classOf[JUnitRunner]) 8 | class StringLifterSpec extends ParentSpec { 9 | describe("String") { 10 | val htmlString = "as

it's an html!

" 11 | val jsonString = """{"a": "it's a json!" }""" 12 | val regularString = "hello world!" 13 | 14 | it("should be true") { 15 | StringLifter.htmlRegexPattern.findFirstIn(htmlString).isDefined must be (true) 16 | } 17 | 18 | it("must return a FieldMap when lifted (html)") { 19 | StringLifter.lift(htmlString) mustBe a [FieldMap[_]] 20 | } 21 | 22 | it("must return a FieldMap when lifted (json)") { 23 | StringLifter.lift(jsonString) mustBe a [FieldMap[_]] 24 | } 25 | 26 | it("must return the original string when lifted") { 27 | StringLifter.lift(regularString) must be ("hello world!") 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/proxy/SequentialMulticastServiceSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.proxy 2 | 3 | import com.twitter.diffy.ParentSpec 4 | import com.twitter.finagle.Service 5 | import com.twitter.util._ 6 | import org.junit.runner.RunWith 7 | import org.mockito.Mockito._ 8 | import org.scalatest.junit.JUnitRunner 9 | 10 | @RunWith(classOf[JUnitRunner]) 11 | class SequentialMulticastServiceSpec extends ParentSpec { 12 | 13 | describe("SequentialMulticastService"){ 14 | val first, second = mock[Service[String, String]] 15 | val multicastHandler = new SequentialMulticastService(Seq(first, second)) 16 | 17 | it("must not access second until first is done"){ 18 | val firstResponse, secondResponse = new Promise[String] 19 | when(first("anyString")) thenReturn firstResponse 20 | when(second("anyString")) thenReturn secondResponse 21 | val result = multicastHandler("anyString") 22 | verify(first)("anyString") 23 | verifyZeroInteractions(second) 24 | firstResponse.setValue("first") 25 | verify(second)("anyString") 26 | secondResponse.setValue("second") 27 | Await.result(result) must be(Seq(Try("first"), Try("second"))) 28 | } 29 | 30 | it("should call all services") { 31 | val request = "anyString" 32 | val services = Seq.fill(100)(mock[Service[String, Int]]) 33 | val responses = Seq.fill(100)(new Promise[Int]) 34 | val svcResp = services zip responses 35 | svcResp foreach { case (service, response) => 36 | when(service(request)) thenReturn response 37 | } 38 | val sequentialMulticast = new SequentialMulticastService(services) 39 | val result = sequentialMulticast("anyString") 40 | def verifySequentialInteraction(s: Seq[((Service[String,Int], Promise[Int]), Int)]): Unit = s match { 41 | case Nil => 42 | case Seq(((svc, resp), index), tail@_*) => { 43 | verify(svc)(request) 44 | tail foreach { case ((subsequent, _), _) => 45 | verifyZeroInteractions(subsequent) 46 | } 47 | resp.setValue(index) 48 | verifySequentialInteraction(tail) 49 | } 50 | } 51 | verifySequentialInteraction(svcResp.zipWithIndex) 52 | Await.result(result) must be((0 until 100).toSeq map {i => Try(i)}) 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/util/EmailSenderSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.util 2 | 3 | import java.util.Date 4 | 5 | import com.twitter.diffy.ParentSpec 6 | import com.twitter.logging.Logger 7 | import com.twitter.util.Await 8 | import org.junit.runner.RunWith 9 | import org.mockito.Mockito._ 10 | import org.scalatest.junit.JUnitRunner 11 | 12 | @RunWith(classOf[JUnitRunner]) 13 | class EmailSenderSpec extends ParentSpec { 14 | 15 | val log = mock[Logger] 16 | val sender = new EmailSender(log, _ => ()) 17 | describe("EmailSender") { 18 | it("should not encouter any errors while trying to compose emails") { 19 | Await.result( 20 | sender( 21 | SimpleMessage( 22 | from = "Diffy ", 23 | to = "diffy-team@twitter.com", 24 | bcc = "diffy-team@twitter.com", 25 | subject = "Diffy Report at " + new Date, 26 | body = "just testing emails from mesos!" 27 | ) 28 | ) 29 | ) 30 | verifyZeroInteractions(log) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/scala/com/twitter/diffy/workflow/DifferenceStatsMonitorSpec.scala: -------------------------------------------------------------------------------- 1 | package com.twitter.diffy.workflow 2 | 3 | import com.twitter.diffy.ParentSpec 4 | import com.twitter.diffy.analysis.{RawDifferenceCounter, EndpointMetadata, DifferenceCounter} 5 | import com.twitter.finagle.stats.InMemoryStatsReceiver 6 | import com.twitter.util.{Future, MockTimer, Time} 7 | import com.twitter.util.TimeConversions._ 8 | import org.junit.runner.RunWith 9 | import org.mockito.Mockito._ 10 | import org.scalatest.junit.JUnitRunner 11 | 12 | @RunWith(classOf[JUnitRunner]) 13 | class DifferenceStatsMonitorSpec extends ParentSpec { 14 | describe("DifferenceStatsMonitor"){ 15 | val diffCounter = mock[DifferenceCounter] 16 | val metadata = 17 | new EndpointMetadata { 18 | override val differences = 0 19 | override val total = 0 20 | } 21 | 22 | val endpoints = Map("endpointName" -> metadata) 23 | when(diffCounter.endpoints) thenReturn Future.value(endpoints) 24 | 25 | val stats = new InMemoryStatsReceiver 26 | val timer = new MockTimer 27 | val monitor = new DifferenceStatsMonitor(RawDifferenceCounter(diffCounter), stats, timer) 28 | 29 | it("must add gauges after waiting a minute"){ 30 | Time.withCurrentTimeFrozen { tc => 31 | monitor.schedule() 32 | timer.tasks.size must be(1) 33 | stats.gauges.size must be(0) 34 | tc.advance(1.minute) 35 | timer.tick() 36 | timer.tasks.size must be(1) 37 | stats.gauges.size must be(2) 38 | stats.gauges.keySet map { _.takeRight(2) } must be(Set(Seq("endpointName", "total"), Seq("endpointName", "differences"))) 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /version.sbt: -------------------------------------------------------------------------------- 1 | version in ThisBuild := "0.0.2-SNAPSHOT" --------------------------------------------------------------------------------