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

Feat/datadog js monitoring #483

Merged
merged 6 commits into from
Feb 5, 2025
Merged

Conversation

davidsauntson
Copy link
Contributor

@davidsauntson davidsauntson commented Feb 5, 2025

To upload the js and sourcemaps to datadog, we need an APP_VERSION that is consistent across the CI run and available to the app itself, so it can be sent along with the errors raised in the client side js. To do this, I have:

  • created an env var called APP_VERSION and set it to the commit sha
  • exposed the commit sha in a meta tag in the app
  • used the commit sha directly in the --release-version flag in the datadog upload command

I've also turned off deployment to prod whilst I test this by raising the deliberate error in datadog (I don't want to have console errors in prod during testing).

@davidsauntson davidsauntson marked this pull request as ready for review February 5, 2025 09:34
The `APP_VERSION` needs to be the same for in the javascript initialisation AND the command line tool that uploads the sourcemaps into datadog.  Using an evironment var allows us to set the `APP_VERSION` variable in the build process, use it in the command line and pass it into the app js.
Copy link
Contributor

@suzannehamilton suzannehamilton left a comment

Choose a reason for hiding this comment

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

This is great! 🎉 Just a couple of minor comments.

@@ -10,7 +10,8 @@ image:
tag: "main"

# plain env vars. Take precedence over any secrets with the same name!
envVars: {}
envVars:
APP_VERSION: ${{ github.sha }}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would be helpful for this to match the backend app version? That uses the Docker image tag (which I think is the image hash?) rather than the GitHub SHA:

tags.datadoghq.com/version: {{ .Values.image.tag }}

TBH, I hardly ever use the version information in Datadog, so might not be worth the effort of making them match if it's hard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I'm a bit confused as to what this would resolved to - AFAICT it's the helm chart version. Would this exist at the point the datadog upload command is run?

Copy link
Contributor

Choose a reason for hiding this comment

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

Example Datadog trace. It has a version tag of bbd79ab1fa0ddc974590d31466119d7a58d3b52e, which matches the Docker image tag for the current latest image.

The Datadog upload step happens after the dev deployment, so the image tag should be available somewhere in GHA.

Alternatively, we could use the git SHA in both places? I think either one (Docker hash or git SHA) would be helpful if you're trying to work out which version of the code is responsible for a specific trace.

import { datadogLogs } from '@datadog/browser-logs'

const initDatadog = () => {
const ddEnv = window.location.hostname === 'www.citizensadvice.org.uk' ? "prod" : "dev";
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this app have review environments? I'm wondering if it would be better to get the environment from the DOM (and ultimately an environment variable) rather than hardcoding the prod URL like this.

Copy link
Contributor Author

@davidsauntson davidsauntson Feb 5, 2025

Choose a reason for hiding this comment

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

Yep I could make it an env var like the version number. I hadn't thought to upload the files for review apps, at the moment it would only run on successful deployment to qa and prod

Actually I think this will require a rethink of the workflow structure, because the datadog env is set using matrix.stage for qa and prod builds. This doesn't exist during the new datadog upload workflow because it's run after both have completed (deploy to qa and prod are in the same workflow).


if (ddEnv && appVersion) {
datadogLogs.init({
clientToken: 'pub32a7538113d7910e4304b8aa13ff4521',
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment explaining that this is designed to be a public token - it's not a security issue to have this in the code?

Are client tokens linked domain name or something? What stops someone from spamming our logs? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep I can add a comment.

It looks like datadog are aware this could be used to send spam data - DataDog/browser-sdk#853 and DataDog/browser-sdk#853 (comment)

I'm not sure there is a way around this really? Could we build smth that forwards errors to our own endpoint and maybe use the DD API to create logs? Then we'd have more control over what ends up in the logs and from where, but I'm not sure it's worth the effort?

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed on Slack 👍

@davidsauntson
Copy link
Contributor Author

This is great! 🎉 Just a couple of minor comments.

Thanks! I've added some responses.

I'd like to check the datadog command and parameters work in the qa env before I make lots of changes to this, otherwise I think the PR might get confusing.

I'd like to propose:

  • leaving the hostname parsing to work out the datadog env for now
  • making this work for review apps in a separate PR once it's working on prod and dev

Is that ok? The spamming question remains but I can't see an easy fix that isn't a mini-project in its own right. I might be missing something tho.

Copy link
Contributor

@suzannehamilton suzannehamilton left a comment

Choose a reason for hiding this comment

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

Approving this so it can be tested on dev.

I'm going to look into the version question a bit more in case we can make the versions consistent. Happy to revisit the environments question at a later date.

@davidsauntson davidsauntson merged commit 320d861 into main Feb 5, 2025
7 checks passed
@davidsauntson davidsauntson deleted the feat/datadog-js-monitoring branch February 5, 2025 13:24
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

Successfully merging this pull request may close these issues.

2 participants