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

How to create traces in Console App #5018

Closed
BTXAndy opened this issue Jun 3, 2020 · 5 comments
Closed

How to create traces in Console App #5018

BTXAndy opened this issue Jun 3, 2020 · 5 comments
Assignees
Labels
api: cloudtrace Issues related to the Cloud Trace API. type: docs Improvement to the documentation for an API.

Comments

@BTXAndy
Copy link

BTXAndy commented Jun 3, 2020

Hi,

Is there a sample somewhere that shows how to create traces outside of ASP.NET? If not, can you describe what would be required?

(We have containers that run background jobs and it would be good to trace these as well as our asp.net core services)

thanks

@jskeet
Copy link
Collaborator

jskeet commented Jun 3, 2020

I don't believe we have any samples for that, no. Assigning to Amanda, who can hopefully give you some pointers, but I wouldn't expect full code samples or anything like that - at least not for the moment.

@jskeet jskeet added the type: question Request for information or clarification. Not an issue. label Jun 3, 2020
@amanda-tarafa amanda-tarafa added api: cloudtrace Issues related to the Cloud Trace API. type: docs Improvement to the documentation for an API. and removed type: question Request for information or clarification. Not an issue. labels Jun 3, 2020
@amanda-tarafa
Copy link
Contributor

We don't have samples for that but it shouldn't be hard to accomplish.
Take a look at SimpleManagedTracerTests for how to create traces from other than ASP.NET.
Since you won't have a middleware to create the tracer for you, you'll have to do that yourself, but you can use the functions in ManagedTracer to do that.

Let me know if this doesn't help (or if there's more demand for it) and I can add a couple of samples.

@amanda-tarafa
Copy link
Contributor

@BTXAndy I will close this issue now since I belive I've answered your question. Please leave a comment if you believe otherwirse or a new issue if you encounter problems using the approach I suggested.

@tomislavpet
Copy link

@amanda-tarafa Can you please check out this example? Tracer gets injected into the service, but nothing gets sent to Google Cloud. Logging works fine and is visible on GC.

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Google.Cloud.Diagnostics.Common;
using Google.Cloud.Diagnostics.AspNetCore;
using Microsoft.Extensions.Configuration;

namespace Example
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var services = new ServiceCollection();
            var serviceProvider = ConfigureServices(services);

            var app = serviceProvider.GetService<MyService>();

            for (int i = 0; i < 20; i++)
            {
                app.Doit();
            }

            Console.WriteLine("Waiting 10 sec");

            Task.Delay(10000).Wait();

            Console.WriteLine("Done");
        }

        public static IServiceProvider ConfigureServices(IServiceCollection serviceCollection)
        {
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var provider = serviceProvider.GetService<ILoggerProvider>();

            IConfiguration config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", true, true)
                .Build();

            serviceCollection
                .AddOptions()
                .Configure<StackdriverOptions>(config.GetSection("Stackdriver"))
                .AddLogging(opt => { opt.AddProvider(GoogleLoggerProvider.Create(null, config["Stackdriver:ProjectId"])).AddConsole().AddDebug(); })
                .AddGoogleTrace(options =>
                {
                    options.ProjectId = config["Stackdriver:ProjectId"];
                    options.Options = TraceOptions.Create(
                        retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate),
                        bufferOptions: BufferOptions.NoBuffer(),
                        qpsSampleRate: double.MaxValue
                    );
                })
                .AddSingleton<MyService>();

            serviceProvider = serviceCollection.BuildServiceProvider();

            return serviceProvider;
        }
    }

    public class StackdriverOptions
    {
        public string ProjectId { get; set; }
        public string ServiceName { get; set; }
        public string Version { get; set; }
    }

    public class MyService
    {
        private ILogger m_Logger;
        private IManagedTracer m_Tracer;

        public MyService(ILogger<MyService> logger, IManagedTracer tracer)
        {
            m_Logger = logger;
            m_Tracer = tracer;
        }

        public void Doit()
        {
            using (m_Tracer.StartSpan("test trace"))
            {
                Console.WriteLine("MyService.Doit");

                m_Logger.LogInformation("Doing it!");
                m_Logger.LogWarning("Warning!");
                m_Logger.LogError("Here's an error!");

                Task.Delay(500).Wait();

                Console.WriteLine("MyService.Doit");
            }
        }
    }
}

@amanda-tarafa
Copy link
Contributor

@tomislavpet please do create a new issue, (and maybe reference this one).
And to set some expectations I won't be able to get to this until next week at the very least. You can fork the repo and debug your code against our library code so you can see which bits you are missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudtrace Issues related to the Cloud Trace API. type: docs Improvement to the documentation for an API.
Projects
None yet
Development

No branches or pull requests

4 participants