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

Commit 98d0442

Browse files
authored
Map http attributes to the stackdriver format (#652)
* Map http attributes to the stackdriver format * update CHANGELOG
1 parent 77ecbf0 commit 98d0442

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44

55
## Unreleased
66
- Fix a TypeError of url.parse (#640)
7+
- Map http attributes to the stackdriver format
78

89
## 0.0.17 - 2019-09-03
910
- fix: allow override global trace params limits (#643)

packages/opencensus-exporter-stackdriver/src/stackdriver-cloudtrace-utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ const AGENT_LABEL_KEY = 'g.co/agent';
2121
const AGENT_LABEL_VALUE_STRING = `opencensus-node [${coreTypes.version}]`;
2222
const AGENT_LABEL_VALUE = createAttributeValue(AGENT_LABEL_VALUE_STRING);
2323

24+
const HTTP_ATTRIBUTE_MAPPING: { [key: string]: string } = {
25+
'http.host': '/http/host',
26+
'http.method': '/http/method',
27+
'http.path': '/http/path',
28+
'http.route': '/http/route',
29+
'http.user_agent': '/http/user_agent',
30+
'http.status_code': '/http/status_code',
31+
'http.url': '/http/url',
32+
};
33+
2434
/**
2535
* Creates StackDriver Links from OpenCensus Link.
2636
* @param links coreTypes.Link[]
@@ -132,7 +142,8 @@ function createAttributesBuilder(
132142
): types.Attributes {
133143
const attributeMap: Record<string, types.AttributeValue> = {};
134144
for (const key of Object.keys(attributes)) {
135-
attributeMap[key] = createAttributeValue(attributes[key]);
145+
const mapKey = HTTP_ATTRIBUTE_MAPPING[key] || key;
146+
attributeMap[mapKey] = createAttributeValue(attributes[key]);
136147
}
137148
return { attributeMap, droppedAttributesCount };
138149
}

packages/opencensus-exporter-stackdriver/test/test-stackdriver-cloudtrace-utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,39 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
249249
expectedAttributeMap
250250
);
251251
});
252+
253+
it('should map http attributes to the stackdriver format', () => {
254+
const attributes = {
255+
'http.host': 'localhost',
256+
'http.method': 'GET',
257+
'http.path': '/status',
258+
'http.route': 'route',
259+
'http.user_agent': 'agent',
260+
'http.status_code': 200,
261+
'http.url': 'http://localhost',
262+
};
263+
264+
const expectedMap = {
265+
'g.co/agent': {
266+
stringValue: { value: `opencensus-node [${coreTypes.version}]` },
267+
},
268+
'/http/host': { stringValue: { value: 'localhost' } },
269+
'/http/method': { stringValue: { value: 'GET' } },
270+
'/http/path': { stringValue: { value: '/status' } },
271+
'/http/route': { stringValue: { value: 'route' } },
272+
'/http/user_agent': { stringValue: { value: 'agent' } },
273+
'/http/status_code': { intValue: '200' },
274+
'/http/url': { stringValue: { value: 'http://localhost' } },
275+
};
276+
277+
const stackdriverAttribute = createAttributes(attributes, {}, 0);
278+
assert.strictEqual(stackdriverAttribute.droppedAttributesCount, 0);
279+
assert.strictEqual(
280+
Object.keys(stackdriverAttribute.attributeMap!).length,
281+
8
282+
);
283+
assert.deepStrictEqual(stackdriverAttribute.attributeMap, expectedMap);
284+
});
252285
});
253286

254287
describe('getResourceLabels()', () => {

0 commit comments

Comments
 (0)