Skip to content

Add OpenAPI 3.2 specification generation for Prometheus HTTP API#17825

Merged
roidelapluie merged 6 commits intoprometheus:mainfrom
roidelapluie:roidelapluie/openapi
Jan 29, 2026
Merged

Add OpenAPI 3.2 specification generation for Prometheus HTTP API#17825
roidelapluie merged 6 commits intoprometheus:mainfrom
roidelapluie:roidelapluie/openapi

Conversation

@roidelapluie
Copy link
Member

@roidelapluie roidelapluie commented Jan 9, 2026

This commit introduces an OpenAPI specification for the Prometheus API. After testing multiple code-generation servers with built-in APIs, this implementation uses an independent spec file outside of the critical path. This spec file is tested with a framework present in this pull request.

The specification helps clients know which parameters they can use and is served at /api/v1/openapi.yaml. The spec file will evolve with the Prometheus API and has the same version number.

Downstream projects can tune the APIs presented in the spec file with configuration options using the IncludePaths setting for path filtering.

In the future, there is room to generate a server from this spec file (e.g. with interfaces), but this is out of scope for this pull request.

Architecture:

  • Core OpenAPI infrastructure (openapi.go): Dynamic spec building, caching, and thread-safe spec generation
  • Schema definitions (openapi_schemas.go): Complete type definitions for all API request and response types
  • Path specifications (openapi_paths.go): Endpoint definitions with parameters, request bodies, and response schemas
  • Examples (openapi_examples.go): Realistic request/response examples
  • Helper functions (openapi_helpers.go): Reusable builders for common OpenAPI structures

Testing:

  • Comprehensive test suite with golden file validation
  • Test helpers package for API testing infrastructure
  • OpenAPI compliance validation utilities

The golden file captures the complete specification for snapshot testing. Update with: go test -run TestOpenAPIGolden -update-openapi-spec

REVIEWERS: The most important thing to check would be the OpenAPI golden file (web/api/v1/testdata/openapi_golden.yaml). Test scenarios are important as they test the actual OpenAPI spec validity.

Which issue(s) does the PR fix:

Fixes #2392

Does this PR introduce a user-facing change?

[FEATURE] web: Add OpenAPI 3.2 specification for the HTTP API at /api/v1/openapi.yaml

@roidelapluie roidelapluie force-pushed the roidelapluie/openapi branch 5 times, most recently from d86390b to 8ea23c6 Compare January 9, 2026 14:39
@roidelapluie roidelapluie force-pushed the roidelapluie/openapi branch 6 times, most recently from a87a421 to 2126d70 Compare January 9, 2026 16:45
@roidelapluie roidelapluie requested a review from juliusv as a code owner January 9, 2026 16:45
@juliusv
Copy link
Member

juliusv commented Jan 12, 2026

Hey hey thanks, that's a biggie :) Since I don't know much about OpenAPI yet, I will have to study things a bit. I also read the issue at #2392 and saw that @bwplotka has knowledge and opinions on an OpenAPI spec, so his input here would probably be interesting as well.

@krajorama krajorama self-assigned this Jan 20, 2026
Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be continued

Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be continued

Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work

Did you try generate Go client for this? Curious if we can finally replace client_golang api package 🤔

Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fantastic, I did some spot checks on the main endpoints and also let Claude Opus 4.5 take a look.

I've added one more comment, so if we resolve my two comments and Bartek's question, I think we can approve.

@roidelapluie roidelapluie requested a review from a team as a code owner January 28, 2026 10:10
@roidelapluie
Copy link
Member Author

I gene

Amazing work

Did you try generate Go client for this? Curious if we can finally replace client_golang api package 🤔

I generated https://github.com/roidelapluie/promclient

Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

This commit introduces an OpenAPI specification for the Prometheus API.
After testing multiple code-generation servers with built-in APIs, this
implementation uses an independent spec file outside of the critical path.
This spec file is tested with a framework present in this pull request.

The specification helps clients know which parameters they can use and is
served at /api/v1/openapi.yaml. The spec file will evolve with the
Prometheus API and has the same version number.

Downstream projects can tune the APIs presented in the spec file with
configuration options using the IncludePaths setting for path filtering.

In the future, there is room to generate a server from this spec file
(e.g. with interfaces), but this is out of scope for this pull request.

Architecture:
- Core OpenAPI infrastructure (openapi.go): Dynamic spec building,
  caching, and thread-safe spec generation
- Schema definitions (openapi_schemas.go): Complete type definitions
  for all API request and response types
- Path specifications (openapi_paths.go): Endpoint definitions with
  parameters, request bodies, and response schemas
- Examples (openapi_examples.go): Realistic request/response examples
- Helper functions (openapi_helpers.go): Reusable builders for common
  OpenAPI structures

Testing:
- Comprehensive test suite with golden file validation
- Test helpers package for API testing infrastructure
- OpenAPI compliance validation utilities

The golden file captures the complete specification for snapshot testing.
Update with: go test -run TestOpenAPIGolden -update-openapi-spec

REVIEWERS: The most important thing to check would be the OpenAPI golden
file (web/api/v1/testdata/openapi_golden.yaml). Test scenarios are important
as they test the actual OpenAPI spec validity.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
Add support for both OpenAPI 3.1 and 3.2 specifications with version
selection via openapi_version query parameter. Defaults to 3.1 for
broader compatibility

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
- Add timestampExamples helper for consistent time formatting
- Add exampleMap helper to simplify example creation
- Improve example summaries with query details
- Add matrix result example for range vector queries

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
Implement the AtST() method required by chunkenc.Iterator interface
for FakeSeriesIterator and FakeHistogramSeriesIterator test helpers.
The method returns 0 as these test helpers don't use start timestamps

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
@roidelapluie roidelapluie force-pushed the roidelapluie/openapi branch 4 times, most recently from 1eab834 to 8c46c60 Compare January 29, 2026 11:07
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
@roidelapluie roidelapluie merged commit 75f9490 into prometheus:main Jan 29, 2026
32 checks passed
roidelapluie added a commit to roidelapluie/prometheus that referenced this pull request Jan 29, 2026
Now that prometheus#17825 is merged.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
roidelapluie added a commit to roidelapluie/prometheus that referenced this pull request Jan 29, 2026
Now that prometheus#17825 is merged.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
module github.com/prometheus/prometheus

go 1.24.0
go 1.25.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please be more careful about Go version changes? While it's true today that Go 1.26 is the latest release. At the time of this change we still supported Go 1.24.0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

This was an arterfact of the library used, but the code did build and was tested with the previous go version.

We have CI tests for go 1.24 and they did run, so I was thinking this change was fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait .... #17783 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate Prometheus APIs to OpenAPI/Swagger

5 participants