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

Better logging of api calls from client interface #26

Closed
clux opened this issue May 27, 2019 · 3 comments
Closed

Better logging of api calls from client interface #26

clux opened this issue May 27, 2019 · 3 comments
Labels
client http issues with the client

Comments

@clux
Copy link
Member

clux commented May 27, 2019

Would like to have something akin to kubectl VERB RESOURCE -v=9 where it prints the corresponding curl command with all headers at trace level:

curl -k -v -XGET  -H "Accept: application/json" -H "User-Agent: kubectl/v1.13.3 (linux/amd64) kubernetes/721bfa7" -H "Authorization: Bearer TOKENHERE" 'https://MYCLUSTER/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/foos.clux.dev'
GET https://MYCLUSTER/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/foos.clux.dev 200 OK in 17 milliseconds
Response Headers:
    Date: Mon, 27 May 2019 00:14:51 GMT
    Audit-Id: 8cf2d917-b232-4ec6-9798-090c9c3b051c
    Content-Type: application/json
    Content-Length: 1445
Response Body: {"kind":"CustomResourceDefinition","apiVersion":"apiextensions.k8s.io/v1beta1","metadata":{"name":"foos.clux.dev","selfLink":"/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/foos.clux.dev","uid":"700afb9e-8014-11e9-9779-02a8860543ec","resourceVersion":"75406382","generation":1,"creationTimestamp":"2019-05-27T00:14:47Z","annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"apiextensions.k8s.io/v1beta1\",\"kind\":\"CustomResourceDefinition\",\"metadata\":{\"annotations\":{},\"name\":\"foos.clux.dev\"},\"spec\":{\"group\":\"clux.dev\",\"names\":{\"kind\":\"Foo\",\"listKind\":\"FooList\",\"plural\":\"foos\",\"singular\":\"foo\"},\"scope\":\"Namespaced\",\"version\":\"v1\",\"versions\":[{\"name\":\"v1\",\"served\":true,\"storage\":true}]}}\n"}},"spec":{"group":"clux.dev","version":"v1","names":{"plural":"foos","singular":"foo","kind":"Foo","listKind":"FooList"},"scope":"Namespaced","versions":[{"name":"v1","served":true,"storage":true}],"conversion":{"strategy":"None"}},"status":{"conditions":[{"type":"NamesAccepted","status":"True","lastTransitionTime":"2019-05-27T00:14:47Z","reason":"NoConflicts","message":"no conflicts found"},{"type":"Established","status":"True","lastTransitionTime":null,"reason":"InitialNamesAccepted","message":"the initial names have been accepted"}],"acceptedNames":{"plural":"foos","singular":"foo","kind":"Foo","listKind":"FooList"},"storedVersions":["v1"]}}

We're using client.execute now rather than send, so there SHOULD be more info, but the default headers set in the client module does not seem to be printed.

Canonical Reason is probably also useful here.

@clux clux added enhancement help wanted Not immediately prioritised, please help! and removed help wanted Not immediately prioritised, please help! labels May 27, 2019
@clux clux removed the enhancement label Feb 27, 2020
@kazk kazk added the client http issues with the client label Feb 8, 2021
@kazk
Copy link
Member

kazk commented Feb 8, 2021

This can be done by making LogRequest a Layer that also logs the response.

https://github.com/clux/kube-rs/blob/1bb1c57946b7b1d94010476d5107395aa17a9575/kube/src/service/log.rs#L40-L45

@kazk
Copy link
Member

kazk commented Jun 2, 2021

#540 replaces LogRequest with TraceLayer from tower-http.

A rough equivalent can be implemented with something like this:

TraceLayer::new_for_http()
    .on_request(|req: &Request<Body>, _span: &Span| {
        tracing::trace!("{}", as_curl_command(req))
    })
    .on_response(|res: &Response<Body>, latency: Duration, _span: &Span| {
        tracing::trace!(
            "{} in {}ms\nResponse Headers:\n{}\nResponse Body:\n{}",
            res.status(), // `Display` includes canonical reason. e.g., "200 OK"
            latency.as_millis(),
            format_headers(res.headers()),
            format_body(res.body())
        )
    })

@kazk
Copy link
Member

kazk commented Jun 6, 2021

Closing because we switched to TraceLayer and users can customize it.

@kazk kazk closed this as completed Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client http issues with the client
Projects
None yet
Development

No branches or pull requests

2 participants