-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathFileBlobProvider.cs
221 lines (197 loc) · 7.33 KB
/
FileBlobProvider.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// <copyright file="FileBlobProvider.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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Timers;
using OpenTelemetry.Extensions.PersistentStorage.Abstractions;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Extensions.PersistentStorage;
/// <summary>
/// Persistent file storage <see cref="FileBlobProvider"/> allows to save data
/// as blobs in file storage.
/// </summary>
public class FileBlobProvider : PersistentBlobProvider, IDisposable
{
private readonly string directoryPath;
private readonly long maxSizeInBytes;
private readonly long retentionPeriodInMilliseconds;
private readonly int writeTimeoutInMilliseconds;
private readonly Timer maintenanceTimer;
private bool disposedValue;
/// <summary>
/// Initializes a new instance of the <see cref="FileBlobProvider"/>
/// class.
/// </summary>
/// <param name="path">
/// Sets file storage folder location where blobs are stored.
/// </param>
/// <param name="maxSizeInBytes">
/// Maximum allowed storage folder size.
/// Default is 50 MB.
/// </param>
/// <param name="maintenancePeriodInMilliseconds">
/// Maintenance event runs at specified interval.
/// Removes expired leases and blobs that exceed retention period.
/// Default is 2 minutes.
/// </param>
/// <param name="retentionPeriodInMilliseconds">
/// Retention period in milliseconds for the blob.
/// Default is 2 days.
/// </param>
/// <param name="writeTimeoutInMilliseconds">
/// Controls the timeout when writing a buffer to blob.
/// Default is 1 minute.
/// </param>
/// <exception cref="ArgumentNullException">
/// path is null.
/// </exception>
/// <exception cref="DirectoryNotFoundException">
/// invalid path.
/// </exception>
/// <exception cref="PathTooLongException">
/// path exceeds system defined maximum length.
/// </exception>
/// <exception cref="UnauthorizedAccessException">
/// insufficient priviledges for provided path.
/// </exception>
/// <exception cref="NotSupportedException">
/// path contains a colon character (:) that is not part of a drive label ("C:\").
/// </exception>
/// <exception cref="ArgumentException">
/// path contains invalid characters.
/// </exception>
/// <exception cref="IOException">
/// path is either file or network name is not known.
/// </exception>
public FileBlobProvider(
string path,
long maxSizeInBytes = 52428800,
int maintenancePeriodInMilliseconds = 120000,
long retentionPeriodInMilliseconds = 172800000,
int writeTimeoutInMilliseconds = 60000)
{
Guard.ThrowIfNull(path);
// TODO: Validate time period values
this.directoryPath = PersistentStorageHelper.CreateSubdirectory(path);
this.maxSizeInBytes = maxSizeInBytes;
this.retentionPeriodInMilliseconds = retentionPeriodInMilliseconds;
this.writeTimeoutInMilliseconds = writeTimeoutInMilliseconds;
this.maintenanceTimer = new Timer(maintenancePeriodInMilliseconds);
this.maintenanceTimer.Elapsed += this.OnMaintenanceEvent;
this.maintenanceTimer.AutoReset = true;
this.maintenanceTimer.Enabled = true;
}
public void Dispose()
{
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
this.maintenanceTimer.Dispose();
}
this.disposedValue = true;
}
}
protected override IEnumerable<PersistentBlob> OnGetBlobs()
{
var retentionDeadline = DateTime.UtcNow - TimeSpan.FromMilliseconds(this.retentionPeriodInMilliseconds);
foreach (var file in Directory.EnumerateFiles(this.directoryPath, "*.blob", SearchOption.TopDirectoryOnly).OrderByDescending(f => f))
{
DateTime fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file);
if (fileDateTime > retentionDeadline)
{
yield return new FileBlob(file);
}
}
}
protected override bool OnTryCreateBlob(byte[] buffer, int leasePeriodMilliseconds, [NotNullWhen(true)] out PersistentBlob blob)
{
blob = this.CreateFileBlob(buffer, leasePeriodMilliseconds);
return blob != null;
}
protected override bool OnTryCreateBlob(byte[] buffer, [NotNullWhen(true)] out PersistentBlob blob)
{
blob = this.CreateFileBlob(buffer);
return blob != null;
}
protected override bool OnTryGetBlob([NotNullWhen(true)] out PersistentBlob blob)
{
blob = this.OnGetBlobs().FirstOrDefault();
return blob != null;
}
private void OnMaintenanceEvent(object source, ElapsedEventArgs e)
{
try
{
if (!Directory.Exists(this.directoryPath))
{
Directory.CreateDirectory(this.directoryPath);
}
}
catch (Exception ex)
{
PersistentStorageEventSource.Log.PersistentStorageException(nameof(FileBlobProvider), $"Error creating directory {this.directoryPath}", ex);
return;
}
PersistentStorageHelper.RemoveExpiredBlobs(this.directoryPath, this.retentionPeriodInMilliseconds, this.writeTimeoutInMilliseconds);
}
private bool CheckStorageSize()
{
var size = PersistentStorageHelper.GetDirectorySize();
if (size >= this.maxSizeInBytes)
{
// TODO: check accuracy of size reporting.
PersistentStorageEventSource.Log.PersistentStorageWarning(
nameof(FileBlobProvider),
$"Persistent storage max capacity has been reached. Currently at {size / 1024} KiB. Please consider increasing the value of storage max size in exporter config.");
return false;
}
return true;
}
private PersistentBlob CreateFileBlob(byte[] buffer, int leasePeriodMilliseconds = 0)
{
if (!this.CheckStorageSize())
{
return null;
}
try
{
var blobFilePath = Path.Combine(this.directoryPath, PersistentStorageHelper.GetUniqueFileName(".blob"));
var blob = new FileBlob(blobFilePath);
if (blob.TryWrite(buffer, leasePeriodMilliseconds))
{
return blob;
}
else
{
return null;
}
}
catch (Exception ex)
{
PersistentStorageEventSource.Log.CouldNotCreateFileBlob(ex);
return null;
}
}
}