UNPKG

2.8 kBJavaScriptView Raw
1"use strict";
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 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.clear = exports.withTagContext = exports.setCurrentTagContext = exports.getCurrentTagContext = exports.EMPTY_TAG_MAP = void 0;
19const tag_map_1 = require("./tag-map");
20exports.EMPTY_TAG_MAP = new tag_map_1.TagMap();
21const CURRENT_TAG_MAP_KEY = 'current_tag_map';
22/** Gets the current tag context. */
23function getCurrentTagContext(contextManager) {
24 const tagsFromContext = contextManager.get(CURRENT_TAG_MAP_KEY);
25 if (tagsFromContext) {
26 return makeDeepCopy(tagsFromContext);
27 }
28 return new tag_map_1.TagMap();
29}
30exports.getCurrentTagContext = getCurrentTagContext;
31/**
32 * Sets the current tag context.
33 * @param tags The TagMap.
34 */
35function setCurrentTagContext(contextManager, tags) {
36 contextManager.set(CURRENT_TAG_MAP_KEY, makeDeepCopy(tags));
37}
38exports.setCurrentTagContext = setCurrentTagContext;
39/**
40 * Enters the scope of code where the given `TagMap` is in the current context
41 * (replacing the previous `TagMap`).
42 * @param tags The TagMap to be set to the current context.
43 * @param fn Callback function.
44 * @returns The callback return.
45 */
46function withTagContext(contextManager, tags, fn) {
47 const oldContext = getCurrentTagContext(contextManager);
48 return contextManager.runAndReturn(() => {
49 const newContext = new tag_map_1.TagMap();
50 for (const [tagKey, tagValue] of oldContext.tags) {
51 newContext.set(tagKey, tagValue);
52 }
53 for (const [tagKey, tagValue] of tags.tags) {
54 newContext.set(tagKey, tagValue);
55 }
56 setCurrentTagContext(contextManager, newContext);
57 return fn();
58 });
59}
60exports.withTagContext = withTagContext;
61/** Clear the current tag context. */
62function clear(contextManager) {
63 contextManager.set(CURRENT_TAG_MAP_KEY, new tag_map_1.TagMap());
64}
65exports.clear = clear;
66function makeDeepCopy(tags) {
67 const tagsCopy = new tag_map_1.TagMap();
68 for (const [tagKey, valueWithMetadata] of tags.tagsWithMetadata) {
69 tagsCopy.set(tagKey, valueWithMetadata.tagValue, valueWithMetadata.tagMetadata);
70 }
71 return tagsCopy;
72}
73//# sourceMappingURL=tagger.js.map
\No newline at end of file