예제 #1
0
        private SplunkTracingHttpClient GetClient(TransportOptions t = TransportOptions.JsonHttp)
        {
            var collectorOptions = new CollectorOptions("localhost", 8088, true);
            var tracerOptions    = new Options("TEST").WithCollector(collectorOptions).WithAutomaticReporting(false).WithTransport(t);

            return(new SplunkTracingHttpClient("http://localhost:80", tracerOptions));
        }
예제 #2
0
        private static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .WriteTo.Console()
                         .CreateLogger();
            // var collectorOptions = new CollectorOptions("input-prd-p-5s7q2gt8x3qv.cloud.splunk.com"); "0b47304e-408c-47a5-8daa-3b5e424ef244"
            var collectorOptions = new CollectorOptions(host: "localhost", usePlaintext: true);
            var tracer           = new Tracer(new Options("08243c00-a31b-499d-9fae-776b41990997").WithCollector(collectorOptions));

            GlobalTracer.Register(tracer);

            for (var i = 0; i < 500; i++)
            {
                using (var scope = tracer.BuildSpan("testParent").WithTag("testSpan", "true").StartActive(true))
                {
                    scope.Span.Log("test");
                    tracer.ActiveSpan.Log($"iteration {i}");

                    Thread.Sleep(new Random().Next(5, 10));
                    var innerSpan = tracer.BuildSpan("childSpan").Start();
                    innerSpan.SetTag("innerTestTag", "true");

                    Thread.Sleep(new Random().Next(10, 20));
                    innerSpan.Finish();
                }
            }
            tracer.Flush();
            Console.ReadKey();
        }
예제 #3
0
        private Tracer GetTracer(ISpanRecorder recorder = null)
        {
            var spanRecorder     = recorder ?? new SimpleMockRecorder();
            var collectorOptions = new CollectorOptions("localhost", 80, true);
            var tracerOptions    = new Options("TEST").WithCollector(collectorOptions).WithAutomaticReporting(false);

            return(new Tracer(tracerOptions, spanRecorder));
        }
예제 #4
0
        public void TracerOptionsShouldLetYouOverrideTags()
        {
            var collectorOptions = new CollectorOptions("localhost", 80, true);
            var overrideTags     = new Dictionary <string, object> {
                { SplunkTracingConstants.ComponentNameKey, "test_component" }
            };
            var tracerOptions = new Options("TEST").WithCollector(collectorOptions).WithTags(overrideTags).WithAutomaticReporting(false);

            Assert.Equal("test_component", tracerOptions.Tags[SplunkTracingConstants.ComponentNameKey]);
            Assert.Equal(SplunkTracingConstants.TracerPlatformValue,
                         tracerOptions.Tags[SplunkTracingConstants.TracerPlatformKey]);
        }
예제 #5
0
        static void Main(string[] args)
        {
            // create your tracer options, initialize it, assign it to the GlobalTracer
            var splKey      = Environment.GetEnvironmentVariable("SPL_KEY");
            var splSettings = new CollectorOptions("localhost");
            var splOptions  = new Options(splKey).WithCollector(splSettings);
            var tracer      = new Tracer(splOptions);

            GlobalTracer.Register(tracer);

            // do some work in parallel, this work also includes awaited calls
            Parallel.For(1, 100, i => DoThing(i));

            // block until you enter a key
            Console.ReadKey();
        }
예제 #6
0
        public void TestFileCollector()
        {
            var testFolder = AsaHelpers.GetTempFolder();

            Directory.CreateDirectory(testFolder);

            var opts = new CollectorOptions()
            {
                EnableFileSystemCollector = true,
                GatherHashes        = true,
                SelectedDirectories = new List <string>()
                {
                    testFolder
                },
                DownloadCloud = false,
            };

            using (var file = File.Open(Path.Combine(testFolder, "AsaLibTesterMZ"), FileMode.OpenOrCreate))
            {
                file.Write(FileSystemUtils.WindowsMagicNumber, 0, 2);
                file.Write(FileSystemUtils.WindowsMagicNumber, 0, 2);

                file.Close();
            }

            using (var file = File.Open(Path.Combine(testFolder, "AsaLibTesterJavaClass"), FileMode.OpenOrCreate))
            {
                file.Write(FileSystemUtils.JavaMagicNumber, 0, 4);
                file.Close();
            }

            var fsc = new FileSystemCollector(opts);

            fsc.TryExecute();

            Assert.IsTrue(fsc.Results.Any(x => x is FileSystemObject FSO && FSO.Path.EndsWith("AsaLibTesterJavaClass") && FSO.IsExecutable == true));
            Assert.IsTrue(fsc.Results.Any(x => x is FileSystemObject FSO && FSO.Path.EndsWith("AsaLibTesterMZ") && FSO.IsExecutable == true));

            ConcurrentStack <CollectObject> results = new ConcurrentStack <CollectObject>();

            fsc = new FileSystemCollector(opts, x => results.Push(x));
            fsc.TryExecute();

            Assert.IsTrue(results.Any(x => x is FileSystemObject FSO && FSO.Path.EndsWith("AsaLibTesterJavaClass") && FSO.IsExecutable == true));
            Assert.IsTrue(results.Any(x => x is FileSystemObject FSO && FSO.Path.EndsWith("AsaLibTesterMZ") && FSO.IsExecutable == true));
        }
