forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Resource: Add merge function * Add merge operation on Resource * use shorthand syntax
- Loading branch information
1 parent
f663c1f
commit 969925d
Showing
6 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters