1 | ;
|
2 | /**
|
3 | * Copyright 2019, OpenCensus Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.TagMap = void 0;
|
19 | const types_1 = require("./types");
|
20 | const validation_1 = require("./validation");
|
21 | const UNLIMITED_PROPAGATION_MD = {
|
22 | tagTtl: types_1.TagTtl.UNLIMITED_PROPAGATION,
|
23 | };
|
24 | /** TagMap is maps of TagKey -> TagValueWithMetadata */
|
25 | class TagMap {
|
26 | constructor() {
|
27 | // A map mapping TagKey to to its respective TagValueWithMetadata.
|
28 | this.registeredTags = new Map();
|
29 | }
|
30 | /**
|
31 | * Adds the key/value pair regardless of whether the key is present.
|
32 | * @param tagKey The TagKey which will be set.
|
33 | * @param tagValue The TagValue to set for the given key.
|
34 | * @param tagMetadata The TagMetadata associated with this Tag.
|
35 | */
|
36 | set(tagKey, tagValue, tagMetadata) {
|
37 | if (!validation_1.isValidTagKey(tagKey) || !validation_1.isValidTagValue(tagValue))
|
38 | return;
|
39 | let existingKey;
|
40 | for (const key of this.registeredTags.keys()) {
|
41 | if (key.name === tagKey.name) {
|
42 | existingKey = key;
|
43 | break;
|
44 | }
|
45 | }
|
46 | if (existingKey)
|
47 | this.registeredTags.delete(existingKey);
|
48 | const valueWithMetadata = this.getValueWithMetadata(tagValue, tagMetadata);
|
49 | this.registeredTags.set(tagKey, valueWithMetadata);
|
50 | }
|
51 | /**
|
52 | * Deletes a tag from the map if the key is in the map.
|
53 | * @param tagKey The TagKey which will be removed.
|
54 | */
|
55 | delete(tagKey) {
|
56 | this.registeredTags.delete(tagKey);
|
57 | }
|
58 | /** Gets the tags map without metadata. */
|
59 | get tags() {
|
60 | const tagsWithoutMetadata = new Map();
|
61 | for (const [tagKey, valueWithMetadata] of this.registeredTags) {
|
62 | tagsWithoutMetadata.set(tagKey, valueWithMetadata.tagValue);
|
63 | }
|
64 | return tagsWithoutMetadata;
|
65 | }
|
66 | /** Gets the tags map with metadata. */
|
67 | get tagsWithMetadata() {
|
68 | return this.registeredTags;
|
69 | }
|
70 | /**
|
71 | * Constructs a new TagValueWithMetadata using tagValue and tagMetadata.
|
72 | * For backwards-compatibility this method still produces propagating Tags
|
73 | * (UNLIMITED_PROPAGATION) if tagMetadata is not provided or missing.
|
74 | */
|
75 | getValueWithMetadata(tagValue, tagMetadata) {
|
76 | if (tagMetadata) {
|
77 | return { tagValue, tagMetadata };
|
78 | }
|
79 | return { tagValue, tagMetadata: UNLIMITED_PROPAGATION_MD };
|
80 | }
|
81 | }
|
82 | exports.TagMap = TagMap;
|
83 | //# sourceMappingURL=tag-map.js.map |
\ | No newline at end of file |