Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Create a wrapper library to tag span with API metadata #12

Merged

Conversation

philippemiguet
Copy link
Contributor

@philippemiguet philippemiguet commented Oct 3, 2021

New Bufftracer library to standardize how DD traces are tracked and tagged.

Comment on lines 13 to 27
const DD_SERVICE_NAME = env.get('DD_SERVICE_NAME').required().asString()

const DD_TRACE_AGENT_HOST = env
.get('DD_TRACE_AGENT_HOST')
.required()
.asString()

const DD_TRACE_AGENT_PORT = env
.get('DD_TRACE_AGENT_PORT')
.required()
.asString()

const DD_ENABLE_TRACING = env.get('DD_ENABLE_TRACING').required().asBool()
const APP_STAGE = env.get('APP_STAGE').default('').asString()
const NODE_ENV = env.get('NODE_ENV').asString()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CoxyBoris, I'm thinking maybe we don't rely on ENV var at all and let each client pass these data? I'm not sure what is the best developer experience?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got my answer while writing the README, it's much easier to leave that code here, and only ask engineers to set the ENV VAR in the helm chart. The way I wrote the library, we can easily override any of these env variables if we need.

Copy link

@CoxyBoris CoxyBoris left a comment

Choose a reason for hiding this comment

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

Couple of comments here, nothing blocking ! Thanks for that work @philippemiguet !

.editorconfig Outdated

[*.graphql]
indent_style = space
indent_size = 2

Choose a reason for hiding this comment

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

Shall we remove this file?

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 one, I will try. I had issues with the .md file and copied it from core. Do prettier and eslint work for MB files?

Comment on lines 24 to 39
export function isGraphQlRequest(
req: GraphQlRequest | RPCRequest,
): req is GraphQlRequest {
// Any GraphQL request (query or mutation) is passed as a string in bode.query
return req?.body && 'query' in req.body
}

export function isRPCRequest(
req: GraphQlRequest | RPCRequest,
): req is RPCRequest {
const hasNameInBody = req?.body && 'name' in req.body
const hasMethodInParams = req?.params && 'method' in req.params
const hasArgsInBody = req?.body && 'args' in req.body

return (hasNameInBody || hasMethodInParams) && hasArgsInBody
}

Choose a reason for hiding this comment

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

Shall these methods leave in this file? 🤔 Sounds more like utils than types 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if we want types.ts to be purely about types, then yes. I will change that 👍

span: Span | undefined,
req: RPCRequest | GraphQlRequest,
errorCallback: (ex: Error) => void,
): boolean {

Choose a reason for hiding this comment

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

Not sure if it's needed to return anything here 🤔 Looks like it's not used where it's called. Maybe void would be more accurate?
I see that you use it in tests, but maybe we can change that and check if errorCallback was called or not 😇 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch, indeed I started returning boolean for the unit tests, but I don't like it! I will change that

@@ -0,0 +1,12 @@
import { GraphQlRequest, RPCRequest } from './types'

export const HEADER_CLIENT_ID = 'x-buffer-client-id'

Choose a reason for hiding this comment

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

more of a question, but in datadog will we be able to filter by both the client and the client version? We are currently sending the app version with the client header - just wondering if it will be possible to narrow things down in data dog. E.g "This endpoint hasn't been used since the android app version 6.0, which is from 5 years ago"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hitherejoe, the client here represents both the client+version. So, this is how they will appear in DD: https://share.buffer.com/o0uPWg82.

You can then filter by a specific one if you want in a single dropdown (instead of client + version): https://share.buffer.com/4gul6yO4.

Screenshot got taken from there: https://app.datadoghq.com/dashboard/meb-fbz-gmw/buffertools-api-usage-metric?tpl_var_client=%2A&from_ts=1633242996919&to_ts=1633329396919&live=true

If you think it would be better to have two different fields, we can change that 😊

Choose a reason for hiding this comment

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

perfect! Was just curious that's all - I think this will be great as it is for now 🙌

@hitherejoe hitherejoe self-requested a review October 4, 2021 09:27
Copy link

@hitherejoe hitherejoe left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks for your work here, @philippemiguet ! 🚀

tagSpanTrace(span, req)
},
},
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CoxyBoris, due to a bug within the dd-trace library, we can't export this as an initTracer function to use in our code. If we do, then the tracer.use hook won't work. It seems to be a known issue, so I will open a ticket for DD support and hope to change it back when I get back from vacation.

For now, we will simply do a `import from '@bufferapp/bufftracer' and won't be able to pass an error call back.

Choose a reason for hiding this comment

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

That sounds good to me 🙂 , since the library catch errors and don't crash the process we're good

Copy link

@erickhun erickhun left a comment

Choose a reason for hiding this comment

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

Not much to add, but really excited by this work @philippemiguet !

Also since we're pushing tooling to improve devs velocity, and we have issues with old versions of bufflog ) , I'm working on forcing/tracking internal upgrades. Probably a process including depending of dependabot + Jira

@philippemiguet philippemiguet merged commit 37f86e7 into wg-create-api-apm-tagger Oct 5, 2021
@philippemiguet philippemiguet deleted the wg-actually-create-api-apm-tagger branch October 5, 2021 12:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants