Skip to content

Commit c6b11e3

Browse files
authored
fix: Correct an order of instrumentation entries (#1362)
* fix: Correct an order of instrumentation entries * Add MAX_INSTRUMENTATION_COUNT constant
1 parent 90bb09f commit c6b11e3

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

src/utils/instrumentation.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const maxDiagnosticValueLen = 14;
3838
export const DIAGNOSTIC_INFO_KEY = 'logging.googleapis.com/diagnostic';
3939
export const INSTRUMENTATION_SOURCE_KEY = 'instrumentation_source';
4040
export const NODEJS_LIBRARY_NAME_PREFIX = 'nodejs';
41+
export const MAX_INSTRUMENTATION_COUNT = 3;
4142
export type InstrumentationInfo = {name: string; version: string};
4243

4344
/**
@@ -131,23 +132,23 @@ function validateAndUpdateInstrumentation(
131132
infoList: InstrumentationInfo[]
132133
): InstrumentationInfo[] {
133134
const finalInfo: InstrumentationInfo[] = [];
134-
// First, add current library information
135-
finalInfo.push({
136-
name: NODEJS_LIBRARY_NAME_PREFIX,
137-
version: getNodejsLibraryVersion(),
138-
});
139-
// Iterate through given list of libraries and for each entry perform validations and transformations
140-
// Limit amount of entries to be up to 3
135+
// First, iterate through given list of libraries and for each entry perform validations and transformations.
136+
// Limit amount of entries to be up to MAX_INSTRUMENTATION_COUNT
141137
let count = 1;
142138
for (const info of infoList) {
143139
if (isValidInfo(info)) {
144140
finalInfo.push({
145141
name: truncateValue(info.name, maxDiagnosticValueLen),
146142
version: truncateValue(info.version, maxDiagnosticValueLen),
147143
});
144+
if (++count === MAX_INSTRUMENTATION_COUNT) break;
148145
}
149-
if (++count === 3) break;
150146
}
147+
// Finally, add current library information to be the last entry added
148+
finalInfo.push({
149+
name: NODEJS_LIBRARY_NAME_PREFIX,
150+
version: getNodejsLibraryVersion(),
151+
});
151152
return finalInfo;
152153
}
153154

test/instrumentation.ts

+60-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('instrumentation_info', () => {
8888
);
8989
});
9090

91-
it('should add instrumentation info to existing list', () => {
91+
it('should add instrumentation info to existing list in right order', () => {
9292
const dummyEntry = createEntry(NODEJS_TEST, VERSION_TEST);
9393
const entries = instrumentation.populateInstrumentationInfo(dummyEntry);
9494
assert.equal(entries[0].length, 1);
@@ -102,19 +102,19 @@ describe('instrumentation_info', () => {
102102
assert.equal(
103103
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
104104
instrumentation.INSTRUMENTATION_SOURCE_KEY
105-
]?.[0]?.[NAME],
105+
]?.[1]?.[NAME],
106106
instrumentation.NODEJS_LIBRARY_NAME_PREFIX
107107
);
108108
assert.equal(
109109
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
110110
instrumentation.INSTRUMENTATION_SOURCE_KEY
111-
]?.[1]?.[NAME],
111+
]?.[0]?.[NAME],
112112
NODEJS_TEST
113113
);
114114
assert.equal(
115115
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
116116
instrumentation.INSTRUMENTATION_SOURCE_KEY
117-
]?.[1]?.[VERSION],
117+
]?.[0]?.[VERSION],
118118
VERSION_TEST
119119
);
120120
});
@@ -147,13 +147,13 @@ describe('instrumentation_info', () => {
147147
assert.equal(
148148
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
149149
instrumentation.INSTRUMENTATION_SOURCE_KEY
150-
]?.[1]?.[NAME],
150+
]?.[0]?.[NAME],
151151
NODEJS_TEST + '-oo*'
152152
);
153153
assert.equal(
154154
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
155155
instrumentation.INSTRUMENTATION_SOURCE_KEY
156-
]?.[1]?.[VERSION],
156+
]?.[0]?.[VERSION],
157157
VERSION_TEST + '.0.0.0.0.*'
158158
);
159159
});
@@ -174,6 +174,60 @@ describe('instrumentation_info', () => {
174174
assert.equal(entries[0].length, 1);
175175
assert.deepEqual(dummyEntry, entries[0][0]);
176176
});
177+
178+
it('should discard extra instrumentation records', () => {
179+
// Add 4 library versions and make sure that last 2 are discarded and the "nodejs" base
180+
// library version is always added as a third one
181+
const dummy = createEntry(
182+
instrumentation.NODEJS_LIBRARY_NAME_PREFIX + '-one',
183+
'v1'
184+
);
185+
dummy.data?.[instrumentation.DIAGNOSTIC_INFO_KEY][
186+
instrumentation.INSTRUMENTATION_SOURCE_KEY
187+
].push({
188+
name: instrumentation.NODEJS_LIBRARY_NAME_PREFIX + '-two',
189+
version: 'v2',
190+
});
191+
dummy.data?.[instrumentation.DIAGNOSTIC_INFO_KEY][
192+
instrumentation.INSTRUMENTATION_SOURCE_KEY
193+
].push({
194+
name: NODEJS_TEST,
195+
version: VERSION_TEST,
196+
});
197+
dummy.data?.[instrumentation.DIAGNOSTIC_INFO_KEY][
198+
instrumentation.INSTRUMENTATION_SOURCE_KEY
199+
].push({
200+
name: LONG_NODEJS_TEST,
201+
version: LONG_VERSION_TEST,
202+
});
203+
const entries = instrumentation.populateInstrumentationInfo(dummy);
204+
assert.equal(entries[0].length, 1);
205+
assert.equal(
206+
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
207+
instrumentation.INSTRUMENTATION_SOURCE_KEY
208+
]?.length,
209+
3
210+
);
211+
assert.equal(true, entries[1]);
212+
assert.equal(
213+
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
214+
instrumentation.INSTRUMENTATION_SOURCE_KEY
215+
]?.[0]?.[NAME],
216+
instrumentation.NODEJS_LIBRARY_NAME_PREFIX + '-one'
217+
);
218+
assert.equal(
219+
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
220+
instrumentation.INSTRUMENTATION_SOURCE_KEY
221+
]?.[1]?.[NAME],
222+
instrumentation.NODEJS_LIBRARY_NAME_PREFIX + '-two'
223+
);
224+
assert.equal(
225+
entries[0][0].data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
226+
instrumentation.INSTRUMENTATION_SOURCE_KEY
227+
]?.[2]?.[NAME],
228+
instrumentation.NODEJS_LIBRARY_NAME_PREFIX
229+
);
230+
});
177231
});
178232

179233
function createEntry(name: string | undefined, version: string | undefined) {

0 commit comments

Comments
 (0)