UNPKG

3.62 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.deserializeTextFormat = exports.serializeTextFormat = exports.MAX_NUMBER_OF_TAGS = void 0;
19/**
20 * This module contains the functions for serializing and deserializing
21 * TagMap (TagContext) with W3C Correlation Context as the HTTP text format.
22 * It allows tags to propagate across requests.
23 *
24 * OpenCensus uses W3C Correlation Context as the HTTP text format.
25 * https://github.com/w3c/correlation-context/blob/master/correlation_context/HTTP_HEADER_FORMAT.md
26 */
27const tag_map_1 = require("../tag-map");
28const types_1 = require("../types");
29exports.MAX_NUMBER_OF_TAGS = 180;
30const TAG_SERIALIZED_SIZE_LIMIT = 4096;
31const TAGMAP_SERIALIZED_SIZE_LIMIT = 8192;
32const TAG_KEY_VALUE_DELIMITER = '=';
33const TAG_DELIMITER = ',';
34const UNLIMITED_PROPAGATION_MD = {
35 tagTtl: types_1.TagTtl.UNLIMITED_PROPAGATION,
36};
37/**
38 * Serializes a given TagMap to the on-the-wire format based on the W3C HTTP
39 * text format standard.
40 * @param tagMap The TagMap to serialize.
41 */
42function serializeTextFormat(tagMap) {
43 let ret = '';
44 let totalChars = 0;
45 let totalTags = 0;
46 const tags = tagMap.tagsWithMetadata;
47 tags.forEach((tagsWithMetadata, tagKey) => {
48 if (tagsWithMetadata.tagMetadata.tagTtl !== types_1.TagTtl.NO_PROPAGATION) {
49 if (ret.length > 0)
50 ret += TAG_DELIMITER;
51 totalChars += validateTag(tagKey, tagsWithMetadata.tagValue);
52 ret +=
53 tagKey.name + TAG_KEY_VALUE_DELIMITER + tagsWithMetadata.tagValue.value;
54 totalTags++;
55 }
56 });
57 if (totalTags > exports.MAX_NUMBER_OF_TAGS) {
58 throw new Error(`Number of tags in the TagMap exceeds limit ${exports.MAX_NUMBER_OF_TAGS}`);
59 }
60 if (totalChars > TAGMAP_SERIALIZED_SIZE_LIMIT) {
61 throw new Error(`Size of TagMap exceeds the maximum serialized size ${TAGMAP_SERIALIZED_SIZE_LIMIT}`);
62 }
63 return ret;
64}
65exports.serializeTextFormat = serializeTextFormat;
66/**
67 * Deserializes input to TagMap based on the W3C HTTP text format standard.
68 * @param str The TagMap to deserialize.
69 */
70function deserializeTextFormat(str) {
71 const tags = new tag_map_1.TagMap();
72 if (!str)
73 return tags;
74 const listOfTags = str.split(TAG_DELIMITER);
75 listOfTags.forEach(tag => {
76 const keyValuePair = tag.split(TAG_KEY_VALUE_DELIMITER);
77 if (keyValuePair.length !== 2)
78 throw new Error(`Malformed tag ${tag}`);
79 const [name, value] = keyValuePair;
80 tags.set({ name }, { value }, UNLIMITED_PROPAGATION_MD);
81 });
82 return tags;
83}
84exports.deserializeTextFormat = deserializeTextFormat;
85function validateTag(tagKey, tagValue) {
86 const charsOfTag = tagKey.name.length + tagValue.value.length;
87 if (charsOfTag > TAG_SERIALIZED_SIZE_LIMIT) {
88 throw new Error(`Serialized size of tag exceeds limit ${TAG_SERIALIZED_SIZE_LIMIT}`);
89 }
90 return charsOfTag;
91}
92//# sourceMappingURL=text-format.js.map
\No newline at end of file