Ejemplo n.º 1
0
        // To run this example, run the following command from
        // the reporoot\examples\Console\.
        // (eg: C:\repos\opentelemetry-dotnet\examples\Console\)
        //
        // dotnet run console
        internal static object Run(ConsoleOptions options)
        {
            // Enable TracerProvider for the source "MyCompany.MyProduct.MyWebServer"
            // and use a custom MyProcessor, along with Console exporter.
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddSource("MyCompany.MyProduct.MyWebServer")
                                       .SetResource(Resources.CreateServiceResource("MyServiceName"))
                                       .AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter
                                       .AddConsoleExporter()
                                       .Build();

            // The above line is required only in applications
            // which decide to use OpenTelemetry.

            // Libraries would simply write the following lines of code to
            // emit activities, which are the .NET representation of OpenTelemetry Spans.
            var source = new ActivitySource("MyCompany.MyProduct.MyWebServer");

            // The below commented out line shows more likely code in a real world webserver.
            // using (var parent = source.StartActivity("HttpIn", ActivityKind.Server, HttpContext.Request.Headers["traceparent"] ))
            using (var parent = source.StartActivity("HttpIn", ActivityKind.Server))
            {
                // TagNames can follow the OpenTelemetry guidelines
                // from https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions
                parent?.SetTag("http.method", "GET");
                parent?.SetTag("http.host", "MyHostName");
                if (parent != null)
                {
                    parent.DisplayName = "HttpIn DisplayName";

                    // IsAllDataRequested is the equivalent of Span.IsRecording
                    if (parent.IsAllDataRequested)
                    {
                        parent.SetTag("expensive data", "This data is expensive to obtain. Avoid it if activity is not being recorded");
                    }
                }

                try
                {
                    // Actual code to achieve the purpose of the library.
                    // For websebserver example, this would be calling
                    // user middlware pipeline.

                    // There can be child activities.
                    // In this example HttpOut is a child of HttpIn.
                    using (var child = source.StartActivity("HttpOut", ActivityKind.Client))
                    {
                        child?.SetTag("http.url", "www.mydependencyapi.com");
                        try
                        {
                            // do actual work.

                            child?.AddEvent(new ActivityEvent("sample activity event."));
                            child?.SetTag("http.status_code", "200");
                        }
                        catch (Exception)
                        {
                            child?.SetTag("http.status_code", "500");
                        }
                    }

                    parent?.SetTag("http.status_code", "200");
                }
                catch (Exception)
                {
                    parent?.SetTag("http.status_code", "500");
                }
            }

            System.Console.WriteLine("Press Enter key to exit.");

            return(null);
        }
Ejemplo n.º 2
0
 // To run this example, run the following command from
 // the reporoot\examples\Console\.
 // (eg: C:\repos\opentelemetry-dotnet\examples\Console\)
 //
 // dotnet run console
 internal static object Run(ConsoleOptions options)
 {
     return(RunWithActivitySource());
 }