예제 #7
0
        public void TracerShouldTrapExceptions()
        {
            var x = false;
            Action <Exception> eh = delegate { x = true; };
            var collectorOptions  = new CollectorOptions("localhost", 80, true);
            var tracerOptions     = new Options().WithCollector(collectorOptions).WithExceptionHandler(eh.Invoke);
            var recorder          = new SimpleMockRecorder();
            var mockClient        = new Mock <ISplunkTracingHttpClient>();

            mockClient.Setup(client => client.Translate(recorder)).Throws <OverflowException>();

            var tracer = new Tracer(tracerOptions, recorder, mockClient.Object);

            var span = tracer.BuildSpan("test").Start();

            span.Finish();

            Assert.False(x);
            tracer.Flush();
            Assert.True(x);
        }
예제 #8
0
        public void PropagatorStackInTracerShouldInjectAndExtract()
        {
            var ps = new PropagatorStack(BuiltinFormats.HttpHeaders);

            ps.AddPropagator(new B3Propagator());
            ps.AddPropagator(new HttpHeadersPropagator());
            ps.AddPropagator(new TextMapPropagator());

            var sr         = new SimpleMockRecorder();
            var satOpts    = new CollectorOptions("localhost", 8088, true);
            var tracerOpts = new Options("TEST").WithCollector(satOpts).WithAutomaticReporting(false);

            var tracer = new Tracer(tracerOpts, sr, ps);

            var span = tracer.BuildSpan("propTest").Start();

            var traceId = span.TypedContext().TraceId;
            var spanId  = span.TypedContext().SpanId;

            var hexTraceId = Convert.ToUInt64(traceId).ToString("X");
            var hexSpanId  = Convert.ToUInt64(spanId).ToString("X");

            var data = new Dictionary <string, string>();

            tracer.Inject(span.Context, BuiltinFormats.HttpHeaders, new TextMapInjectAdapter(data));

            Assert.Equal(hexTraceId, data["ot-tracer-traceid"]);
            Assert.Equal(hexTraceId, data["X-B3-TraceId"]);
            Assert.Equal(hexSpanId, data["ot-tracer-spanid"]);
            Assert.Equal(hexSpanId, data["X-B3-SpanId"]);

            span.Finish();

            var ctx = tracer.Extract(BuiltinFormats.HttpHeaders, new TextMapExtractAdapter(data));

            Assert.Equal(ctx.SpanId, spanId);
            Assert.Equal(ctx.TraceId, traceId);
        }
예제 #9
0
 protected BaseCollector(CollectorOptions?opts, Action <CollectObject>?changeHandler)
 {
     this.opts          = opts ?? new CollectorOptions();
     this.changeHandler = changeHandler;
 }
예제 #10
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.ConfigureAppOptions(Configuration);

            // Register Configuration
            // https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer
            //NLog.Extensions.Logging.ConfigSettingLayoutRenderer.DefaultConfiguration = Configuration;
            services.AddHttpContextAccessor();

            services.AddTransient <IImageRepository, ImageRepository>();
            services.AddTransient <IAppLogRepository, AppLogRepository>();

            services.AddApplicationServices(Configuration);

            var envVars = Environment.GetEnvironmentVariables();

            var connectionString = Configuration.GetConnectionString("Default");

            if (envVars.Contains("ASPNETCORE_CONNECTION_STRING"))
            {
                connectionString = envVars["ASPNETCORE_CONNECTION_STRING"].ToString();
            }

            services.AddDbContext <DefaultDatabaseContext>(options =>
            {
                options.UseSqlServer(connectionString, sqlServerOptions =>
                {
                    sqlServerOptions.MigrationsAssembly("Bing.Wallpaper.Data.SqlServer");
                });
            });

            var collectorOptions = new CollectorOptions();

            services.Configure <CollectorOptions>(options =>
            {
                Configuration.GetSection(CollectorOptions.Name).Bind(options);

                if (string.IsNullOrWhiteSpace(options.ThumbnailPath))
                {
                    options.ThumbnailPath = Path.Join(WebHostEnvironment.ContentRootPath, "thumbnails");

                    if (!Directory.Exists(options.ThumbnailPath))
                    {
                        Directory.CreateDirectory(options.ThumbnailPath);
                    }
                }


                collectorOptions = options;
            });

            services.AddScheduler(builder =>
            {
                builder.AddJob <BingImageJob>(configure: options =>
                {
                    /*
                     * -------------------------------------------------------------------------------------------------------------
                     *                                        Allowed values    Allowed special characters   Comment
                     *
                     * 忙式式式式式式式式式式式式式 second (optional)       0-59              * , - /
                     * 弛 忙式式式式式式式式式式式式式 minute                0-59              * , - /
                     * 弛 弛 忙式式式式式式式式式式式式式 hour                0-23              * , - /
                     * 弛 弛 弛 忙式式式式式式式式式式式式式 day of month      1-31              * , - / L W ?
                     * 弛 弛 弛 弛 忙式式式式式式式式式式式式式 month           1-12 or JAN-DEC   * , - /
                     * 弛 弛 弛 弛 弛 忙式式式式式式式式式式式式式 day of week   0-6  or SUN-SAT   * , - / # L ?                Both 0 and 7 means SUN
                     * 弛 弛 弛 弛 弛 弛
                     * * * * * * *
                     * -------------------------------------------------------------------------------------------------------------
                     */
                    //options.CronSchedule = "0 0 5 * * *";
                    options.CronSchedule   = collectorOptions.Schedule;
                    options.CronTimeZone   = TimeZoneInfo.Local.Id;
                    options.RunImmediately = false;
                });
            });

            services.AddDtoMapper();

            services.AddControllersWithViews();

            var defaultVersion = new ApiVersion(1, 0);

            services.AddApiVersioningAndSwaggerGen(defaultVersion);
        }