34 Commits

Author SHA1 Message Date
Michael Munch 12b7290697 distributedtracing: export Prometheus metrics via OTLP (#8450)
* distributedtracing: export Prometheus metrics via OTLP

Add support for pushing OPA's existing Prometheus metrics to an
OpenTelemetry collector via OTLP, eliminating the need for a dedicated
scraper sidecar. Uses the OTel Prometheus bridge to read from OPA's
prometheus.Registry and export through an OTLP metric exporter (gRPC
or HTTP), reusing the same address and TLS configuration as traces.

New config fields: distributed_tracing.metrics (bool, default false)
and distributed_tracing.metrics_export_interval_ms (int, default 60000).

Fixes #7591

Signed-off-by: Michael Munch <mm.munk@gmail.com>

* metricsexport: decouple metrics export into top-level config section

Extract metrics export from distributed_tracing into its own
metrics_export config section with independent type (otlp/grpc,
otlp/http), address, and TLS settings. This allows exporting
Prometheus metrics via OTLP without enabling tracing, and to a
different endpoint than traces.

- Extract shared TLS helpers into internal/tlsutil
- Add MetricsExport field to top-level Config
- Create internal/metricsexport package with Init, config parsing
- Remove metrics fields from distributedtracing
- Update runtime to call metricsexport.Init separately
- Move e2e tests to v1/test/e2e/metricsexport
- Add Metrics Export section to configuration docs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Michael Munch <mm.munk@gmail.com>

* ci: retrigger checks

Signed-off-by: Michael Munch <mm.munk@gmail.com>

* go.mod: upgrade dependencies downgraded during rebase

Modules like containerd, go-sqlbuilder, OpenTelemetry, and golang.org/x/*
were at older versions than main after a rebase. Upgrade them to match or
exceed main.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Michael Munch <mm.munk@gmail.com>

* Update internal/distributedtracing/distributedtracing_test.go

Signed-off-by: Michael Munch <mm.munk@gmail.com>

---------

Signed-off-by: Michael Munch <mm.munk@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 14:09:51 +00:00
Stephan Renatus 24611c0262 build: bump go 1.26.1 (#8409)
https://groups.google.com/g/golang-announce/c/EdhZqrQ98hk

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2026-03-06 13:32:31 -06:00
Stephan Renatus c256ef1f39 build: bump golang 1.25.7 -> 1.26.0
And raise `go` version in go.mod. 1.24.x is no longer supported.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2026-02-12 08:42:05 +01:00
Stephan Renatus a938b9202e build: bump go 1.25.5 -> 1.25.6
https://groups.google.com/g/golang-announce/c/Vd2tYVM8eUc

Unsure about the TLS issues. Let's just update to err on the safe side.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2026-01-16 09:12:14 +01:00
Stephan Renatus 40ec031d5c build: bump golang for build: 1.25.1 -> 1.25.2
https://groups.google.com/g/golang-announce/c/4Emdl2iQ_bI

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2025-10-13 17:22:04 +02:00
Stephan Renatus cef6c430b1 build+ci: fix go versions
- build with 1.25.1 (.go-version)
- go-compat test run with 1.24.*
- adjust prometheus metrics test

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2025-09-22 13:44:51 +02:00
Johan Fylling 5ac310470e build: Bump go to 1.24.6 (1.23.12) (#7834)
Build version: from 1.24.4 to 1.24.6
SDK version: from 1.23.8 to 1.23.12

See: https://pkg.go.dev/vuln/GO-2025-3849

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2025-08-12 18:36:54 +02:00
Stephan Renatus 3b5545ba1d server: ensure that wrapped middlewares all support http.Flusher
Signed-off-by: Stephan Renatus <stephan@styra.com>
2025-07-15 18:09:16 +02:00
Anders Eknert b1ce92c459 perf: more efficient data/v1 POST handler (#7673)
Since we don't use the HTTP API in Regal, I hadn't looked at this from
a performance POV before, and it was a fun side quest :)

A pretty decent reduction of the baseline cost for the most common
request type, v1/data POST. Changes:

- Add NoOp implementation of `Metrics` for when metrics aren't needed
- Cheaper `metrics.New` instantiation avoiding unnecessary lock
- Avoid cost of decision logging if decision logging isn't enabled
- Only call request.URL.Query() if URL.RawQuery isn't empty, avoiding
  allocating an empty map with each request

While the target was v1/data POST handling, some of the changes above
have a positive impact on all or most handlers. All changes have been
run through the existing tests, and a new benchmark to measure the impact
of the fixes have been added, showing:

```
13063 ns/op	   15162 B/op	     195 allocs/op - main
12796 ns/op	   14856 B/op	     189 allocs/op - avoid r.URL.Query() when no query provided
12541 ns/op	   14483 B/op	     187 allocs/op - decisionLogger.Log early exit if not enabled
12133 ns/op	   14235 B/op	     180 allocs/op - get revisions and init logger only if needed
11098 ns/op	   14207 B/op	     180 allocs/op - more efficient metrics.New() (without locking)
10683 ns/op	   13171 B/op	     169 allocs/op - use no-op metrics implementation when metrics aren't requested
```

Even if the impact is pretty good, it's worth noting that most of the improvements
above are only seen when decision logging is turned off. While this is the common case
for development, it's not in production. Getting the baseline cost down is important still
as there should be no cost paid for features unused.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-06-09 12:33:48 +02:00
Sebastian Spaink a55e1b54fa deps: use google.golang.org/protobuf (#7655)
Signed-off-by: sspaink <sspaink@styra.com>
2025-06-03 09:16:02 -05:00
Anders Eknert e43ef0a979 Use any in place of interface{} (#7566)
Earlier this evening I tried to run the Go
[modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize)
analyzer on OPA. That didn't go as planned:

- https://github.com/golang/go/issues/73661
- https://github.com/golang/go/issues/73663

While we wait for that to be fixed, I figured an old-fashioned
search-and-replace across the repo may work for at least the
`interface{}` to `any` conversion. That should help make it easier
to see the other fixes as applied by the modernize tool once it has
had those issues resolved.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-05-12 13:57:48 +02:00
Anders Eknert bd5ceb5142 Enable unused-receiver linter (revive) (#7448)
Signed-off-by: Anders Eknert <anders@styra.com>
2025-03-14 11:41:25 +01:00
Anders Eknert 684ef8da7a Fix test failures with Go 1.24 (#7376)
CI build will have to determine whether they also work in Go 1.23 :)

Signed-off-by: Anders Eknert <anders@styra.com>
2025-02-20 18:34:51 +01:00
Anders Eknert 55e87e79ae Add perfsprint linter (#7334)
And update code to conform to the rule.

- Replace unnecessary fmt.Sprintf with string concatenation
- Replace fmt.Sprint with more efficient strconv.Itoa
- Replace static fmt.Errorf calls with more efficient errors.New

Thanks @srenatus for pushing me down this rabbit hole!

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-31 20:24:05 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00
Stephan Renatus 1554ce286e build: bump go used for build to 1.23.1 (#7006)
Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-09-09 15:26:03 +02:00
Ashutosh Narkar 9964aee637 build(deps): bump github.com/prometheus/client_golang from 1.19.1 to 1.20.0
In addition to the vendor update, a test case was updated to account
for the new help text of the `go_gc_duration_seconds` metric. Also
`go_memstat_lookups_total` metric was removed in 1.20.0.

Release Notes: https://github.com/prometheus/client_golang/releases/tag/v1.20.0

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2024-08-19 09:05:59 -07:00
Stephan Renatus b9302f3998 build(go): bump 1.22.4 -> 1.22.5 (#6845)
https://groups.google.com/g/golang-announce/c/gyb7aM1C9H4

Also adds a new metric to our Prometheus tests.

Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-07-04 10:48:20 +02:00
Stephan Renatus 963f5eaada build(go): bump golang, build with 1.22, support 1.20+ (#6595)
https://go.dev/doc/go1.22

Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-02-20 12:46:17 +01:00
aarnautu 38c997eef4 This change adds support to configurable prometheus buckets
http_request_duration has fixed, hardcoded number of buckets with no possibility to tweak them
For cases when the most of the latencies are above 1ms, with only 4 available buckets there's no good insight on OPA's performance.

This implementation:
- adds the possibility for the buckets to be configurable in ```server.metrics.prom.http_request_duration_seconds.buckets``` key
- it's not a breaking change, if the buckets are not present in the configuration, the metric is configured with the existing values as a fallback

Signed-off-by: aarnautu <aarnautu@adobe.com>
2023-10-06 13:27:46 -07:00
Ashutosh Narkar cdf2d35a25 golang: Update golang to 1.21.1
This version includes security fixes which address the following CVEs:

* CVE-2023-39318
* CVE-2023-39319
* CVE-2023-39320
* CVE-2023-39321
* CVE-2023-39322

Also changes to internal/prometheus/TestJSONSerialization: add new metric

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-09-08 11:31:29 -07:00
Stephan Renatus f8e1e4bf2d golang: 1.20.7 -> 1.21 (#6189)
https://tip.golang.org/doc/go1.21

This required some test updates:

* topdown/tokens_test: adjust for go1.21

   What was correct for 1.20 is correct for 1.21, so I've flipped the exception logic.

* plugins/rest/TestClientCert: adapt cert-related error string

* internal/prometheus/TestJSONSerialization: add new metrics

   There are new metrics!

Signed-off-by: Stephan Renatus <stephan@styra.com>
2023-08-29 15:26:09 +02:00
Anders Eknert 74ac69cb65 Add metrics/alloc for showing memory allocated (#5715)
Signed-off-by: Anders Eknert <anders@styra.com>
2023-03-02 12:51:48 +01:00
Stephan Renatus c9ec05d3fe bump: go 1.19.5 -> 1.20.1
This PR bumps go to 1.20.1 (https://go.dev/doc/go1.20) which
addresses the following vulnerabilities:
https://pkg.go.dev/vuln/GO-2023-1571
https://pkg.go.dev/vuln/GO-2023-1570
https://pkg.go.dev/vuln/GO-2023-1568

As part of the migration, general Golang
stdlib deprecations and test failures were addressed as well.
Some of those changes are:

* Bump golangci-lint for support with go1.20
* Migrate rand.Seed() calls to the newer rand.New(rand.NewSource(seed))

Co-authored-by: Stephan Renatus <stephan@styra.com>
Co-authored-by: Philip Conrad <philipaconrad@gmail.com>

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-02-21 13:51:23 -08:00
Anders Eknert 95708108f3 linters: add unconvert (#5318)
Got a few warnings from my IDE about redundant type conversions,
so I decided to look into it. Added the unconvert linter to our
checks, and fixed the violations. Added two ignore comments as I
wasn't sure about whether they'd change the semantics of the code.

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-10-27 13:35:39 +02:00
Stephan Renatus 27274e08b6 build: use go 1.19, drop go 1.16 (#5013)
With this, we'll build our container images and binaries using golang 1.19.

Also, the go.mod version stanza is increased, letting us use go1.17+ features.

I had to run

    go mod tidy -go=1.16 && go mod tidy -go=1.17

to get rid of `go mod tidy` related messages, and ran `go mod vendor`
afterwards.

* prometheus: adjust tests for new go1.19 metrics

Note that the new metrics only appear when using the Go runtime of 1.19. So,
we do the same we've done before when 1.17 brought in new metrics: add them
to the tests, and use build flags to not run the tests in the previous versions.

When the bump of github.com/prometheus/go_client to 1.13.0 was merged, it was
properly tested with all of 1.17 and 1.18. So, the previously expected metrics
should be there when using OPA from 1.17 or 1.18.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-08-15 18:05:55 +02:00
Stephan Renatus 998adfee79 prometheus: bump client_golang, adjust setup and tests (#4755)
* build(deps): bump github.com/prometheus/client_golang to v1.12.2

Fixes #4697.

* internal/prometheus: adapt to client_golang changes in v1.12.2

- three histograms lost their erroneous "_total" suffix
- one gc cpu fractions metrics is gone

Also, the tests now actually run on Go1.17+, I had misunderstood a build tag earlier.

We're using a deprecated option to keep all the metrics we had before.
In the future, it probably makes sense to use the advised collection only.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-06-09 12:12:17 +02:00
Stephan Renatus d2914c0d54 build: bump golang: 1.17 -> 1.18
No change to go.mod's `go` stanza, so no changes in code compatibility.

However, it's used for building our docker images and release
binaries, and for fuzz testing in our nightly workflow.

Some test-related changes with the dns lookup built-in function's
error handling; and the hardcoded signature. Running

    go test ./topdown -run TestTopdownJWTEncodeSignECWithSeedReturnsSameSignature -count 10000

makes me believe that for whatever reason the signature changed,
it's at least stable.

topdown/http_test: Test-only change to accomodate this change in Go (https://go.dev/doc/go1.18):

    Certificate.Verify now uses platform APIs to verify certificate
    validity on macOS and iOS when it is called with a nil
    VerifyOpts.Roots or when using the root pool returned from
    SystemCertPool.

We're keeping the old message for go <= 1.17; in a silly-simple way.

Also:

* ci: build and test two old golang version on macos|linux

  We'll drop golang 1.15, keep one unsupported version (1.16).

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-03-28 07:24:21 +02:00
Stephan Renatus 9afdad7919 Status API: use jsonpb for json marshalling of prometheus metrics (#4324)
* Wrap the prometheus portion of our metrics in such a way that they use jsonpb for
   encoding to JSON, as prescribed by the protobuf library.

   Note: We're using jsonpb, not protojson, because there is no protobuf V2 version of
   github.com/prometheus/client_golang

* build(deps): bump github.com/prometheus/client_golang (#4307)

 This reverts commit 2f298db68c.

* CHANGELOG.md: add note re: JSON encoding of Status API payloads

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-02-22 09:25:19 +01:00
Rafael Otero Reinert 8569551dd8 status: publish metrics via prometheus endpoint (#4251)
To improve plugin and bundle monitoring, new metrics related to bundle-activation
are exported via the prometheus endpoint of the OPA service.

Signed-off-by: rafael otero reinert <rafaelreinert@gmail.com>
2022-02-08 14:21:33 +01:00
Stephan Renatus 4b6d1b3e71 internal/prometheus: fix deprecated call
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-07-06 15:07:57 -07:00
Luong Vo f6c30e76ca Internal/prometheus: Add smaller buckets
This pull request ditches some higher granularity buckets in favour of adding a
few smaller ones. The bucket that I chose was based on https://www.openpolicyagent.org/docs/latest/policy-performance/#high-performance-policy-decisions
where the expectation is "policy evaluation has a budget on the order of 1 millisecond".
Also, I tried to stay within Prometheus's default 10 buckets.

This fixes #3196

Signed-off-by: Luong Vo <vo.tran.thanh.luong@gmail.com>
2021-03-05 11:16:34 -05:00
Patrick East b48c534722 Run make fmt with new goimports cmd
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-27 09:55:11 -04:00
Torin Sandall b0523466c1 Refactor metric provider implementation
* Remove metric provider config to avoid introducing new public
  interfaces. Since there is only one provider (prometheus) and it
  doesn't have any configurable settings, remove the configuration
  changes for now. We can always add these in the future.

* Remove dummy metric provider implementation. This isn't needed now
  that we're using the metrics.Metrics interface instead of
  metrics.GlobalMetrics.

* Remove metrics.GlobalMetrics in favour of metrics.Metrics. Move the
  HTTP handler instrumentation interfaces into the server package to
  avoid coupling the metrics package to the net/http package.

* Refactor the prometheus provider to implement the metrics.Metrics
  interface. Since the prometheus registry can error on Gather()
  calls, the provider has been updated to accept a logger and use ti
  when the Gather() call fails. This doesn't affect any public
  interfaces so it can be revisited in future if needed. Alteratnively
  we could add a Gather() interface onto metrics.Metrics which could
  return the error.

* Refactor status plugin to include metrics in status update by
  default. Users implementing the status API are likely to need
  performance metrics to gauge the OPA's health. Moreover if they are
  implementing the status API it's unlikely they will want to poll the
  /metrics endpoint on the OPA HTTP API (which may not even be
  exposed.)

* Move the prometheus endpoint test case into the e2e package so the
  server package has no dependencies on prometheus anymore.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-08-15 15:08:38 -04:00