我们如何在 Service Fabric Actor Reliable 服务中添加 Application Insight
Posted
技术标签:
【中文标题】我们如何在 Service Fabric Actor Reliable 服务中添加 Application Insight【英文标题】:How can we add Application Insight in Service Fabric Actor Reliable service 【发布时间】:2021-01-09 18:13:41 【问题描述】:我们有 5 个可靠的参与者服务,它们是从无状态的 aspnet 核心 web api 调用的。 现在,Service Fabric 应用程序正在生产中运行,但作为迁移到 Azure 的一部分,我们希望所有自定义事件和跟踪到应用程序洞察力。
我们如何将相同的内容添加到 SF?请注意,我正在寻找可靠的演员服务。在可靠服务中添加它有点简单,但我在 Actor 服务方面面临挑战。
我参考了这个 https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-tutorial-monitoring-aspnet 教程以获得可靠的服务,但在使用 .NET Framework 编写的可靠参与者服务的情况下同样不起作用。
【问题讨论】:
请检查这里提到的例子是否有帮助:github.com/microsoft/ApplicationInsights-ServiceFabric/issues/… 【参考方案1】:引用此开放 GitHub 问题中的示例:Add documentation for configure actor services
设置服务上下文
-
创建您自己的actor服务类并在那里初始化上下文:
internal class MyActorService : ActorService
public MyActorService(
StatefulServiceContext context,
ActorTypeInformation actorTypeInfo,
Func<ActorService, ActorId, ActorBase> actorFactory = null,
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
IActorStateProvider stateProvider = null,
ActorServiceSettings settings = null)
: base(context, actorTypeInfo, actorFactory, stateManagerFactory, stateProvider, settings)
FabricTelemetryInitializerExtension.SetServiceCallContext(this.Context);
-
注册actor服务如下:
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new MyActorService(context, actorType)).GetAwaiter().GetResult();
为服务远程处理启用关联
首先,确保 Service Remoting 设置正确。您可以找到更多信息here。
远程处理 V2(推荐)
初始化服务远程依赖/请求跟踪模块,如下所示。
注意:您也可以通过CreateFabricTelemetryInitializer
方法设置服务上下文。在这种情况下,您无需致电SetServiceCallContext
public MyActorServiceNetCore(
StatefulServiceContext context,
ActorTypeInformation actorTypeInfo,
Func<ActorService, ActorId, ActorBase> actorFactory = null,
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
IActorStateProvider stateProvider = null,
ActorServiceSettings settings = null)
: base(context, actorTypeInfo, actorFactory, stateManagerFactory, stateProvider, settings)
var config = TelemetryConfiguration.Active;
config.InstrumentationKey = "<your ikey>";
config.TelemetryInitializers.Add(FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(this.Context));
var requestTrackingModule = new ServiceRemotingRequestTrackingTelemetryModule();
var dependencyTrackingModule = new ServiceRemotingDependencyTrackingTelemetryModule();
requestTrackingModule.Initialize(config);
dependencyTrackingModule.Initialize(config);
远程 V1
如果您仍想使用 Service Remoting V1 并跟踪相关性,可以按照以下说明进行操作,但强烈建议您升级到 Remoting V2。
-
在发件人端创建如下代理
//IActorService actorServiceProxy = ActorServiceProxy.Create(new Uri(serviceUri), partitionKey);
CorrelatingActorProxyFactory actorProxyFactory = new CorrelatingActorProxyFactory(serviceContext, callbackClient => new FabricTransportActorRemotingClientFactory(callbackClient));
IActorService actorServiceProxy = actorProxyFactory.CreateActorServiceProxy<IActorService>(new Uri(serviceUri), partitionKey);
-
像下面这样初始化监听器:
internal class MyActorService : ActorService
public MyActorService(
StatefulServiceContext context,
ActorTypeInformation actorTypeInfo,
Func<ActorService, ActorId, ActorBase> actorFactory = null,
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
IActorStateProvider stateProvider = null,
ActorServiceSettings settings = null)
: base(context, actorTypeInfo, actorFactory, stateManagerFactory, stateProvider, settings)
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
return new[]
new ServiceReplicaListener(
context => new FabricTransportActorServiceRemotingListener(
context,
new CorrelatingRemotingMessageHandler(this),
new FabricTransportRemotingListenerSettings()))
;
-
注册您自己的演员服务:
// ActorRuntime.RegisterActorAsync<MyActor>(
// (context, actorType) => new ActorService(context, actorType)).GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new MyActorService(context, actorType)).GetAwaiter().GetResult();
最初由 @yantang-msft 发表于 https://github.com/microsoft/ApplicationInsights-ServiceFabric/issue_comments/434430800
其他资源:service-fabric-application-insights-example
【讨论】:
以上是关于我们如何在 Service Fabric Actor Reliable 服务中添加 Application Insight的主要内容,如果未能解决你的问题,请参考以下文章
Service Fabric - 如何修复失败的有状态应用程序