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

Function getMetadata to get variables container.id and host.name that have already been defined inside agent #4449

Closed
sefiros1 opened this issue Feb 3, 2025 · 2 comments

Comments

@sefiros1
Copy link
Contributor

sefiros1 commented Feb 3, 2025

Is your feature request related to a problem? Please describe.
I need to get metadata container.id and/or host.name from APM Agent for my logger lib to link my logs with service in Kibana UI.
I've already added ...apmAgent.currentTraceIds() in logs, but not works as intended. For example, container.id or host.name required for logs to be shown in Logs tab in Kibana (thread).

I not have access or opportunity to add these variables in filebeat or something else outside of node application. My solutions will be dictated by this limitation.

Describe the solution you'd like
I suggest to create function apmAgent.getMetadata(), that will return object like this:

{
  service: {
    name: 'localtest',
    environment: 'develop',
    runtime: { name: 'node', version: '20.15.1' },
    language: { name: 'javascript' },
    agent: { name: 'nodejs', version: '4.11.0', activation_method: 'require' },
    framework: undefined,
    version: '5.0.0',
    node: undefined
  },
  process: {
    pid: 1160,
    ppid: 1,
    title: 'node',
    argv: [ '/usr/local/bin/ts-node', '/app/lib/apm-agent.ts' ]
  },
  system: {
    architecture: 'x64',
    platform: 'linux',
    container: { id: 'some-container-id' },
    kubernetes: undefined,
    detected_hostname: '54e8d5b15c0d'
  }
}

It can be stored and updated in http-apm-client and proxied through Agent.

Describe alternatives you've considered
As temporary working solution, I ended up with using addMetadataFilter function like this:

public async getMetadata(apmAgent: Agent) {
	return new Promise<Payload>(resolve => {
		apmAgent.addMetadataFilter((payload) => {
			resolve(payload);
			return payload;
		});
	});
}

As far as I know, this filter should be invoked immediately after adding if agent is started. But still looks like bad solution in my opinion, because multiple calls will increase filter array and execution ticks for process these filters (memory leak opportunity).

Additional context

I repeat, that I do not have access or opportunity to add these variables in filebeat or something else outside of node application, I will appreciate solution in NodeJS application.

@david-luna
Copy link
Member

@sefiros1

There are no plans for adding new features to elastic-apm-node. The solution you're proposing is a good use case for the addMetadataFilter API and should be IMO it should be in the documentation.

But still looks like bad solution in my opinion, because multiple calls will increase filter array and execution ticks for process these filters (memory leak opportunity).

this is a good point and to avoid this problem we could modify a bit this code to not add a filter for each call to the function

// Hold references to metadata & a promise 
let apmMetadata = null;
let metadataResolve;
let metadataPromise = new Promise((resolve) => {
  metadataResolve = resolve;
});

// Call filter only once at TOP level
// - apmMetadata will be updated for every filter call
// - the promise will resolve with the 1st value 
apm.addMetadataFilter((md) => {
  apmMetadata = md;
  mdResolve(apmMetadata);
  return md;
});

// function will return last metadata or the promise for the 1st call
async function getMetadata() {
  if (apmMetadata) {
    return apmMetadata;
  }
  return metadataPromise;
}

Cheers

@sefiros1
Copy link
Contributor Author

sefiros1 commented Feb 6, 2025

@david-luna Thank you for your answer.
I'll stay with addMetadataFilter with your addition.

@sefiros1 sefiros1 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants