Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add APM tracing for client-go requests to the Kubernetes API #5651

Merged
merged 7 commits into from
May 24, 2022

Conversation

pebrc
Copy link
Collaborator

@pebrc pebrc commented May 10, 2022

Follow up to #5649

Will rebase once the first PR is merged. Relevant commits is:

15e7e87

This adds a custom APM round tripper for requests to the Kubernetes API. This can potentially be contributed to elastic/apm-agent-go as a new module. But I think it would be good to try it first in the context of the eck-operator. I structured the code in such a way to allow that if we decide it is worth contributing.

Why a custom round tripper?

To get a meaningful span summary that is specific to k8s. The current implementation is not perfect in this regard especially sub-resource requests are represented somewhat arbitrarily by subsections of the request path.

Screenshot 2022-05-10 at 16 01 22

The other thing implement here is to track requests done in the background by the client-go library to maintain the local cache. As far as I know there is no way to inject a context into the library that would enable tracing. This PR implements instead a default transaction mechanism to track those requests.

Screenshot 2022-05-10 at 16 00 48

@pebrc pebrc changed the title K8s apm phase2 Add APM tracing for client-go requests to the Kubernetes API May 10, 2022
@pebrc pebrc marked this pull request as draft May 10, 2022 14:27
@botelastic botelastic bot added the triage label May 10, 2022
@pebrc pebrc added the >enhancement Enhancement of existing functionality label May 10, 2022
@botelastic botelastic bot removed the triage label May 10, 2022
@pebrc pebrc force-pushed the k8s-apm-phase2 branch 3 times, most recently from 1fc2655 to 12aa428 Compare May 12, 2022 06:54
@pebrc pebrc force-pushed the k8s-apm-phase2 branch from 12aa428 to 6110598 Compare May 17, 2022 12:58
@pebrc pebrc marked this pull request as ready for review May 17, 2022 13:11
Copy link
Contributor

@barkbay barkbay left a comment

Choose a reason for hiding this comment

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

I'm still doing some tests, it works well so far 👍 , just wanted to leave a few comments.

I feel a bit out of depth because I'm not familiar with the go.elastic.co/apm/v2 package, but it's a very interesting PR!

cmd/manager/main.go Outdated Show resolved Hide resolved
pkg/controller/common/tracing/k8s.go Outdated Show resolved Hide resolved
pkg/controller/common/tracing/apmclientgo/client.go Outdated Show resolved Hide resolved
@pebrc pebrc added the v2.3.0 label May 19, 2022
Copy link
Contributor

@barkbay barkbay left a comment

Choose a reason for hiding this comment

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

LGTM

I'm not sure I'm the best person to review the RoundTrip function though, but given it seems inspired from the APM http module I think it is fine (it might be an argument for having it as a new module as you suggested).

Comment on lines 21 to 22
// Allows an optional default transaction to be configured for requests where context cannot be controlled
// e.g. client-go's cache management
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: latinism

Suggested change
// Allows an optional default transaction to be configured for requests where context cannot be controlled
// e.g. client-go's cache management
// Allows an optional default transaction to be configured for requests where context cannot be controlled,
// for example client-go's cache management.

Comment on lines +153 to +164
numSegments := 2
// add a bit more context in the summary for PUT requests e.g. namespace
if req.Method == "PUT" {
numSegments = 3
}

pathSegments := strings.Split(req.URL.Path, "/")
path := strings.Join(pathSegments[len(pathSegments)-numSegments:], "/")
// let's call out watch requests explicitly
if watch := req.URL.Query().Get("watch"); watch == "true" {
statement = "WATCH"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to not just use req.URL.Path?

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought it got very long in the UI so I was looking for something shorter that still conveyed the most relevant information.

@pebrc
Copy link
Collaborator Author

pebrc commented May 24, 2022

Jenkins test this please

/bin/bash: line 1: golangci-lint: command not found

I am confused

@pebrc pebrc merged commit c419d39 into elastic:main May 24, 2022
fantapsody pushed a commit to fantapsody/cloud-on-k8s that referenced this pull request Feb 7, 2023
…#5651)

This adds a custom APM round tripper for requests to the Kubernetes API. This can potentially be contributed to elastic/apm-agent-go as a new module. But I think it would be good to try it first in the context of the eck-operator. I structured the code in such a way to allow that if we decide it is worth contributing.
Why a custom round tripper? To get a meaningful span summary that is specific to k8s. The current implementation is not perfect in this regard especially sub-resource requests are represented somewhat arbitrarily by subsections of the request path. The other thing implement here is to track requests done in the background by the client-go library to maintain the local cache. As far as I know there is no way to inject a context into the library that would enable tracing. This PR implements instead a default transaction mechanism to track those requests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement Enhancement of existing functionality v2.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants