|
| 1 | +// <copyright file="Resource.cs" company="OpenCensus Authors"> |
| 2 | +// Copyright 2018, OpenCensus Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of theLicense at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +namespace OpenCensus.Resources |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Collections.Generic; |
| 21 | + using System.Security; |
| 22 | + using System.Text.RegularExpressions; |
| 23 | + using OpenCensus.Implementation; |
| 24 | + using OpenCensus.Tags; |
| 25 | + using OpenCensus.Utils; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Represents a resource that captures identification information about the entities for which signals (stats or traces) |
| 29 | + /// are reported. It further provides a framework for detection of resource information from the environment and progressive |
| 30 | + /// population as signals propagate from the core instrumentation library to a backend's exporter. |
| 31 | + /// </summary> |
| 32 | + public abstract class Resource : IResource |
| 33 | + { |
| 34 | + /// <summary> |
| 35 | + /// Tag list splitter. |
| 36 | + /// </summary> |
| 37 | + private const char LabelListSplitter = ','; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Key-value splitter. |
| 41 | + /// </summary> |
| 42 | + private const char LabelKeyValueSplitter = '='; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Environment identification (for example, AKS/GKE/etc). |
| 46 | + /// </summary> |
| 47 | + private static readonly string EnvironmentType; |
| 48 | + |
| 49 | + private static readonly ITag[] EnvironmentToLabelMap; |
| 50 | + |
| 51 | + static Resource() |
| 52 | + { |
| 53 | + string openCensusResourceType; |
| 54 | + string openCensusEnvironmentTags; |
| 55 | + |
| 56 | + try |
| 57 | + { |
| 58 | + openCensusResourceType = Environment.GetEnvironmentVariable(Constants.ResourceTypeEnvironmentVariable); |
| 59 | + } |
| 60 | + catch (SecurityException ex) |
| 61 | + { |
| 62 | + openCensusResourceType = Constants.GlobalResourceType; |
| 63 | + |
| 64 | + Log.FailedReadingEnvironmentVariableWarning(Constants.ResourceTypeEnvironmentVariable, ex); |
| 65 | + } |
| 66 | + |
| 67 | + try |
| 68 | + { |
| 69 | + openCensusEnvironmentTags = Environment.GetEnvironmentVariable(Constants.ResourceLabelsEnvironmentVariable); |
| 70 | + } |
| 71 | + catch (SecurityException ex) |
| 72 | + { |
| 73 | + openCensusEnvironmentTags = string.Empty; |
| 74 | + |
| 75 | + Log.FailedReadingEnvironmentVariableWarning(Constants.ResourceLabelsEnvironmentVariable, ex); |
| 76 | + } |
| 77 | + |
| 78 | + TryParseResourceType(openCensusResourceType, out EnvironmentType); |
| 79 | + EnvironmentToLabelMap = ParseResourceLabels(Environment.GetEnvironmentVariable(Constants.ResourceLabelsEnvironmentVariable)); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Gets or sets the identification of the resource. |
| 84 | + /// </summary> |
| 85 | + public abstract string Type { get; protected set; } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Gets the map between the tag and its value. |
| 89 | + /// </summary> |
| 90 | + public abstract IEnumerable<ITag> Tags { get; } |
| 91 | + |
| 92 | + private static OpenCensusEventSource Log => OpenCensusEventSource.Log; |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Creates a label/tag map from the OC_RESOURCE_LABELS environment variable. |
| 96 | + /// OC_RESOURCE_LABELS: A comma-separated list of labels describing the source in more detail, |
| 97 | + /// e.g. “key1=val1,key2=val2”. Domain names and paths are accepted as label keys. |
| 98 | + /// Values may be quoted or unquoted in general. If a value contains whitespaces, =, or " characters, it must |
| 99 | + /// always be quoted. |
| 100 | + /// </summary> |
| 101 | + /// <param name="rawEnvironmentTags">Environment tags as a raw, comma separated string</param> |
| 102 | + /// <returns>Environment Tags as a list.</returns> |
| 103 | + internal static ITag[] ParseResourceLabels(string rawEnvironmentTags) |
| 104 | + { |
| 105 | + if (rawEnvironmentTags == null) |
| 106 | + { |
| 107 | + return new ITag[0] { }; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + var labels = new List<ITag>(); |
| 112 | + string[] rawLabels = rawEnvironmentTags.Split(LabelListSplitter); |
| 113 | + |
| 114 | + Regex regex = new Regex("^\"|\"$", RegexOptions.Compiled); |
| 115 | + |
| 116 | + foreach (var rawLabel in rawLabels) |
| 117 | + { |
| 118 | + string[] keyValuePair = rawLabel.Split(LabelKeyValueSplitter); |
| 119 | + if (keyValuePair.Length != 2) |
| 120 | + { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + string key = keyValuePair[0].Trim(); |
| 125 | + string value = Regex.Replace(keyValuePair[1].Trim(), "^\"|\"$", string.Empty); |
| 126 | + |
| 127 | + if (!IsValidAndNotEmpty(key)) |
| 128 | + { |
| 129 | + Log.InvalidCharactersInResourceElement("Label key"); |
| 130 | + return new ITag[0] { }; |
| 131 | + } |
| 132 | + |
| 133 | + if (!IsValid(value)) |
| 134 | + { |
| 135 | + Log.InvalidCharactersInResourceElement("Label key"); |
| 136 | + return new ITag[0] { }; |
| 137 | + } |
| 138 | + |
| 139 | + labels.Add(new Tag(TagKey.Create(key), TagValue.Create(value))); |
| 140 | + } |
| 141 | + |
| 142 | + return labels.ToArray(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + internal static bool TryParseResourceType(string rawEnvironmentType, out string resourceType) |
| 147 | + { |
| 148 | + if (string.IsNullOrEmpty(rawEnvironmentType)) |
| 149 | + { |
| 150 | + resourceType = Constants.GlobalResourceType; |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + if (rawEnvironmentType.Length > Constants.MaxResourceTypeNameLength) |
| 155 | + { |
| 156 | + Log.InvalidCharactersInResourceElement(rawEnvironmentType); |
| 157 | + resourceType = Constants.GlobalResourceType; |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + resourceType = rawEnvironmentType.Trim(); |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Checks whether given string is a valid printable ASCII string with a length not exeeding |
| 167 | + /// <see cref="Constants.MaxResourceTypeNameLength"/> characters. |
| 168 | + /// </summary> |
| 169 | + /// <param name="name">The string.</param> |
| 170 | + /// <returns>Whether given string is valid.</returns> |
| 171 | + private static bool IsValid(string name) |
| 172 | + { |
| 173 | + return name.Length <= Constants.MaxResourceTypeNameLength && StringUtil.IsPrintableString(name); |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Checks whether given string is a valid printable ASCII string with a length |
| 178 | + /// greater than 0 and not exceeding <see cref="Constants.MaxResourceTypeNameLength"/> characters. |
| 179 | + /// </summary> |
| 180 | + /// <param name="name">The string.</param> |
| 181 | + /// <returns>Whether given string is valid.</returns> |
| 182 | + private static bool IsValidAndNotEmpty(string name) |
| 183 | + { |
| 184 | + return !string.IsNullOrEmpty(name) && IsValid(name); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments