Skip to content

Commit 969925d

Browse files
authored
Resource API (open-telemetry#59)
* Resource: Add merge function * Add merge operation on Resource * use shorthand syntax
1 parent f663c1f commit 969925d

File tree

6 files changed

+130
-2
lines changed

6 files changed

+130
-2
lines changed

packages/opentelemetry-core/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@
4949
"ts-node": "^8.0.0",
5050
"typescript": "^3.4.5"
5151
},
52-
"dependencies": {}
52+
"dependencies": {
53+
"@opentelemetry/types": "^0.0.1"
54+
}
5355
}

packages/opentelemetry-core/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
export * from './resources/Resource';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as types from '@opentelemetry/types';
18+
19+
export class Resource implements types.Resource {
20+
constructor(
21+
// TODO: Consider to add check/validation on labels.
22+
readonly labels: { [key: string]: string }
23+
) {}
24+
25+
merge(other: types.Resource | null): types.Resource {
26+
if (!other || !Object.keys(other.labels).length) return this;
27+
28+
// Labels from resource overwrite labels from other resource.
29+
const mergedLabels = Object.assign({}, other.labels, this.labels);
30+
return new Resource(mergedLabels);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { Resource } from '../src/resources/Resource';
19+
20+
describe('Resource', () => {
21+
const resource1 = new Resource({
22+
'k8s.io/container/name': 'c1',
23+
'k8s.io/namespace/name': 'default',
24+
'k8s.io/pod/name': 'pod-xyz-123',
25+
});
26+
const resource2 = new Resource({
27+
'k8s.io/zone': 'zone1',
28+
'k8s.io/location': 'location',
29+
});
30+
const resource3 = new Resource({
31+
'k8s.io/container/name': 'c2',
32+
'k8s.io/location': 'location1',
33+
});
34+
const emptyResource = new Resource({});
35+
36+
it('should return merged resource', () => {
37+
const expectedResource = new Resource({
38+
'k8s.io/container/name': 'c1',
39+
'k8s.io/namespace/name': 'default',
40+
'k8s.io/pod/name': 'pod-xyz-123',
41+
'k8s.io/zone': 'zone1',
42+
'k8s.io/location': 'location',
43+
});
44+
const actualResource = resource1.merge(resource2);
45+
assert.strictEqual(Object.keys(actualResource.labels).length, 5);
46+
assert.deepStrictEqual(actualResource, expectedResource);
47+
});
48+
49+
it('should return merged resource when collision in labels', () => {
50+
const expectedResource = new Resource({
51+
'k8s.io/container/name': 'c1',
52+
'k8s.io/namespace/name': 'default',
53+
'k8s.io/pod/name': 'pod-xyz-123',
54+
'k8s.io/location': 'location1',
55+
});
56+
const actualResource = resource1.merge(resource3);
57+
assert.strictEqual(Object.keys(actualResource.labels).length, 4);
58+
assert.deepStrictEqual(actualResource, expectedResource);
59+
});
60+
61+
it('should return merged resource when first resource is empty', () => {
62+
const actualResource = emptyResource.merge(resource2);
63+
assert.strictEqual(Object.keys(actualResource.labels).length, 2);
64+
assert.deepStrictEqual(actualResource, resource2);
65+
});
66+
67+
it('should return merged resource when other resource is empty', () => {
68+
const actualResource = resource1.merge(emptyResource);
69+
assert.strictEqual(Object.keys(actualResource.labels).length, 3);
70+
assert.deepStrictEqual(actualResource, resource1);
71+
});
72+
73+
it('should return merged resource when other resource is null', () => {
74+
const actualResource = resource1.merge(null);
75+
assert.strictEqual(Object.keys(actualResource.labels).length, 3);
76+
assert.deepStrictEqual(actualResource, resource1);
77+
});
78+
});

packages/opentelemetry-types/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
export * from './resources/Resource';
1718
export * from './trace/span';
1819
export * from './trace/span_context';
1920
export * from './trace/span_kind';

packages/opentelemetry-types/src/resources/Resource.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
/** A Resource describes the entity for which a signal was collected. */
17+
/**
18+
* A Resource describes the entity for which a signals (metrics or trace) are
19+
* collected.
20+
*/
1821
export interface Resource {
1922
/**
2023
* A dictionary of labels with string keys and values that provide information
2124
* about the entity.
2225
*/
2326
readonly labels: { [key: string]: string };
27+
28+
/**
29+
* Returns a new, merged {@link Resource} by merging the current Resource
30+
* with the other Resource. In case of a collision, current Resource takes
31+
* precedence.
32+
*
33+
* @param other the Resource that will be merged with this.
34+
* @returns the newly merged Resource.
35+
*/
36+
merge(other: Resource | null): Resource;
2437
}

0 commit comments

Comments
 (0)