Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit 36fd3ba

Browse files
authored
feat: add 'extraMetadata' config option (#192)
This is a way to provide additional metadata that is merged into the metadata object created from the other individual config options. This is similar to `expectExtraMetadata: true` && `setExtraMetadata(...)`, but is a way to provide the metadata *synchronously*. Refs: elastic/apm-agent-nodejs#3071
1 parent d3842f6 commit 36fd3ba

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# elastic-apm-http-client changelog
22

3+
## v11.1.0
4+
5+
- Add an `extraMetadata` config option, which is an object to merge into the
6+
built metadata object. This is an alternative to the existing
7+
`cloudMetadataFetcher` and `expectExtraMetadata` options which provide ways
8+
to asynchronously provide metadata. Only one (or zero) of these three options
9+
may be used.
10+
311
## v11.0.4
412

513
- Update the default `serverUrl` to "http://127.0.0.1:8200". We no longer use

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ Arguments:
5656
- `options` - An object containing config options (see below). All options
5757
are optional, except those marked "(required)".
5858

59-
Data sent to the APM Server as part of the [metadata object](https://www.elastic.co/guide/en/apm/server/current/metadata-api.html):
59+
Data sent to the APM Server as part of the [metadata object](https://www.elastic.co/guide/en/apm/server/current/metadata-api.html).
60+
See also the "Cloud & Extra Metadata" section below.
6061

6162
- `agentName` - (required) The APM agent name
6263
- `agentVersion` - (required) The APM agent version
@@ -114,21 +115,22 @@ HTTP client configuration:
114115
default of 15s](https://pkg.go.dev/net#Dialer) (when talking to the Elastic
115116
APM Lambda extension). (default: `4000`)
116117

117-
Cloud & Extra Metadata Configuration:
118+
Cloud & Extra Metadata Configuration. Zero or one of the following three
119+
options may be used.
118120

119121
- `cloudMetadataFetcher` - An object with a `getCloudMetadata(cb)` method
120122
for fetching metadata related to the current cloud environment. The callback
121123
is of the form `function (err, cloudMetadata)` and the returned `cloudMetadata`
122124
will be set on `metadata.cloud` for intake requests to APM Server. If
123125
provided, this client will not begin any intake requests until the callback
124-
is called. The `cloudMetadataFetcher` option must not be used with the
125-
`expectExtraMetadata` option.
126+
is called.
126127
- `expectExtraMetadata` - A boolean option to indicate that the client should
127128
not allow any intake requests to begin until `cloud.setExtraMetadata(...)`
128129
has been called. It is the responsibility of the caller to call
129130
`cloud.setExtraMetadata()`. If not, then the Client will never perform an
130-
intake request. The `expectExtraMetadata` option must not be used with the
131-
`cloudMetadataFetcher` option.
131+
intake request.
132+
- `extraMetadata` - An object with extra metadata to merge into the metadata
133+
object created from the individual fields above.
132134

133135
APM Agent Configuration via Kibana:
134136

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,13 @@ function Client (opts) {
129129
this.config(opts)
130130
this._log = this._conf.logger || new NoopLogger()
131131

132-
if (this._conf.cloudMetadataFetcher && this._conf.expectExtraMetadata) {
133-
throw new Error('it is an error to create a Client with both cloudMetadataFetcher and expectExtraMetadata')
132+
const numExtraMdOpts = [
133+
this._conf.cloudMetadataFetcher,
134+
this._conf.expectExtraMetadata,
135+
this._conf.extraMetadata
136+
].reduce((accum, curr) => curr ? accum + 1 : accum, 0)
137+
if (numExtraMdOpts > 1) {
138+
throw new Error('it is an error to configure a Client with more than one of "cloudMetadataFetcher", "expectExtraMetadata", or "extraMetadata"')
134139
} else if (this._conf.cloudMetadataFetcher) {
135140
// Start stream in corked mode, uncork when cloud metadata is fetched and
136141
// assigned. Also, the _maybeUncork will not uncork until _encodedMetadata
@@ -155,6 +160,8 @@ function Client (opts) {
155160
// Uncorking will happen in the expected `.setExtraMetadata()` call.
156161
this._log.trace('corking (expectExtraMetadata)')
157162
this.cork()
163+
} else if (this._conf.extraMetadata) {
164+
this.setExtraMetadata(this._conf.extraMetadata)
158165
} else {
159166
this._resetEncodedMetadata()
160167
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "elastic-apm-http-client",
3-
"version": "11.0.4",
3+
"version": "11.1.0",
44
"description": "A low-level HTTP client for communicating with the Elastic APM intake API",
55
"main": "index.js",
66
"directories": {

test/extraMetadata.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
// Test usage of `extraMetadata: ...`.
4+
5+
const test = require('tape')
6+
const utils = require('./lib/utils')
7+
8+
const APMServer = utils.APMServer
9+
const processIntakeReq = utils.processIntakeReq
10+
11+
test('extraMetadata', function (t) {
12+
const apmEvents = []
13+
const extraMetadata = {
14+
foo: 'bar',
15+
service: {
16+
language: {
17+
name: 'spam'
18+
}
19+
}
20+
}
21+
22+
const server = APMServer(function (req, res) {
23+
const objStream = processIntakeReq(req)
24+
objStream.on('data', function (obj) {
25+
apmEvents.push(obj)
26+
})
27+
objStream.on('end', function () {
28+
res.statusCode = 202
29+
res.end()
30+
})
31+
}).client({ extraMetadata, apmServerVersion: '8.0.0' }, function (client) {
32+
client.sendTransaction({ req: 1 })
33+
34+
client.flush(() => {
35+
t.equal(apmEvents.length, 2, 'APM Server got 2 events')
36+
t.ok(apmEvents[0].metadata, 'event 0 is metadata')
37+
t.equal(apmEvents[0].metadata.foo, 'bar', 'extraMetadata added "foo" field')
38+
t.equal(apmEvents[0].metadata.service.language.name, 'spam',
39+
'extraMetadata overrode nested service.language.name field properly')
40+
t.ok(apmEvents[1].transaction, 'event 1 is a transaction')
41+
42+
client.end()
43+
server.close()
44+
t.end()
45+
})
46+
})
47+
})

0 commit comments

Comments
 (0)