-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: fix delivering traceId from scheduler (#344)
* fix: delivering traceid from scheduler to alarm * chore: update
- Loading branch information
1 parent
a70ae36
commit bc556ab
Showing
7 changed files
with
101 additions
and
34 deletions.
There are no files selected for viewing
28 changes: 18 additions & 10 deletions
28
src/Application/Masa.Alert.Application/AlarmRules/Jobs/CheckAlarmRuleJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the Apache License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Alert.Application.AlarmRules.Jobs | ||
namespace Masa.Alert.Application.AlarmRules.Jobs; | ||
|
||
public class CheckAlarmRuleJob : BackgroundJobBase<CheckAlarmRuleJobArgs> | ||
{ | ||
public class CheckAlarmRuleJob : BackgroundJobBase<CheckAlarmRuleJobArgs> | ||
private readonly IEventBus _eventBus; | ||
public static ActivitySource ActivitySource { get; private set; } = new("Masa.Alert.Background"); | ||
|
||
public CheckAlarmRuleJob(ILogger<BackgroundJobBase<CheckAlarmRuleJobArgs>>? logger | ||
, IEventBus eventBus) : base(logger) | ||
{ | ||
private readonly IEventBus _eventBus; | ||
_eventBus = eventBus; | ||
} | ||
|
||
public CheckAlarmRuleJob(ILogger<BackgroundJobBase<CheckAlarmRuleJobArgs>>? logger | ||
, IEventBus eventBus) : base(logger) | ||
protected async override Task ExecutingAsync(CheckAlarmRuleJobArgs args) | ||
{ | ||
var command = new CheckAlarmRuleCommand(args.AlarmRuleId, args.ExcuteTime); | ||
var activity = string.IsNullOrEmpty(args.TraceParent) ? default : ActivitySource.StartActivity("", ActivityKind.Consumer, args.TraceParent); | ||
try | ||
{ | ||
_eventBus = eventBus; | ||
await _eventBus.PublishAsync(command); | ||
} | ||
|
||
protected override async Task ExecutingAsync(CheckAlarmRuleJobArgs args) | ||
finally | ||
{ | ||
var command = new CheckAlarmRuleCommand(args.AlarmRuleId, args.ExcuteTime); | ||
await _eventBus.PublishAsync(command); | ||
activity?.Dispose(); | ||
} | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
src/Application/Masa.Alert.Application/AlarmRules/Jobs/CheckAlarmRuleJobArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the Apache License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Alert.Application.AlarmRules.Jobs | ||
namespace Masa.Alert.Application.AlarmRules.Jobs; | ||
|
||
public class CheckAlarmRuleJobArgs | ||
{ | ||
public class CheckAlarmRuleJobArgs | ||
{ | ||
public Guid AlarmRuleId { get; set; } | ||
public Guid AlarmRuleId { get; set; } | ||
|
||
public DateTimeOffset? ExcuteTime { get; set; } | ||
|
||
public DateTimeOffset? ExcuteTime { get; set; } | ||
} | ||
public string? TraceParent { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Services/Masa.Alert.Service/Extensions/OpenTelemetryAlertExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the Apache License. See LICENSE.txt in the project root for license information. | ||
|
||
using OpenTelemetry.Logs; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class OpenTelemetryAlertExtensions | ||
{ | ||
public static IServiceCollection AddAlertObservable(this IServiceCollection services, | ||
ILoggingBuilder loggingBuilder, | ||
Func<MasaObservableOptions> optionsConfigure, | ||
Func<string>? otlpUrlConfigure = null) | ||
{ | ||
var options = optionsConfigure(); | ||
var otlpUrl = otlpUrlConfigure?.Invoke() ?? string.Empty; | ||
|
||
ArgumentNullException.ThrowIfNull(options); | ||
|
||
Uri? uri = null; | ||
if (!string.IsNullOrEmpty(otlpUrl) && !Uri.TryCreate(otlpUrl, UriKind.Absolute, out uri)) | ||
throw new UriFormatException($"{nameof(otlpUrl)}:{otlpUrl} is invalid url"); | ||
|
||
var resources = ResourceBuilder.CreateDefault().AddMasaService(options); | ||
loggingBuilder.AddMasaOpenTelemetry(builder => | ||
{ | ||
builder.SetResourceBuilder(resources); | ||
builder.AddOtlpExporter(otlp => otlp.Endpoint = uri); | ||
}); | ||
|
||
services.AddMasaMetrics(builder => | ||
{ | ||
builder.SetResourceBuilder(resources); | ||
builder.AddOtlpExporter(options => | ||
{ | ||
options.Endpoint = uri; | ||
}); | ||
}); | ||
|
||
services.AddMasaTracing( | ||
builder => | ||
{ | ||
builder.SetResourceBuilder(resources); | ||
builder.AddOtlpExporter(options => options.Endpoint = uri); | ||
builder.AddSource(CheckAlarmRuleJob.ActivitySource.Name); | ||
}, | ||
builder => | ||
{ | ||
builder.AspNetCoreInstrumentationOptions.AppendDefaultFilter(builder, false); | ||
}); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters