Skip to content

Commit

Permalink
Fix: fix delivering traceId from scheduler (#344)
Browse files Browse the repository at this point in the history
* fix: delivering traceid from scheduler to alarm

* chore: update
  • Loading branch information
Qinyouzeng authored May 8, 2024
1 parent a70ae36 commit bc556ab
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 34 deletions.
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();
}
}
}
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; }
}
16 changes: 8 additions & 8 deletions src/Application/Masa.Alert.Application/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

global using System.Collections.Concurrent;
global using System.Diagnostics;
global using System.Linq.Expressions;
global using System.Net.Http;
global using System.Text;
global using System.Text.Json;
global using FluentValidation;
global using Mapster;
global using Masa.Alert.Application.AlarmHistories.Commands;
global using Masa.Alert.Application.AlarmRules.Commands;
global using Masa.Alert.Application.Contracts.AlarmHistories.Dtos;
global using Masa.Alert.Application.Contracts.AlarmRules.Dtos;
global using Masa.Alert.Application.Contracts.AlarmRules.Validator;
Expand All @@ -26,14 +28,19 @@
global using Masa.Alert.Domain.NotificationService;
global using Masa.Alert.Domain.Shared.AlarmHistory;
global using Masa.Alert.Domain.Shared.AlarmRules;
global using Masa.Alert.Domain.Shared.Consts;
global using Masa.Alert.Domain.WebHooks.Aggregates;
global using Masa.Alert.Domain.WebHooks.Events;
global using Masa.Alert.Domain.WebHooks.Repositories;
global using Masa.Alert.Infrastructure.Constants;
global using Masa.Alert.Infrastructure.Ddd.Application.Contracts.Dtos;
global using Masa.BuildingBlocks.Authentication.Identity;
global using Masa.BuildingBlocks.Caching;
global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.Ddd.Domain.Events;
global using Masa.BuildingBlocks.Data.Contracts;
global using Masa.BuildingBlocks.Ddd.Domain.Events;
global using Masa.BuildingBlocks.Dispatcher.Events;
global using Masa.BuildingBlocks.Extensions.BackgroundJobs;
global using Masa.BuildingBlocks.Globalization.I18n;
global using Masa.BuildingBlocks.ReadWriteSplitting.Cqrs.Commands;
global using Masa.BuildingBlocks.ReadWriteSplitting.Cqrs.Queries;
Expand All @@ -51,10 +58,3 @@
global using Masa.Contrib.StackSdks.Config;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.Logging;
global using Masa.Alert.Infrastructure.Constants;
global using Masa.BuildingBlocks.Dispatcher.Events;
global using Masa.BuildingBlocks.Caching;
global using Masa.Alert.Domain.Shared.Consts;
global using Masa.BuildingBlocks.Extensions.BackgroundJobs;
global using Masa.Alert.Application.AlarmRules.Commands;
global using Masa.BuildingBlocks.StackSdks.Tsc.Contracts.Log;
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;
}
}
2 changes: 1 addition & 1 deletion src/Services/Masa.Alert.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
await builder.Services.AddMasaStackConfigAsync(MasaStackProject.Alert, MasaStackApp.Service);
var masaStackConfig = builder.Services.GetMasaStackConfig();

builder.Services.AddObservable(builder.Logging, () =>
builder.Services.AddAlertObservable(builder.Logging, () =>
{
return new MasaObservableOptions
{
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Masa.Alert.Service/Services/AlarmRuleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public async Task CheckAsync(Guid id, DateTimeOffset? excuteTime)
var args = new CheckAlarmRuleJobArgs()
{
AlarmRuleId = id,
ExcuteTime = excuteTime
ExcuteTime = excuteTime,
TraceParent = Activity.Current?.Id
};

await BackgroundJobManager.EnqueueAsync(args);
}
}
15 changes: 8 additions & 7 deletions src/Services/Masa.Alert.Service/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

global using System.Diagnostics;
global using System.Linq;
global using System.Text.RegularExpressions;
global using FluentValidation;
global using Mapster;
global using Masa.Alert.Application.AlarmHistories.Commands;
global using Masa.Alert.Application.AlarmHistories.Queries;
global using Masa.Alert.Application.AlarmRules.Commands;
global using Masa.Alert.Application.AlarmRules.Jobs;
global using Masa.Alert.Application.AlarmRules.Queries;
global using Masa.Alert.Application.Contracts.AlarmHistories.Dtos;
global using Masa.Alert.Application.Contracts.AlarmRules.Dtos;
Expand All @@ -19,26 +21,25 @@
global using Masa.Alert.EntityFrameworkCore;
global using Masa.Alert.Infrastructure.Common.Extensions;
global using Masa.Alert.Infrastructure.Ddd.Application.Contracts.Dtos;
global using Masa.Alert.Infrastructure.Middleware;
global using Masa.Alert.NotificationService.Provider.Mc;
global using Masa.Alert.Service.Admin.Internal;
global using Masa.BuildingBlocks.Data.UoW;
global using Masa.BuildingBlocks.Ddd.Domain.Repositories;
global using Masa.BuildingBlocks.Dispatcher.Events;
global using Masa.BuildingBlocks.Dispatcher.IntegrationEvents;
global using Masa.BuildingBlocks.Extensions.BackgroundJobs;
global using Masa.BuildingBlocks.RulesEngine;
global using Masa.BuildingBlocks.StackSdks.Auth.Contracts;
global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts;
global using Masa.Contrib.Caching.Distributed.StackExchangeRedis;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc;
global using Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EFCore;
global using Masa.Contrib.StackSdks.Config;
global using Masa.Contrib.StackSdks.Isolation;
global using Masa.Contrib.StackSdks.Middleware;
global using Masa.Contrib.StackSdks.Tsc;
global using Microsoft.AspNetCore.Authentication.JwtBearer;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.OpenApi.Models;
global using Masa.Contrib.StackSdks.Middleware;
global using Masa.BuildingBlocks.Data.UoW;
global using Masa.Contrib.StackSdks.Isolation;
global using Masa.Alert.Infrastructure.Middleware;
global using Masa.BuildingBlocks.Extensions.BackgroundJobs;
global using Masa.Alert.Application.AlarmRules.Jobs;
global using Microsoft.OpenApi.Models;

0 comments on commit bc556ab

Please sign in to comment.