引用此开放 GitHub 问题中的示例:为配置参与者服务添加文档
设置服务上下文
- 创建您自己的演员服务类并在那里初始化上下文:
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);
}
}
- 注册演员服务,如下所示:
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new MyActorService(context, actorType)).GetAwaiter().GetResult();
为服务远程启用关联
首先,确保 Service Remoting 设置正确。你可以在这里找到更多信息。
远程处理 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