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: add apm.setGlobalLabel() #3337

Merged
merged 6 commits into from
May 17, 2023

Conversation

afharo
Copy link
Member

@afharo afharo commented May 10, 2023

In Kibana, we'd like to be able to update some of the global labels based on some dynamic information. The existing apm.addLabels or apm.setLabel only apply to the current transaction, but we need these specific labels to be applied to all the transactions.

Related to elastic/kibana#157093

Checklist

  • Implement code
  • Add tests
  • Update TypeScript typings
  • Update documentation
  • Add CHANGELOG.asciidoc entry
  • Commit message follows commit guidelines

@afharo afharo force-pushed the add-agent.setGlobalLabel branch from ac594e9 to cdc034e Compare May 10, 2023 10:20
@github-actions github-actions bot added agent-nodejs Make available for APM Agents project planning. community triage labels May 10, 2023
@afharo afharo force-pushed the add-agent.setGlobalLabel branch from cdc034e to 6f90794 Compare May 10, 2023 11:06
afharo added a commit to afharo/kibana that referenced this pull request May 10, 2023
@afharo afharo changed the title Add apm.setGlobalLabel() feat: add apm.setGlobalLabel() May 10, 2023
@afharo afharo marked this pull request as ready for review May 16, 2023 14:05
Copy link
Member

@trentm trentm left a comment

Choose a reason for hiding this comment

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

Thanks! This looks great and works for me. I used this example script to try it out: https://gist.github.com/trentm/bc7a6ae0cb849bccf3b87857963185b0

A couple suggested changes:

  1. What do you think about not having this return true? I don't see the value. The existing setLabel only has that to handle there not being a current transaction. If so, then this patch would remove that:
diff --git a/index.d.ts b/index.d.ts
index e2ecc452..b3614ca4 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -116,7 +116,7 @@ declare namespace apm {
     currentSpan: Span | null;

     // Context
-    setGlobalLabel (name: string, value: LabelValue): boolean;
+    setGlobalLabel (name: string, value: LabelValue): void;
     setLabel (name: string, value: LabelValue, stringify?: boolean): boolean;
     addLabels (labels: Labels, stringify?: boolean): boolean;
     setUserContext (user: UserObject): void;
diff --git a/lib/agent.js b/lib/agent.js
index 1c378353..d11320be 100644
--- a/lib/agent.js
+++ b/lib/agent.js
@@ -368,7 +368,6 @@ Agent.prototype.setGlobalLabel = function (key, value) {
       return acc
     }, {})
   })
-  return true
 }

 Agent.prototype.setLabel = function (key, value, stringify) {
  1. Here is an update to the agent.test.js that will have the agent send an actual transaction (and updated metadata) to a mock APM server. This allows the test to verify that the this._transport.config(...) call in the implementation did the necessary work.

patch: https://gist.github.com/trentm/c74e0e5ae5f626e67ad84116349eb6a3

my full test/agent.test.js if that is easier for you to apply: https://gist.github.com/trentm/5f571ecf1d55aa91d2b8b24823f99ff2

docs/agent-api.asciidoc Outdated Show resolved Hide resolved
@trentm trentm self-assigned this May 16, 2023
@trentm trentm removed the triage label May 16, 2023
@afharo
Copy link
Member Author

afharo commented May 17, 2023

Thank you, @trentm! 🧡

Your suggestions make perfect sense to me! I applied them in a8dab78 (#3337). Thank you for showing me The way!

@david-luna
Copy link
Member

Thank you @afharo for your contribution. Just a small comment but not blocking

modulePatches[0] === handler1 &&
modulePatches[1] === handler2, 'module patches are as expected')
modulePatches[0] === handler1 &&
modulePatches[1] === handler2, 'module patches are as expected')
Copy link
Member

Choose a reason for hiding this comment

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

is this a linter problem or our editor did some autoformatting?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops! Not sure what happened here... probably my IDE behaving. I removed those extra spaces in my last commit 😇

@david-luna david-luna merged commit e55fb8b into elastic:main May 17, 2023
@afharo afharo deleted the add-agent.setGlobalLabel branch May 17, 2023 14:33
@david-luna david-luna mentioned this pull request Jun 13, 2023
2 tasks
PeterEinberger pushed a commit to fpm-git/apm-agent-nodejs that referenced this pull request Aug 20, 2024
New API to set global labels programatically within the instrumented app

---------
Co-authored-by: Trent Mick <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning. community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants