private void AppendSampleActivity()

in src/ServiceProfiler.EventPipe.Otel/Azure.Monitor.OpenTelemetry.Profiler.Core/EventListeners/SampleCollector.cs [198:240]


    private void AppendSampleActivity(SampleActivity activity)
    {
        if (activity.IsValid(_logger))
        {
            // Send the AI CustomEvent
            try
            {
                if (SampleActivities.AddSample(activity))
                {
                    if (_logger.IsEnabled(LogLevel.Debug))  // Perf: Avoid serialization when not debugging.
                    {
                        bool isActivitySerialized = _serializer.TrySerialize(activity, out string? serializedActivity);
                        if (isActivitySerialized)
                        {
                            _logger.LogDebug("Sample is added: {0}", serializedActivity);
                        }
                        else
                        {
                            _logger.LogWarning("Serialize failed for activity: {0}", activity?.OperationId);
                        }
                    }
                }
                else
                {
                    _logger.LogError("Fail to add activity into collection. Please making sure there's enough memory.");
                }
            }
            catch (ObjectDisposedException ex)
            {
                // activity builder has been disposed.
                _logger.LogError(ex, "Start activity cache has been disposed before the activity is recorded.");
            }
            catch (InvalidOperationException ex)
            {
                // The underlying collection was modified outside of this BlockingCollection<T> instance.
                _logger.LogError(ex, "Invalid operation on start activity cache. Fail to record the activity.");
            }
        }
        else
        {
            _logger.LogInformation("Target request data is not valid upon receiving requests: {requestId}. This could happen for the first few activities.", activity.RequestId);
        }
    }