Skip to content

Question: How to store Activity.Id before start it? #2213

@shawnzxx

Description

@shawnzxx

Context:

I have a special requirement to store "dependencyActivity.id" in a transaction, see a line of code attached below:
await SaveOutgoingEventAsync(String.Empty);
I am currently storing an empty string as a temp version of dependencyActivity.id, and update it to the actual value of dependencyActivity.Id after I start the dependencyActivity. Use this way to pass down the parent and child relationship to the next external process, dependencyActivity.Id as my downstream service's operationParentId.

Question:

1: What is the best practice to store dependent id before I start and stop telemetry operation?

Code:

public async Task<ReturnOrderDto> SubmitOrderAsync(SubmitOrderDto order)
{
    var executionStrategy = _orderDbContext.Database.CreateExecutionStrategy();
    await executionStrategy.ExecuteAsync(async () =>
    {
        using (var transaction = _orderDbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted))
        {
            //Some processing code
            ......
            ......

            //Store empty string as dependencyActivity.Id in database
            await SaveOutgoingEventAsync(String.Empty);
            // Commit incoming, business and outgoing entities together in outbox pattern
            await transaction.CommitAsync();
        }
    });

    var dependencyActivity = new Activity("Publish Message");
    dependencyActivity.SetParentId(Activity.Current.Id);
    dependencyActivity.Start();
    var operation = _telemetryClient.StartOperation<DependencyTelemetry>(dependencyActivity);
    var messageHeader = new KafkaMessageHeader(dependencyActivity.TraceId.ToString(), dependencyActivity.Id);
    kafkaMessage.UpdateHeader(messageHeader);
    await _publisher.ProduceAsync(kafkaMessage);
    _telemetryClient.StopOperation(operation);
    
    //update previous stored empty Id to dependencyActivity.Id
    outgoingEvent.UpdateOutgoingIntegrationEvent(dependencyActivity.TraceId.ToString(), dependencyActivity.Id);
    outgoingEvent.MarkAsSent();
    await _outgoingEventRepository.SaveChangesAsync();

    return new ReturnOrderDto { OrderId = _orderEntity.OrderId };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Diagnostics-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.trackingThis issue is tracking the completion of other related issues.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions