-
Notifications
You must be signed in to change notification settings - Fork 836
/
Copy pathResource.ts
151 lines (137 loc) · 5.1 KB
/
Resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright The 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 { diag } from '@opentelemetry/api';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { SDK_INFO } from '@opentelemetry/core';
import { ResourceAttributes } from './types';
import { defaultServiceName } from './platform';
import { IResource } from './IResource';
/**
* A Resource describes the entity for which a signals (metrics or trace) are
* collected.
*/
export class Resource implements IResource {
static readonly EMPTY = new Resource({});
private _syncAttributes?: ResourceAttributes;
private _asyncAttributesPromise?: Promise<ResourceAttributes>;
private _attributes?: ResourceAttributes;
/**
* Check if async attributes have resolved. This is useful to avoid awaiting
* waitForAsyncAttributes (which will introduce asynchronous behavior) when not necessary.
*
* @returns true if the resource "attributes" property is not yet settled to its final value
*/
public asyncAttributesPending?: boolean;
/**
* Returns an empty Resource
*/
static empty(): IResource {
return Resource.EMPTY;
}
/**
* Returns a Resource that identifies the SDK in use.
*/
static default(): IResource {
return new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: defaultServiceName(),
[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]:
SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE],
[SemanticResourceAttributes.TELEMETRY_SDK_NAME]:
SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_NAME],
[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]:
SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_VERSION],
});
}
constructor(
/**
* A dictionary of attributes with string keys and values that provide
* information about the entity as numbers, strings or booleans
* TODO: Consider to add check/validation on attributes.
*/
attributes: ResourceAttributes,
asyncAttributesPromise?: Promise<ResourceAttributes>
) {
this._attributes = attributes;
this.asyncAttributesPending = asyncAttributesPromise != null;
this._syncAttributes = this._attributes ?? {};
this._asyncAttributesPromise = asyncAttributesPromise?.then(
asyncAttributes => {
this._attributes = Object.assign({}, this._attributes, asyncAttributes);
this.asyncAttributesPending = false;
return asyncAttributes;
},
err => {
diag.debug("a resource's async attributes promise rejected: %s", err);
this.asyncAttributesPending = false;
return {};
}
);
}
get attributes(): ResourceAttributes {
if (this.asyncAttributesPending) {
diag.error(
'Accessing resource attributes before async attributes settled'
);
}
return this._attributes ?? {};
}
/**
* Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to
* this Resource's attributes. This is useful in exporters to block until resource detection
* has finished.
*/
async waitForAsyncAttributes?(): Promise<void> {
if (this.asyncAttributesPending) {
await this._asyncAttributesPromise;
}
}
/**
* Returns a new, merged {@link Resource} by merging the current Resource
* with the other Resource. In case of a collision, other Resource takes
* precedence.
*
* @param other the Resource that will be merged with this.
* @returns the newly merged Resource.
*/
merge(other: IResource | null): IResource {
if (!other) return this;
// SpanAttributes from other resource overwrite attributes from this resource.
const mergedSyncAttributes = {
...this._syncAttributes,
//Support for old resource implementation where _syncAttributes is not defined
...((other as Resource)._syncAttributes ?? other.attributes),
};
if (
!this._asyncAttributesPromise &&
!(other as Resource)._asyncAttributesPromise
) {
return new Resource(mergedSyncAttributes);
}
const mergedAttributesPromise = Promise.all([
this._asyncAttributesPromise,
(other as Resource)._asyncAttributesPromise,
]).then(([thisAsyncAttributes, otherAsyncAttributes]) => {
return {
...this._syncAttributes,
...thisAsyncAttributes,
//Support for old resource implementation where _syncAttributes is not defined
...((other as Resource)._syncAttributes ?? other.attributes),
...otherAsyncAttributes,
};
});
return new Resource(mergedSyncAttributes, mergedAttributesPromise);
}
}