Skip to content

Commit a0be633

Browse files
authored
Merge pull request #10 from mercari/otel-gcp-2
Improve OpenTelemetry tracing
2 parents 9edf9d1 + bd4424d commit a0be633

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

server/handler/fetcher.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424
"github.com/google/go-github/v72/github"
2525
"github.com/palantir/go-githubapp/appconfig"
2626
"github.com/palantir/policy-bot/policy"
27+
"github.com/palantir/policy-bot/tracing"
28+
"go.opentelemetry.io/otel/attribute"
29+
"go.opentelemetry.io/otel/trace"
2730
"gopkg.in/yaml.v2"
2831
)
2932

@@ -41,6 +44,14 @@ type ConfigFetcher struct {
4144
}
4245

4346
func (cf *ConfigFetcher) ConfigForRepositoryBranch(ctx context.Context, client *github.Client, owner, repository, branch string) FetchedConfig {
47+
ctx, span := tracing.Tracer.Start(ctx, "ConfigFetcher.ConfigForRepositoryBranch",
48+
trace.WithAttributes(
49+
attribute.String("owner", owner),
50+
attribute.String("repository", repository),
51+
attribute.String("branch", branch),
52+
))
53+
defer span.End()
54+
4455
retries := 0
4556
delay := 1 * time.Second
4657
for {

server/otel/handler.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 Palantir Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package otel
16+
17+
import (
18+
"context"
19+
20+
"github.com/palantir/go-githubapp/githubapp"
21+
"github.com/palantir/policy-bot/tracing"
22+
"go.opentelemetry.io/otel/attribute"
23+
"go.opentelemetry.io/otel/trace"
24+
)
25+
26+
type traceHandler struct {
27+
handler githubapp.EventHandler
28+
}
29+
30+
var _ githubapp.EventHandler = (*traceHandler)(nil)
31+
32+
func (h *traceHandler) Handles() []string {
33+
return h.handler.Handles()
34+
}
35+
36+
func (h *traceHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
37+
ctx, span := tracing.Tracer.Start(ctx, "githubapp.EventHandler.Handle",
38+
trace.WithAttributes(
39+
attribute.String("event_type", eventType),
40+
attribute.String("delivery_id", deliveryID),
41+
))
42+
defer span.End()
43+
44+
if err := h.handler.Handle(ctx, eventType, deliveryID, payload); err != nil {
45+
span.RecordError(err)
46+
}
47+
return nil
48+
}
49+
50+
func Trace(handler githubapp.EventHandler) githubapp.EventHandler {
51+
return &traceHandler{handler: handler}
52+
}

server/server.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ func New(ctx context.Context, c *Config) (*Server, error) {
104104
registry := metrics.NewPrefixedRegistry("policybot.")
105105
middlewares := append(
106106
[]func(http.Handler) http.Handler{
107-
func(h http.Handler) http.Handler { return otelhttp.NewHandler(h, "policy-bot") },
107+
func(h http.Handler) http.Handler {
108+
return otelhttp.NewHandler(h, "policy-bot", otelhttp.WithFilter(func(r *http.Request) bool {
109+
skip := r.URL.Path == "/" || r.URL.Path == "/api/github/hook" ||
110+
strings.HasPrefix(r.URL.Path, "/static")
111+
return !skip
112+
}))
113+
},
108114
},
109115
baseapp.DefaultMiddleware(logger, registry)...,
110116
)
@@ -218,14 +224,14 @@ func New(ctx context.Context, c *Config) (*Server, error) {
218224

219225
dispatcher := githubapp.NewEventDispatcher(
220226
[]githubapp.EventHandler{
221-
&handler.Installation{Base: basePolicyHandler},
222-
&handler.MergeGroup{Base: basePolicyHandler},
223-
&handler.PullRequest{Base: basePolicyHandler},
224-
&handler.PullRequestReview{Base: basePolicyHandler},
225-
&handler.IssueComment{Base: basePolicyHandler},
226-
&handler.Status{Base: basePolicyHandler},
227-
&handler.CheckRun{Base: basePolicyHandler},
228-
&handler.WorkflowRun{Base: basePolicyHandler},
227+
policybototel.Trace(&handler.Installation{Base: basePolicyHandler}),
228+
policybototel.Trace(&handler.MergeGroup{Base: basePolicyHandler}),
229+
policybototel.Trace(&handler.PullRequest{Base: basePolicyHandler}),
230+
policybototel.Trace(&handler.PullRequestReview{Base: basePolicyHandler}),
231+
policybototel.Trace(&handler.IssueComment{Base: basePolicyHandler}),
232+
policybototel.Trace(&handler.Status{Base: basePolicyHandler}),
233+
policybototel.Trace(&handler.CheckRun{Base: basePolicyHandler}),
234+
policybototel.Trace(&handler.WorkflowRun{Base: basePolicyHandler}),
229235
},
230236
c.Github.App.WebhookSecret,
231237
githubapp.WithErrorCallback(githubapp.MetricsErrorCallback(base.Registry())),

0 commit comments

Comments
 (0)