Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation/sample for stateful .Net core AI integration #97

Open
DOliana opened this issue Jan 23, 2019 · 5 comments
Open

Add documentation/sample for stateful .Net core AI integration #97

DOliana opened this issue Jan 23, 2019 · 5 comments

Comments

@DOliana
Copy link

DOliana commented Jan 23, 2019

I have a stateful service which listens to an eventhub and processes the messages by sending them to a SQL DB / cosmos DB. So far I was able to integrate AI to the IaaS part and even to write custom events to AI, but I do not get the automatic dependency tracking which should come for SQL/cosmos DB (I have it working in an app service I use). Note: no ServiceIntanceListeners/Webinitialization - so I cannot just register the telemetryprovider in DI.

I assume it has something to do with my configuration. The only way I was able to get anything working was to use (this at least sets the cloud role, so that events tracked by nlog have the correct metadata)

TelemetryConfiguration.Active.InstrumentationKey = "<KEY>"; // I know this is not recommended, but I could not find another solution
TelemetryConfiguration.Active.TelemetryInitializers.Add(FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(this.Context));

I tried the following without luck:

  • adding Applicationinsights.config / .xml to the project (including TelemetryConfiguration.CreateFromConfiguration)
  • adding config.json
  • new project with "add AI wizzard"

Any help on setting up a non-ASP-Net-core service that incldues the automagical dependency tracking would be great!

@yantang-msft
Copy link
Contributor

@DOliana Check out this and see if it helps.

@BoeseB
Copy link

BoeseB commented Feb 19, 2019

Loading the instrumentation key from Applicationinsights.config like its described in the link provided by @yantang-msft does not work for me.
The config file is not included in the service fabric package.

Can someone provide me with some hint how to manage to include the config file to the correct location so AI can load it?
Setting "Copy to output directory" does not do the trick.
The files BuildAction is configured as "Content"

@DOliana is this the same problem you got, that loading the Applicationinsights.config does not work?

Or is this way not recommended for some reason and we should always explicitly set the InstrumentationKey and other config from code?

@yantang-msft
Copy link
Contributor

@BoeseB As documented, "Instructions referring to ApplicationInsights.config are only applicable to apps that are targeting the .NET Framework, and do not apply to .NET Core applications."
The ways of configuration is different for different types of project. What's the type of your project? Asp or non Asp? .net framework or .net core?

@BoeseB
Copy link

BoeseB commented Feb 20, 2019

@yantang-msft Thanks for pointing this out again. Sorry i interpreted the note to apply on Asp.net Core cause most examples are with asp.

My App is a simple non asp stateful service with only a RunAsync Method running at .Net Core.

Here is how I setup AI for now (any suggestions on how to improve are more than welcome):

Initialize in Program.cs Main method

    ServiceRuntime.RegisterServiceAsync(Names.IngestionRouterServiceTypeName,
        context =>
        {
            ConfigureApplicationInsights(context);
            return new RouterService(context);
        }).GetAwaiter().GetResult();
    private static void ConfigureApplicationInsights(ServiceContext context)
    {
         var instrumentationKey =  context.CodePackageActivationContext
                .GetConfigurationPackageObject("config")
                .Settings
                .Sections["ApplicationInsights"]
                .Parameters["InstrumentationKey"]
                .Value;
        var config = TelemetryConfiguration.Active;
        var codePackageVersionTelemetryInitializer = new CodePackageVersionTelemetryInitializer();
        var fabricTelemetryInitializer = FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(context);

        config.InstrumentationKey = instrumentationKey ; 
        config.TelemetryInitializers.Add(codePackageVersionTelemetryInitializer);
        config.TelemetryInitializers.Add(fabricTelemetryInitializer);

        TelemetryModules.Instance.Modules.Add(new ServiceRemotingDependencyTrackingTelemetryModule());
        TelemetryModules.Instance.Modules.Add(new ServiceRemotingRequestTrackingTelemetryModule());
    }

@yantang-msft
Copy link
Contributor

@BoeseB It looks fine overall. The only thing is for the DependencyTracking Module, I have never tried the way you did it here, if it just works then you're good. Otherwise, this is how I initialize it, as documented like here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants