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

Multiple X-Clockwork-Id header #248

Closed
L3o-pold opened this issue Mar 16, 2018 · 26 comments
Closed

Multiple X-Clockwork-Id header #248

L3o-pold opened this issue Mar 16, 2018 · 26 comments

Comments

@L3o-pold
Copy link

Our backend makes multiple call in one request to our API.

For example for a /user backend call, our backend will call 2 API route, one for the user languages and an other one for the user preferences.

So we have a list of X-Clockwork-id unique header, but it's not handled by the Chrome extension.

Is it planned to add this feature?

@itsgoingd
Copy link
Owner

Hey,

Can you describe your use case in a bit more detail? Is your api calling another APIs and appending the returned Clockwork headers to its own response or is the API calling itself? Any reason why don't you simply call the other controllers directly or extract the shared code, so you won't need to make additional HTTP requests?

I'll definitively consider adding support if it makes sense, it's not hard to do.

@L3o-pold
Copy link
Author

Hi,
Our API is calling another API and appending the returned Clockwork headers to its own response.
The frontend makes one call to our API which makes for example 3 differents call to the other API and merge the returned Clockwork headers.

We'd like to track this 3 calls as independent requestsl.

@L3o-pold
Copy link
Author

In this case, we are doing asynchronous multiple requests in PHP. I think it could be useful and will not break existant implementation. It's just a new feature allowing multiple Clockwork headers to be tracked.

@itsgoingd
Copy link
Owner

I like the idea, will take a look today.

@L3o-pold
Copy link
Author

Great, two other ideas, I don't know if it's hard to do or not:

  • Replay request like replay XHR in Network tab
  • Show the request response like in Network tab

@itsgoingd
Copy link
Owner

I wanted to add an ability to reveal selected request in the network panel, so we don't have to reimplement the functionality, but there is no Chrome api that would allow us to do so. I've put in a feature request, but there was not much interest in it - https://bugs.chromium.org/p/chromium/issues/detail?id=614686.

We might need to reimplement the response/preview functionality from network tab at some point. For the request replaying I will need to do some research, might be too complex for the minor convenience compared to full page reload.

For the sub-requests I would suggest logging the responses in your "main" request which would make them available in the "main" response log tab (should work pretty well when working with JSON).

@L3o-pold
Copy link
Author

L3o-pold commented Mar 19, 2018

For the sub-requests I would suggest logging the responses in your "main" request which would make them available in the "main" response log tab (should work pretty well when working with JSON).

You mean for multiple requests?

@itsgoingd
Copy link
Owner

Looking into this, it's not as simple as sending multiple ids. Since the other apis live (or might live) on different urls, we need to know these so we can request the metadata.

Eg. if the sub-request is made to api2.example.com and returns id 123 we need to request metadata from api2.example.com/__clockwork/123.

This is what I'm thinking atm - we'll add a new api Clockwork::subrequest($url, $id), this will do two things:

  • write a new subrequests key in the Clockwork metadata
  • add a X-Clockwork-Subrequest: $id;$url header

This way we can make the metadata request to the appropriate url from the extension.

@L3o-pold
Copy link
Author

Actually, we use only one endpoint to request metadata because we do not wish to track api1.example.com but only api2.example.com. Our api1 works like a Proxy so it's not necessary to track performance or anything.

But you right that if you need to track multiple project in one query Clockwork::subrequest is useful.

@itsgoingd
Copy link
Owner

Let me know if this works for you - itsgoingd/clockwork-chrome#46

@L3o-pold
Copy link
Author

I'm not sure it could works because:

  • We do not use clockwork on API 1, we only forward headers from API 2 so we do not have the clock()->subrequest($url, $id); methods.
  • I have no parent requests I think
  • How can I add mutliple sub-request ? In my example the API 1 is calling 3 times the API 2 so I could not use only one X-Clockwork-Subrequest header
  • Is the URL required in this header? I've tried previously to call a fake URL like this /fake which return Clockwork-id header from an other request (/test) for example and in the clockwork Chrome extension I see /test and not the /fake url so it seems not useful.

Am I missing something?

@itsgoingd
Copy link
Owner

