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

Output the http REST communication to the log #2526

Closed
jonathanvila opened this issue Oct 2, 2020 · 13 comments · Fixed by #3125
Closed

Output the http REST communication to the log #2526

jonathanvila opened this issue Oct 2, 2020 · 13 comments · Fixed by #3125

Comments

@jonathanvila
Copy link

In order to see messages sent and received from/to the K8s client and debug possible issues, having all the http requests to K8s API logged it's interesting. Having the endpoint, and payload sent/received and http headers.

@manusa
Copy link
Member

manusa commented Oct 8, 2020

We'll also check if OkHTTP logging can be configured to print those messages.

@rohanKanojia
Copy link
Member

rohanKanojia commented Oct 12, 2020

I checked code and looks like we're already using okhttp logging-interceptor for this:

Logger reqLogger = LoggerFactory.getLogger(HttpLoggingInterceptor.class);
if (reqLogger.isTraceEnabled()) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClientBuilder.addNetworkInterceptor(loggingInterceptor);
}

If I set logging level to trace in my simplelogger.properties file:

org.slf4j.simpleLogger.defaultLogLevel=trace

If I execute some code like this:

try (OpenShiftClient client = new DefaultOpenShiftClient()) {
    SelfSubjectAccessReview ssar = new SelfSubjectAccessReviewBuilder()
            .withNewSpec()
            .withNewResourceAttributes()
            .withGroup("apps")
            .withResource("deployments")
            .withVerb("create")
            .withNamespace("rokumar")
            .endResourceAttributes()
            .endSpec()
            .build();

    ssar = client.authorization().v1().selfSubjectAccessReview().create(ssar);
}

It produces the following log:


[main] DEBUG io.fabric8.kubernetes.client.Config - Trying to configure client from Kubernetes config...
[main] DEBUG io.fabric8.kubernetes.client.Config - Found for Kubernetes config at: [/home/rohaan/.kube/config].
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://api.example.openshift.com/apis/authorization.k8s.io/v1/selfsubjectaccessreviews http/1.1
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/json; charset=utf-8
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: Content-Length: 183
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: Host: api.example.openshift.com
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: Connection: Keep-Alive
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: Accept-Encoding: gzip
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: User-Agent: okhttp/3.12.12
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: 
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: {"apiVersion":"authorization.k8s.io/v1","kind":"SelfSubjectAccessReview","spec":{"resourceAttributes":{"group":"apps","namespace":"rokumar","resource":"deployments","verb":"create"}}}
Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: --> END POST (183-byte body)
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: <-- 201 Created https://api.example.openshift.com/apis/authorization.k8s.io/v1/selfsubjectaccessreviews (334ms)
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: Audit-Id: 8529c650-caec-423b-a3e6-3200af80f7c4
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: Cache-Control: no-store
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/json
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: Date: Mon, 12 Oct 2020 06:47:06 GMT
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: Content-Length: 283
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: 
Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: {"kind":"SelfSubjectAccessReview","apiVersion":"authorization.k8s.io/v1","metadata":{"creationTimestamp":null},"spec":{"resourceAttributes":{"namespace":"rokumar","verb":"create","group":"apps","resource":"deployments"}},"status":{"allowed":false,"reason":"no RBAC policy matched"}}

Oct 12, 2020 12:17:07 PM okhttp3.internal.platform.Platform log
INFO: <-- END HTTP (283-byte body)

Process finished with exit code 0

I think all we need to do is to document this :-) . Maybe we can add a section in our CHEATSHEET about it?

@rohanKanojia
Copy link
Member

@jonathanvila : Hi, Would you like to contribute a PR for this? I think it would be awesome if you could submit a Doc improvement PR to address this issue?

@stale
Copy link

stale bot commented Apr 8, 2021

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@stale stale bot added the status/stale label Apr 8, 2021
@stale stale bot closed this as completed May 6, 2021
@manusa manusa reopened this May 6, 2021
@MUzairS15
Copy link
Contributor

Hey, can you pls help me in this issue as following through previous conversations we need to just document this feature in cheatsheet but exactly where?
Pls bare with me as i am new to this

@rohanKanojia
Copy link
Member

@MUzairS15 : You can create a Logging section there and add instructions on how to configure logging.

@MUzairS15
Copy link
Contributor

Ok

@MUzairS15
Copy link
Contributor

One check is failing but it is not required , also if there is any other change needed.

@MUzairS15
Copy link
Contributor

@rohanKanojia help??

@rohanKanojia
Copy link
Member

I've restarted the flaky test failure. I've also posted a comment on your PR

manusa pushed a commit that referenced this issue May 24, 2021
…tsheet (#3125)

* Fixes #2526

Updated CHEATSHEET.md
Added example for how to configure okhttp logging-interceptor.

* Updated PR

* Updated changes

* Added in table of contents

* Update CHEATSHEET.md
manusa pushed a commit that referenced this issue Jun 7, 2021
…cedOperation in RbacAPIGroupClient

* Fixes #2526

Updated CHEATSHEET.md
Added example for how to configure okhttp logging-interceptor.

* Updated PR

* Updated changes

* Changed clusterRoleBindings and clusterRoles to return NonNamespace Operation instead of Mixed operation

* Changed clusterRoleBindings and clusterRoles to return NonNamespace Operation instead of Mixed operation
@cstaykov
Copy link

cstaykov commented Aug 5, 2021

How come the logs using okhttp logging-interceptor do not follow the same logging configuration as logs from the kubernetes-client itself?

For example, In this comment #2526 (comment), the first two logs follow the intended pattern of:

[main] DEBUG io.fabric8.kubernetes.client.Config - Trying to configure client from Kubernetes config...
[main] DEBUG io.fabric8.kubernetes.client.Config - Found for Kubernetes config at: [/home/rohaan/.kube/config].

But then all the okhttp logs don't follow [main] DEBUG <classname> - <log> but instead have their own format:

Oct 12, 2020 12:17:06 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://api.example.openshift.com/apis/authorization.k8s.io/v1/selfsubjectaccessreviews http/1.1

How can I change the format of the okhttp logs?

@rohanKanojia
Copy link
Member

I'm not sure how to change the format for okhttp logs. Do you think there is some configuration option in HttpLoggingInterceptor[0] to tweak network log output?

[0] https://square.github.io/okhttp/4.x/okhttp-logging-interceptor/okhttp3.logging/-http-logging-interceptor/

@zem00n
Copy link

zem00n commented Nov 15, 2023

As documentation is not updated atm, for other people - I am using version 6.8.1 and the next change in logging shows Rest communication
<logger name="io.fabric8.kubernetes.client.http" level="TRACE" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants