google / google/go-github

Feature Request: configurable telemetry

Open
#3,658 5 comments 2 reactions 0 assignees View on GitHub
waiting for reply
Dominant language
Go
Stars
11.3k
Forks
2.5k
Avg merge
1d 22h
Merged PRs (30d)
49

Description

In a web service which may be making tens of thousands of call to Github, it's desirable to have instrumentation to identify hot code paths, bad tenants, or poor optimizations resulting in extra API calls.

Currently there are three ways to gain metrics on the API requests:
1. Wrapping every API call with a wrapper that emits the telemetry data
Ugly, a little verbose, easy for new code to accidentally forget to wrap the API call or manually emit the telemetry data, isn't accurate when paging
2. Wrapping API client access with a wrapper which emits the telemetry data every time get the client
Pretty clean, very easy to subvert by saving the returned client in a variable, isn't accurate when paging
3. Wrapping the entire struct and every necessary struct method
Very safe from accidentally using the client without metrics, boilerplate hell, isn't accurate when paging

It would be very helpful for the library to provide a mechanism to instrument the API usage with some form of telemetry. Any kind of pluggable middleware, callbacks, or hook would add a lot of utility. If there is a way to do this which is both hard to accidentally workaround and doesn't require a lot of boilerplate, that would be awesome to know about and document!

One very simple or rudimentary option would be to add an optional field `Client.OnDo: func()` (also settable via `Client.WithCallback()` or something) which, if set, would be called during `Client.bareDo` just before `caller.Do()`. A example usage might be:
```go
func metricEmitter(org: string, repo: string) func() {
return func() {
metrics.emit("api-request", tags: map[string]string{"organization": org, "repository": repo})
}

...
prs, _, err := client
.WithCallback(metricEmitter(request.Args.Owner, request.Args.Repository))
.PullRequests
.List(request.Args.Owner, request.Args.Repository)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.