-
Notifications
You must be signed in to change notification settings - Fork 765
/
EnvironmentVariableHelper.cs
143 lines (127 loc) · 5.41 KB
/
EnvironmentVariableHelper.cs
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
// <copyright file="EnvironmentVariableHelper.cs" company="OpenTelemetry Authors">
// 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
//
// http://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.
// </copyright>
using System;
using System.Globalization;
using System.Security;
namespace OpenTelemetry.Internal
{
/// <summary>
/// EnvironmentVariableHelper facilitates parsing environment variable values as defined by
/// <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md">
/// the specification</a>.
/// </summary>
internal static class EnvironmentVariableHelper
{
/// <summary>
/// Reads an environment variable without any parsing.
/// </summary>
/// <param name="envVarKey">The name of the environment variable.</param>
/// <param name="result">The parsed value of the environment variable.</param>
/// <returns>
/// Returns <c>true</c> when a non-empty value was read; otherwise, <c>false</c>.
/// </returns>
public static bool LoadString(string envVarKey, out string result)
{
result = null;
try
{
result = Environment.GetEnvironmentVariable(envVarKey);
}
catch (SecurityException ex)
{
// The caller does not have the required permission to
// retrieve the value of an environment variable from the current process.
OpenTelemetrySdkEventSource.Log.MissingPermissionsToReadEnvironmentVariable(ex);
return false;
}
return !string.IsNullOrEmpty(result);
}
/// <summary>
/// Reads an environment variable and parses is as a
/// <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#numeric-value">
/// numeric value</a> - a non-negative decimal integer.
/// </summary>
/// <param name="envVarKey">The name of the environment variable.</param>
/// <param name="result">The parsed value of the environment variable.</param>
/// <returns>
/// Returns <c>true</c> when a non-empty value was read; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="FormatException">
/// Thrown when failed to parse the non-empty value.
/// </exception>
public static bool LoadNumeric(string envVarKey, out int result)
{
result = 0;
if (!LoadString(envVarKey, out string value))
{
return false;
}
if (!int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out result))
{
throw new FormatException($"{envVarKey} environment variable has an invalid value: '{value}'");
}
return true;
}
/// <summary>
/// Reads an environment variable and parses it as a <see cref="Uri" />.
/// </summary>
/// <param name="envVarKey">The name of the environment variable.</param>
/// <param name="result">The parsed value of the environment variable.</param>
/// <returns>
/// Returns <c>true</c> when a non-empty value was read; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="FormatException">
/// Thrown when failed to parse the non-empty value.
/// </exception>
public static bool LoadUri(string envVarKey, out Uri result)
{
result = null;
if (!LoadString(envVarKey, out string value))
{
return false;
}
if (!Uri.TryCreate(value, UriKind.Absolute, out result))
{
throw new FormatException($"{envVarKey} environment variable has an invalid value: '${value}'");
}
return true;
}
/// <summary>
/// Reads an environment variable and parses it as a <see cref="bool" />.
/// </summary>
/// <param name="envVarKey">The name of the environment variable.</param>
/// <param name="result">The parsed value of the environment variable.</param>
/// <returns>
/// Returns <c>true</c> when a non-empty value was read; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="FormatException">
/// Thrown when failed to parse the non-empty value.
/// </exception>
public static bool LoadBoolean(string envVarKey, out bool result)
{
result = default;
if (!LoadString(envVarKey, out string value))
{
return false;
}
if (!bool.TryParse(value, out result))
{
throw new FormatException($"{envVarKey} environment variable has an invalid value: '${value}'");
}
return true;
}
}
}