github.com/thanos-io/thanos@v0.32.5/docs/proposals-accepted/202107-protobuf-openapi-httpapi.md (about) 1 --- 2 type: proposal 3 title: Expose Thanos APIs to OpenAPI/protobuf and expose on website and UIs 4 status: accepted 5 owner: Hangzhi 6 menu: proposals-accepted 7 --- 8 9 ## **Expose Thanos APIs to OpenAPI/protobuf and expose on website and UIs** 10 11 * Owners: 12 * Hangzhi 13 * Related Tickets: 14 * Program Proposal: https://github.com/thanos-io/thanos/issues/4102 15 * Need for auto-documentation in Prometheus: https://github.com/prometheus/prometheus/issues/7192, https://github.com/prometheus/prometheus/issues/5567 16 * Other docs: 17 * A proposal of LFX mentorship: https://docs.google.com/document/d/1GeyFCk4QkVSrx4pQ4OSmOA1KE_FvmJRi_aq9hr2GoZQ/edit?usp=sharing 18 19 This design doc is proposing a design for Thanos HTTP API defined in protobuf and OpenAPI. 20 21 ## **Motivations** 22 23 To improve Thanos usage for users, we would like to define our HTTP APIs in protobuf/OpenAPI and expose those in the repository. OpenAPI is a language for describing REST APIs with a widely-used tooling ecosystem. With OpenAPI, developers can generate live documentation, validate APIs and even generate client and server stubs from OpenAPI to use our APIs efficiently. Also, the auto-generated documentation problem prevent documentation errors (Prometheus#7192, Prometheus#5567). Protocol Buffers (a.k.a., protobuf) is well-known as a mechanism for serializing structured data, and it's usually used to define gRPC APIs. Also, protobuf specification could be used to define REST API. We hope to use protobuf to define our APIs for consistency, while we also want to leverage the tooling ecosystem of OpenAPI. 24 25 So, we want to define REST APIs in protobuf, generate OpenAPI definition from protobuf with [gnostic](https://github.com/google/gnostic). This would allow users to use tools for documentation, validation, type checking, and even interface code generation to use our APIs efficiently. 26 27 Similarly, we want to reuse this work in Prometheus. 28 29 ### **Pitfalls of the current solution** 30 31 * Documentation, Server code, and client code boilerplate needs to be written manually. 32 * Hand-written interface code is expensive to write and hard to maintain. 33 * It's hard to discover the current API programmatically. 34 * When modifying the API, it is very difficult to know if this breaks downstream users. 35 36 ## **Goals** 37 38 * Define all APIs in protobuf. 39 * Be able to generate OpenAPI3 from protobuf. 40 * Auto-generate documentation with OpenAPI3 specification. 41 * Generate server code from API specification (OpenAPI3 or protobuf). 42 43 ## **Non-Goals** 44 45 * Don't mix gRPC with HTTP APIs in the same protobuf package 46 47 ## **How** 48 49 * Define Thanos APIs in protobuf specification. 50 * Generate OpenAPI from protobuf with gnostic extension. 51 * Generate documentation from OpenAPI with [Swagger](https://github.com/swagger-api/swagger-codegen). 52 * Generate server and client API stubs from OpenAPI with Swagger. 53 54 ## **Alternatives** 55 56 ## **Define HTTP RESTful API only in OpenAPI. Not in protobuf.** 57 58 1. Pros: 59 1. There are might be some complexity, edge case and extra tooling to make the 3-step process (proto -> OpenAPI -> documentation to work. 60 61 2. Cons: 62 1. We write API definitions in protobuf for consistency. OpenAPI is less consistent compared to protobuf in a project built on Golang. 63 2. Protobuf definition is more concise to write. 64 65 ## **Define APIs in gRPC and have RESTful APIs alongside with grpc - gateway** 66 67 1. Pros: 68 1. We can have gRPC and RESTful APIs at the same time. 69 2. We have gRPC APIs like rules API. 70 2. Cons: 71 1. There are might be some complexity, edge cases and extra tooling to make the process (define gRPC API and get RESTful APIs with grpc-gateway) work. 72 2. We need to redefine our API service in gRPC since most of Thanos APIs and Prometheus APIs are RESTful APIs. 73 3. We need to run another sidecar (complexity of running the system). 74 4. Semantics of gRPC and HTTP might be different and surprising for end user. 75 5. We want to reuse in Prometheus and Prometheus does not support gRPC (gRPC dependency was removed from codebase). 76 6. Same port library is not maintained. (cmux) 77 78 ## **Action Plan** 79 80 * [ ] Experiments in rules API. 81 * [ ] Define all HTTP APIs in Protobuf. 82 * [ ] Generate documentation, client code, and server stubs from OpenAPI specification.