|
6 | 6 | using System.Text.Json.Nodes; |
7 | 7 | using System.Threading; |
8 | 8 | using System.Threading.Tasks; |
| 9 | +using Arcus.EventGrid.Core; |
9 | 10 | using Arcus.Observability.Correlation; |
10 | 11 | using Arcus.Observability.Telemetry.Core; |
11 | 12 | using Arcus.Security.Core; |
@@ -350,6 +351,7 @@ private async Task<Response> TrackPublishEventAsync<TEvent>(object oneOrManyEven |
350 | 351 |
|
351 | 352 | string dependencyId = Options.GenerateDependencyId(); |
352 | 353 | string transactionId = CorrelationAccessor.GetCorrelationInfo()?.TransactionId; |
| 354 | + |
353 | 355 | oneOrManyEvents = SetCorrelationPropertiesInEvent(oneOrManyEvents, dependencyId, transactionId); |
354 | 356 |
|
355 | 357 | bool isSuccessful = false; |
@@ -410,47 +412,55 @@ private static string DetermineEventType(object @event) |
410 | 412 |
|
411 | 413 | private object SetCorrelationPropertiesInEvent(object @event, string dependencyId, string transactionId) |
412 | 414 | { |
413 | | - if (!Options.EnableDependencyTracking) |
| 415 | + if (Options.Format is EventCorrelationFormat.Hierarchical |
| 416 | + && !Options.EnableDependencyTracking) |
414 | 417 | { |
415 | 418 | return @event; |
416 | 419 | } |
417 | 420 |
|
418 | | - string upstreamServicePropertyName = Options.UpstreamServicePropertyName; |
419 | | - string transactionIdPropertyName = Options.TransactionIdEventDataPropertyName; |
| 421 | + var properties = new Collection<(string propertyName, string propertyValue)>(); |
| 422 | + if (Options.Format is EventCorrelationFormat.Hierarchical) |
| 423 | + { |
| 424 | + properties.Add((Options.TransactionIdEventDataPropertyName, transactionId)); |
| 425 | + properties.Add((Options.UpstreamServicePropertyName, dependencyId)); |
| 426 | + } |
420 | 427 |
|
421 | | - switch (@event) |
| 428 | + if (Options.Format is EventCorrelationFormat.W3C) |
422 | 429 | { |
423 | | - case BinaryData data: |
424 | | - data = SetCorrelationPropertyInCustomEvent(data, upstreamServicePropertyName, dependencyId); |
425 | | - data = SetCorrelationPropertyInCustomEvent(data, transactionIdPropertyName, transactionId); |
426 | | - return data; |
427 | | - case IEnumerable<BinaryData> datas: |
428 | | - datas = SetCorrelationPropertyInCustomEvents(datas, upstreamServicePropertyName, dependencyId); |
429 | | - datas = SetCorrelationPropertyInCustomEvents(datas, transactionIdPropertyName, transactionId); |
430 | | - return datas; |
431 | | - case EventGridEvent ev: |
432 | | - ev = SetCorrelationPropertyInEventGridEvent(ev, upstreamServicePropertyName, dependencyId); |
433 | | - ev = SetCorrelationPropertyInEventGridEvent(ev, transactionIdPropertyName, transactionId); |
434 | | - return ev; |
435 | | - case IEnumerable<EventGridEvent> events: |
436 | | - events = SetCorrelationPropertyInEventGridEvents(events, upstreamServicePropertyName, dependencyId); |
437 | | - events = SetCorrelationPropertyInEventGridEvents(events, transactionIdPropertyName, transactionId); |
438 | | - return events; |
439 | | - case CloudEvent ev: |
440 | | - ev = SetCorrelationPropertyInCloudEvent(ev, upstreamServicePropertyName, dependencyId); |
441 | | - ev = SetCorrelationPropertyInCloudEvent(ev, transactionIdPropertyName, transactionId); |
442 | | - return ev; |
443 | | - case IEnumerable<CloudEvent> events: |
444 | | - events = SetCorrelationPropertyInCloudEvents(events, upstreamServicePropertyName, dependencyId); |
445 | | - events = SetCorrelationPropertyInCloudEvents(events, transactionIdPropertyName, transactionId); |
446 | | - return events; |
447 | | - case ReadOnlyMemory<byte> encodedCloudEvents: |
448 | | - encodedCloudEvents = SetCorrelationPropertyInEncodedCloudEvents(encodedCloudEvents, upstreamServicePropertyName, dependencyId); |
449 | | - encodedCloudEvents = SetCorrelationPropertyInEncodedCloudEvents(encodedCloudEvents, transactionIdPropertyName, transactionId); |
450 | | - return encodedCloudEvents; |
451 | | - default: |
452 | | - throw new ArgumentOutOfRangeException(nameof(@event), @event, "Unknown event type"); |
| 430 | + properties.Add((Options.TraceParentPropertyName, $"00-{transactionId}-{dependencyId}-00")); |
453 | 431 | } |
| 432 | + |
| 433 | + foreach ((string propertyName, string propertyValue) in properties) |
| 434 | + { |
| 435 | + switch (@event) |
| 436 | + { |
| 437 | + case BinaryData data: |
| 438 | + @event = SetCorrelationPropertyInCustomEvent(data, propertyName, propertyValue); |
| 439 | + break; |
| 440 | + case IEnumerable<BinaryData> datas: |
| 441 | + @event = SetCorrelationPropertyInCustomEvents(datas, propertyName, propertyValue); |
| 442 | + break; |
| 443 | + case EventGridEvent ev: |
| 444 | + @event = SetCorrelationPropertyInEventGridEvent(ev, propertyName, propertyValue); |
| 445 | + break; |
| 446 | + case IEnumerable<EventGridEvent> events: |
| 447 | + @event = SetCorrelationPropertyInEventGridEvents(events, propertyName, propertyValue); |
| 448 | + break; |
| 449 | + case CloudEvent ev: |
| 450 | + @event = SetCorrelationPropertyInCloudEvent(ev, propertyName, propertyValue); |
| 451 | + break; |
| 452 | + case IEnumerable<CloudEvent> events: |
| 453 | + @event = SetCorrelationPropertyInCloudEvents(events, propertyName, propertyValue); |
| 454 | + break; |
| 455 | + case ReadOnlyMemory<byte> encodedCloudEvents: |
| 456 | + @event = SetCorrelationPropertyInEncodedCloudEvents(encodedCloudEvents, propertyName, propertyValue); |
| 457 | + break; |
| 458 | + default: |
| 459 | + throw new ArgumentOutOfRangeException(nameof(@event), @event, "Unknown event type"); |
| 460 | + } |
| 461 | + } |
| 462 | + |
| 463 | + return @event; |
454 | 464 | } |
455 | 465 |
|
456 | 466 | private IEnumerable<EventGridEvent> SetCorrelationPropertyInEventGridEvents(IEnumerable<EventGridEvent> eventGridEvents, string propertyName, string dependencyId) |
@@ -632,7 +642,8 @@ protected virtual BinaryData SetCorrelationPropertyInCustomEvent(BinaryData data |
632 | 642 |
|
633 | 643 | private void LogEventGridDependency(string eventType, bool isSuccessful, DurationMeasurement measurement, string dependencyId) |
634 | 644 | { |
635 | | - if (Options.EnableDependencyTracking) |
| 645 | + if (Options.Format is EventCorrelationFormat.Hierarchical |
| 646 | + && Options.EnableDependencyTracking) |
636 | 647 | { |
637 | 648 | Logger.LogDependency( |
638 | 649 | dependencyType: "Azure Event Grid", |
|
0 commit comments