Skip to content
This repository has been archived by the owner on Feb 16, 2025. It is now read-only.

feat: add link to logs #620

Merged
merged 11 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions website/static/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ img.emblem-avatar {
.emblem-campaign-details .mdc-button {
margin-top: 1em;
float: right;
display: inline-flex;
}

.emblem-campaign-details .mdc-button:nth-child(1) {
margin-left: 1em;
}

/***** Donate to Campaign page *****/
Expand Down
14 changes: 13 additions & 1 deletion website/templates/donations/view-donation.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@
<span class="mdc-list-item__meta">{{ donation.time_created }}</span>
</li>
</ul>

</div>
<!-- Buttons -->
<div class="emblem-campaign-details mdc-layout-grid__cell mdc-layout-grid__cell--span-12">
Copy link
Contributor Author

@ace-n ace-n Aug 16, 2022

Choose a reason for hiding this comment

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

Not the cleanest frontend implementation (it doesn't use Material UI's grid layout for right-alignment), but I'm assuming that's not super-important here. 🙂

{% if logsUrl %}
<!-- View Logs button -->
<a href="{{ logsUrl }}" class="mdc-button mdc-button--raised" target=”_blank”>
<span class="mdc-button__ripple"></span>
<i class="material-icons mdc-button__icon" aria-hidden="true"
>manage_search</i
>
<span class="mdc-button__label">View logs</span>
</a>
{% endif %}
<!-- Donate button -->
<a href="/donate?campaign_id={{ campaign.id }}" class="mdc-button mdc-button--raised">
<span class="mdc-button__ripple"></span>
Expand Down
20 changes: 19 additions & 1 deletion website/views/donations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


import os
import requests
from flask import after_this_request, Blueprint, g, redirect, request, render_template

from middleware import session
Expand Down Expand Up @@ -101,7 +102,8 @@ def add_header(response):
log(f"Exception when creating a donation: {e}", severity="ERROR")
return render_template("errors/403.html"), 403

return redirect("/viewDonation?donation_id=" + donation.id)
trace_id = request.headers.get("X-Cloud-Trace-Context").split("/")[0]
return redirect(f"/viewDonation?donation_id={donation.id}&trace_id={trace_id}")


@donations_bp.route("/viewDonation", methods=["GET"])
Expand All @@ -119,8 +121,24 @@ def webapp_view_donation():
log(f"Exception when getting a campaign for a donations: {e}", severity="ERROR")
return render_template("errors/403.html"), 403

logs_url = None
try:
trace_id = request.args.get("trace_id")
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See line 106 - the POST /donate trace ID is captured there, and forwarded to the GET /viewDonation request via this query parameter.

(As explained here, the trace ID from POST /donate does not get propagated to GET /viewDonation. Thus, they have totally separate trace IDs.)

Copy link
Contributor

@grayside grayside Aug 23, 2022

Choose a reason for hiding this comment

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

We can talk more broadly about the trace propagation in #625.

I'm okay not blocking this, but maybe we should have a TODO pointing to 625 for a more permanent solution?

For this specific mode on trace propagation, I don't like the idea that the trace_id is a visible part of the website interface. This may make more sense as session data. @muncus do you have any thoughts on "manual" trace propagation across a web redirect?

Copy link
Contributor Author

@ace-n ace-n Aug 24, 2022

Choose a reason for hiding this comment

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

@grayside now that you mention it, cookies seem appropriate here. @engelke are we OK using those instead of session buckets?

(I'd rather not write to GCS with each request. 🙂 )

Rationale for cookies: this is ephemeral, user-specific data

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a specific reason the two requests should share a trace id? My understanding is that traces are intended to model single interactions, and trace IDs are not intended to be reused between requests (hence why you're having a hard time doing so 😄 ).

If displaying the donation really must be in the same trace, i'd suggest rendering the template here directly, instead of redirecting. Otherwise i'd let the two actions remain separate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@muncus this is useful context, thanks. We could include both trace IDs in the log filter, perhaps?


project_req = requests.get(
"http://metadata.google.internal/computeMetadata/v1/project/project-id",
headers={"Metadata-Flavor": "Google"},
)
project_id = project_req.text

if trace_id and project_id:
logs_url = f"https://console.cloud.google.com/logs/query;query=trace%3D~%22projects%2F{project_id}%2Ftraces%2F{trace_id}%22?project={project_id}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to query limit to project when we're already operating within a particular project?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call-out: TIL you can do cross-project traces.

@pattishin do you mind if we drop the per-project filter?

except Exception as e:
log(f"Exception when getting trace ID: {e}", severity="WARNING")

return render_template(
"donations/view-donation.html",
donation=donation_instance,
campaign=campaign_instance,
logsUrl=logs_url,
)