This is designed for a case where the API 1 would also use Clockwork, but can still be used in your setup by manually appending the headers:

  • X-Clockwork-Id header has to be present (this is how we detect if the app uses Clockwork), you can either choose one of your forwarded ids to be your "main id" (it will be shown as "parent" request in the extension) or simply send some random id (it will be shown in the requests list but no details will be available since we have no metadata)
  • add X-Clockwork-Subrequest header for each of the rest of forwarded ids in the from of $id;$url, where $id is the id itself and $url is the url containing the host were we should be looking for metadata (this doesn't need to be full request url, eg. for request to http://api.example.com/users/create we need only http://api.example.com/ so we can read metadata from http://api.example.com/__clockwork/$id)

Eg. in your case where you have api1.example.com calling api2.example.com three times returning ids 111, 222, 333 and the metadata is available at http://api1.example.com/__clockwork/$id:

X-Clockwork-Id: 111
X-Clockwork-Subrequest: 222;http://api1.example.com
X-Clockwork-Subrequest: 333;http://api1.example.com

or

X-Clockwork-Id: 0
X-Clockwork-Subrequest: 111;http://api1.example.com
X-Clockwork-Subrequest: 222;http://api1.example.com
X-Clockwork-Subrequest: 333;http://api1.example.com

@L3o-pold
Copy link
Author

Oh ok, the url is actually the path.
I will test it again tomorrow, I will keep you updated.

Thank you

@L3o-pold
Copy link
Author

L3o-pold commented Mar 20, 2018

I think there is an issue with the url.
My headers are:

X-Clockwork-Id: 0

X-Clockwork-Path: /myfolder/__clockwork/

X-Clockwork-Subrequest: 1521534919-4878-1919494053;http://apiv1.domain.com/myfolder/, 1521534919-5203-1481206257;http://apiv1.domain.com/myfolder/, 1521534919-6458-405847040;http://apiv1.domain.com/myfolder/

X-Clockwork-Version: 2.2.3

The extension tries to query http://apiv1.domain.com/__clockwork/1521534920-6453-1849188266
It miss the myfolder to the url.

@itsgoingd
Copy link
Owner

Path is currently not supported for subrequests, we can change the subrequest header format to $id;$url;$path, where $path would be optional and the same format as X-Clockwork-Path header. I will take a look later today.

@L3o-pold
Copy link
Author

An other problem is that your are trying to map X-Clockwork-Subrequest headers but there is only one with multiple value separated with , character.
I'm currently trying to fix it, if I got something, I will send you a commit.

@L3o-pold
Copy link
Author

See itsgoingd/clockwork-chrome#47

@L3o-pold
Copy link
Author

So with this modification, it works great.
To be more complet, we should add Path to subrequest to allow the user to query multiple /__clockwork endpoint.

@itsgoingd
Copy link
Owner

Hey, I've updated the PRs with path and comma-separated header values support, I had to use different code since both comma-separated and multiple headers are valid.

@L3o-pold
Copy link
Author

Ok great. I do not see how we could have multiple headers, in every try I did, it's always comma-separated but it's cool if both are handle.
I will try tomorrow thank you.

@L3o-pold
Copy link
Author

It works great but you need to add a trim on the header value line 141 in the extension.js file:

value = value.trim()
return { id: value.split(';')[0], url: value.split(';')[1], path: value.split(';')[2] }

I have to urlencode the url param in subrequest header because one of our url used comma. I don't know if you are doing something about that in the subrequest method because we are building headers ourself but I prefere to notice you.

Great job by the way ;)

@L3o-pold
Copy link
Author

I confirm you that in our case we need to use
clock()->subrequest(urlencode($sUri), because some of our url use comma.

@itsgoingd
Copy link
Owner

Thanks for testing & feedback, I'll update the PRs later today.

@L3o-pold
Copy link
Author

Do you have a planned date for the merge into master?

@itsgoingd
Copy link
Owner

Hey, this will most likely be å part of the 3.0 release still a few weeks away (#232).

@itsgoingd
Copy link
Owner

Subrequests now available in Clockwork 3.0 - blog post and full changelog.

Still need to document this, but api is the same as in the PR.

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

No branches or pull requests

2 participants