Skip to content

Commit eecee5b

Browse files
authored
Refactored logic for tracking dropped items from storage (#42542)
* Refactored logic for tracking dropped items from storage * Updated CHANGELOG
1 parent 3ae7c44 commit eecee5b

File tree

4 files changed

+455
-106
lines changed

4 files changed

+455
-106
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
([#42382](https://github.com/Azure/azure-sdk-for-python/pull/42382))
88
- Refactored the put methods in storage.py for LocalFileBlob and LocalFileStorage
99
([#42502](https://github.com/Azure/azure-sdk-for-python/pull/42502))
10+
- Customer Facing Statsbeat: Refactored logic for tracking dropped items from storage
11+
([#42542](https://github.com/Azure/azure-sdk-for-python/pull/42542))
1012

1113
### Breaking Changes
1214

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,21 @@
4646
)
4747
# from azure.monitor.opentelemetry.exporter._configuration import _ConfigurationManager
4848
from azure.monitor.opentelemetry.exporter._connection_string_parser import ConnectionStringParser
49-
from azure.monitor.opentelemetry.exporter._storage import (
50-
LocalFileStorage,
51-
StorageExportResult,
52-
)
49+
from azure.monitor.opentelemetry.exporter._storage import LocalFileStorage
5350
from azure.monitor.opentelemetry.exporter._utils import _get_auth_policy
5451
from azure.monitor.opentelemetry.exporter.statsbeat._state import (
5552
get_statsbeat_initial_success,
5653
get_statsbeat_shutdown,
5754
increment_and_check_statsbeat_failure_count,
5855
is_statsbeat_enabled,
5956
set_statsbeat_initial_success,
60-
get_local_storage_setup_state_exception,
6157
)
6258
from azure.monitor.opentelemetry.exporter.statsbeat._utils import (
6359
_update_requests_map,
6460
_track_dropped_items,
6561
_track_retry_items,
6662
_track_successful_items,
63+
_track_dropped_items_from_storage,
6764
)
6865

6966

@@ -207,24 +204,7 @@ def _handle_transmit_from_storage(self, envelopes: List[TelemetryItem], result:
207204
envelopes_to_store = [x.as_dict() for x in envelopes]
208205
result_from_storage_put = self.storage.put(envelopes_to_store)
209206
if self._customer_statsbeat_metrics and self._should_collect_customer_statsbeat():
210-
if result_from_storage_put == StorageExportResult.CLIENT_STORAGE_DISABLED:
211-
# Track items that would have been retried but are dropped since client has local storage disabled
212-
_track_dropped_items(self._customer_statsbeat_metrics, envelopes, DropCode.CLIENT_STORAGE_DISABLED)
213-
elif result_from_storage_put == StorageExportResult.CLIENT_READONLY:
214-
# If filesystem is readonly, track dropped items in customer statsbeat
215-
_track_dropped_items(self._customer_statsbeat_metrics, envelopes, DropCode.CLIENT_READONLY)
216-
elif result_from_storage_put == StorageExportResult.CLIENT_PERSISTENCE_CAPACITY_REACHED:
217-
# If data has to be dropped due to persistent storage being full, track dropped items
218-
_track_dropped_items(self._customer_statsbeat_metrics, envelopes, DropCode.CLIENT_PERSISTENCE_CAPACITY)
219-
elif get_local_storage_setup_state_exception() != "":
220-
# For exceptions caught in _check_and_set_folder_permissions during storage setup
221-
_track_dropped_items(self._customer_statsbeat_metrics, envelopes, DropCode.CLIENT_EXCEPTION, result_from_storage_put)
222-
elif isinstance(result_from_storage_put, str):
223-
# For any exceptions occurred in put method of either LocalFileStorage or LocalFileBlob, track dropped item with reason
224-
_track_dropped_items(self._customer_statsbeat_metrics, envelopes, DropCode.CLIENT_EXCEPTION, result_from_storage_put)
225-
else:
226-
# LocalFileBlob.put returns StorageExportResult.LOCAL_FILE_BLOB_SUCCESS here. Don't need to track anything in this case.
227-
pass
207+
_track_dropped_items_from_storage(self._customer_statsbeat_metrics, result_from_storage_put, envelopes)
228208
elif result == ExportResult.SUCCESS:
229209
# Try to send any cached events
230210
self._transmit_from_storage()
@@ -295,7 +275,9 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
295275
)
296276
if self.storage and resend_envelopes:
297277
envelopes_to_store = [x.as_dict() for x in resend_envelopes]
298-
self.storage.put(envelopes_to_store, 0)
278+
result_from_storage = self.storage.put(envelopes_to_store, 0)
279+
if self._customer_statsbeat_metrics and self._should_collect_customer_statsbeat():
280+
_track_dropped_items_from_storage(self._customer_statsbeat_metrics, result_from_storage, resend_envelopes)
299281
self._consecutive_redirects = 0
300282
elif resend_envelopes:
301283
# Track items that would have been retried but are dropped since client has local storage disabled

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
RetryCode,
88
RetryCodeType,
99
DropCodeType,
10+
DropCode,
1011
_UNKNOWN,
1112
)
1213
from azure.monitor.opentelemetry.exporter._utils import _get_telemetry_type
1314
from azure.monitor.opentelemetry.exporter._generated.models import TelemetryItem
15+
from azure.monitor.opentelemetry.exporter._storage import StorageExportResult
16+
from azure.monitor.opentelemetry.exporter.statsbeat._state import get_local_storage_setup_state_exception
1417

1518

1619
from azure.monitor.opentelemetry.exporter._constants import (
@@ -178,3 +181,24 @@ def _track_retry_items(customer_statsbeat_metrics, envelopes: List[TelemetryItem
178181
retry_code,
179182
str(message)
180183
)
184+
185+
def _track_dropped_items_from_storage(customer_statsbeat_metrics, result_from_storage_put, envelopes):
186+
if customer_statsbeat_metrics:
187+
if result_from_storage_put == StorageExportResult.CLIENT_STORAGE_DISABLED:
188+
# Track items that would have been retried but are dropped since client has local storage disabled
189+
_track_dropped_items(customer_statsbeat_metrics, envelopes, DropCode.CLIENT_STORAGE_DISABLED)
190+
elif result_from_storage_put == StorageExportResult.CLIENT_READONLY:
191+
# If filesystem is readonly, track dropped items in customer statsbeat
192+
_track_dropped_items(customer_statsbeat_metrics, envelopes, DropCode.CLIENT_READONLY)
193+
elif result_from_storage_put == StorageExportResult.CLIENT_PERSISTENCE_CAPACITY_REACHED:
194+
# If data has to be dropped due to persistent storage being full, track dropped items
195+
_track_dropped_items(customer_statsbeat_metrics, envelopes, DropCode.CLIENT_PERSISTENCE_CAPACITY)
196+
elif get_local_storage_setup_state_exception() != "":
197+
# For exceptions caught in _check_and_set_folder_permissions during storage setup
198+
_track_dropped_items(customer_statsbeat_metrics, envelopes, DropCode.CLIENT_EXCEPTION, result_from_storage_put) # pylint: disable=line-too-long
199+
elif isinstance(result_from_storage_put, str):
200+
# For any exceptions occurred in put method of either LocalFileStorage or LocalFileBlob, track dropped item with reason # pylint: disable=line-too-long
201+
_track_dropped_items(customer_statsbeat_metrics, envelopes, DropCode.CLIENT_EXCEPTION, result_from_storage_put) # pylint: disable=line-too-long
202+
else:
203+
# LocalFileBlob.put returns StorageExportResult.LOCAL_FILE_BLOB_SUCCESS here. Don't need to track anything in this case. # pylint: disable=line-too-long
204+
pass

0 commit comments

Comments
 (0)