Skip to content

feat: add OpenTelemetry metrics middleware alongside go-metrics - #531

Open
adityakrmishra wants to merge 1 commit into
palantir:developfrom
adityakrmishra:feature/opentelemetry-metrics
Open

feat: add OpenTelemetry metrics middleware alongside go-metrics#531
adityakrmishra wants to merge 1 commit into
palantir:developfrom
adityakrmishra:feature/opentelemetry-metrics

Conversation

@adityakrmishra

Copy link
Copy Markdown

Addresses #439

Hi team,

Following up on the discussion in #439, this PR introduces OpenTelemetry as an alternative metrics layer. It runs entirely parallel to the existing rcrowley/go-metrics implementation to ensure zero disruption to internal Palantir systems that rely on the legacy metrics.

What this solves:
The legacy go-metrics lacks label/tag support, forcing high-cardinality data like installation_id into the metric names. This new OTel middleware maps the exact same data points but uses proper OTel attributes (e.g., github.rate.remaining{installation.id="42"}).

Changes:

  • Added go.opentelemetry.io/otel/metric to go.mod (API only, keeping it lightweight without SDK bloat).
  • Added githubapp/middleware_otel.go containing the parallel implementation.
  • Zero modifications to existing files (middleware.go, dispatcher.go, scheduler.go).

New Entry Points:
Users can now opt-in via:

  • OTelClientMetrics(mp)
  • OTelErrorCallback(mp)
  • OTelAsyncErrorCallback(mp)
  • WrapSchedulerWithOTel(inner, mp)

All existing tests pass, and the code has been written to match the idiomatic style and comment density of the current project. Let me know if any adjustments are needed!

@palantirtech

Copy link
Copy Markdown
Member

Thanks for your interest in palantir/go-githubapp, @adityakrmishra! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@changelog-app

changelog-app Bot commented Apr 16, 2026

Copy link
Copy Markdown

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

feat: add OpenTelemetry metrics middleware alongside go-metrics

Check the box to generate changelog(s)

  • Generate changelog entry

@adityakrmishra
adityakrmishra force-pushed the feature/opentelemetry-metrics branch from 4001868 to c7aceb2 Compare July 23, 2026 04:20
@@ -0,0 +1,3 @@
---
type: Feature
description: "feat: add OpenTelemetry metrics middleware alongside go-metrics"

@asvoboda asvoboda Jul 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove this file please, its not necessary

@adityakrmishra
adityakrmishra force-pushed the feature/opentelemetry-metrics branch from c7aceb2 to 22ec341 Compare July 23, 2026 11:27

@jamestoyer jamestoyer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have some minor comments but overall this seems like a nice addition. Structurally, I think I'd like to see them in a separate directory for now so it's clear it's not part of the regular API and may be changed at a later point. That may happen as we look in to whether we should adopt OTel more widely and have opinions on how it should laid out.

Comment thread githubapp/middleware_otel.go Outdated
Comment on lines +45 to +47
OTelMetricsKeyHandlerError = "github.handler.errors"
OTelMetricsKeyDroppedEvents = "github.event.dropped"
OTelMetricsKeyEventAge = "github.event.age"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These seem like new metrics. What is their purpose here? I think I'd want to start with parity with the existing metrics and then introduce anything new in a subsequent PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. Removed these entirely to maintain strict 1:1 parity with the existing metrics. We can evaluate adding new telemetry in a future, separate PR.

Comment thread githubapp/middleware_otel.go Outdated

return func(next http.RoundTripper) http.RoundTripper {
return roundTripperFunc(func(r *http.Request) (*http.Response, error) {
installationID, _ := r.Context().Value(installationKey).(int64)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we guard against it not existing and explicitly set it to 0 so it's the same as

installationID, ok := r.Context().Value(installationKey).(int64)
if !ok {
installationID = 0
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added the explicit guard and fallback to 0 to exactly match the behavior in middleware.go.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you make the changes in this file in a separate PR rather than conflating it with the OTel work?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Absolutely. I've force-pushed this branch to remove those changes, and opened them in a clean, dedicated PR here: #593. Apologies for the git mix-up!

@adityakrmishra
adityakrmishra force-pushed the feature/opentelemetry-metrics branch from a8a50fe to 22ec341 Compare July 23, 2026 13:51
@adityakrmishra
adityakrmishra force-pushed the feature/opentelemetry-metrics branch from 22ec341 to ac741f4 Compare July 23, 2026 14:24
@adityakrmishra

Copy link
Copy Markdown
Author

Thanks for the great feedback, @jamestoyer! I've updated the PR to address all your points:

  1. Directory Isolation: Moved the implementation into a new githubapp/otelgithubapp subpackage to clearly mark it as experimental.
  2. Strict Metric Parity: Removed the new metrics (github.handler.errors, github.event.dropped, etc.) to ensure we are starting with strict 1:1 parity with the legacy go-metrics implementation.
  3. Guard Check: Fixed the installationID extraction to safely fall back to 0 if missing, exactly matching the legacy middleware.

(Regarding the caching_client_creator.go changes that briefly leaked into this branch: Apologies for the git mix-up! I have force-pushed this branch to remove them, and opened a dedicated PR for that specific cache-stampede fix here: #593).

Let me know if there's anything else you'd like adjusted!

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.

4 participants