Skip to content

Commit

Permalink
Resource API (open-telemetry#59)
Browse files Browse the repository at this point in the history
* Resource: Add merge function

* Add merge operation on Resource

* use shorthand syntax
  • Loading branch information
mayurkale22 authored Jun 28, 2019
1 parent f663c1f commit 969925d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/opentelemetry-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@
"ts-node": "^8.0.0",
"typescript": "^3.4.5"
},
"dependencies": {}
"dependencies": {
"@opentelemetry/types": "^0.0.1"
}
}
2 changes: 2 additions & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './resources/Resource';
32 changes: 32 additions & 0 deletions packages/opentelemetry-core/src/resources/Resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as types from '@opentelemetry/types';

export class Resource implements types.Resource {
constructor(
// TODO: Consider to add check/validation on labels.
readonly labels: { [key: string]: string }
) {}

merge(other: types.Resource | null): types.Resource {
if (!other || !Object.keys(other.labels).length) return this;

// Labels from resource overwrite labels from other resource.
const mergedLabels = Object.assign({}, other.labels, this.labels);
return new Resource(mergedLabels);
}
}
78 changes: 78 additions & 0 deletions packages/opentelemetry-core/test/resource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import { Resource } from '../src/resources/Resource';

describe('Resource', () => {
const resource1 = new Resource({
'k8s.io/container/name': 'c1',
'k8s.io/namespace/name': 'default',
'k8s.io/pod/name': 'pod-xyz-123',
});
const resource2 = new Resource({
'k8s.io/zone': 'zone1',
'k8s.io/location': 'location',
});
const resource3 = new Resource({
'k8s.io/container/name': 'c2',
'k8s.io/location': 'location1',
});
const emptyResource = new Resource({});

it('should return merged resource', () => {
const expectedResource = new Resource({
'k8s.io/container/name': 'c1',
'k8s.io/namespace/name': 'default',
'k8s.io/pod/name': 'pod-xyz-123',
'k8s.io/zone': 'zone1',
'k8s.io/location': 'location',
});
const actualResource = resource1.merge(resource2);
assert.strictEqual(Object.keys(actualResource.labels).length, 5);
assert.deepStrictEqual(actualResource, expectedResource);
});

it('should return merged resource when collision in labels', () => {
const expectedResource = new Resource({
'k8s.io/container/name': 'c1',
'k8s.io/namespace/name': 'default',
'k8s.io/pod/name': 'pod-xyz-123',
'k8s.io/location': 'location1',
});
const actualResource = resource1.merge(resource3);
assert.strictEqual(Object.keys(actualResource.labels).length, 4);
assert.deepStrictEqual(actualResource, expectedResource);
});

it('should return merged resource when first resource is empty', () => {
const actualResource = emptyResource.merge(resource2);
assert.strictEqual(Object.keys(actualResource.labels).length, 2);
assert.deepStrictEqual(actualResource, resource2);
});

it('should return merged resource when other resource is empty', () => {
const actualResource = resource1.merge(emptyResource);
assert.strictEqual(Object.keys(actualResource.labels).length, 3);
assert.deepStrictEqual(actualResource, resource1);
});

it('should return merged resource when other resource is null', () => {
const actualResource = resource1.merge(null);
assert.strictEqual(Object.keys(actualResource.labels).length, 3);
assert.deepStrictEqual(actualResource, resource1);
});
});
1 change: 1 addition & 0 deletions packages/opentelemetry-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

export * from './resources/Resource';
export * from './trace/span';
export * from './trace/span_context';
export * from './trace/span_kind';
Expand Down
15 changes: 14 additions & 1 deletion packages/opentelemetry-types/src/resources/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@
* limitations under the License.
*/

/** A Resource describes the entity for which a signal was collected. */
/**
* A Resource describes the entity for which a signals (metrics or trace) are
* collected.
*/
export interface Resource {
/**
* A dictionary of labels with string keys and values that provide information
* about the entity.
*/
readonly labels: { [key: string]: string };

/**
* Returns a new, merged {@link Resource} by merging the current Resource
* with the other Resource. In case of a collision, current Resource takes
* precedence.
*
* @param other the Resource that will be merged with this.
* @returns the newly merged Resource.
*/
merge(other: Resource | null): Resource;
}

0 comments on commit 969925d

Please sign in to comment.