Ejemplo n.º 1
0
        static JObject SerializeSampler(ISampler sampler)
        {
            var samplerObj = new JObject();
            var fields     = sampler.GetType().GetFields();

            foreach (var field in fields)
            {
                samplerObj.Add(new JProperty(field.Name, JToken.FromObject(field.GetValue(sampler))));
            }

            if (sampler.GetType() != typeof(ConstantSampler))
            {
                var rangeProperty = sampler.GetType().GetProperty("range");
                if (rangeProperty != null)
                {
                    var range    = (FloatRange)rangeProperty.GetValue(sampler);
                    var rangeObj = new JObject
                    {
                        new JProperty("minimum", range.minimum),
                        new JProperty("maximum", range.maximum)
                    };
                    samplerObj.Add(new JProperty("range", rangeObj));
                }
            }
            return(samplerObj);
        }
        public SamplerElement(SerializedProperty property, Parameter parameter)
        {
            m_Property = property;
            var template = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(
                $"{StaticData.uxmlDir}/Sampler/SamplerElement.uxml");

            template.CloneTree(this);

            m_Parameter = parameter;
            m_Sampler   = GetSamplerFromSerializedObject();

            if (m_Sampler == null)
            {
                CreateSampler(typeof(UniformSampler));
            }

            var samplerName = this.Q <Label>("sampler-name");

            samplerName.text = UppercaseFirstLetter(m_Property.name);

            m_Properties               = this.Q <VisualElement>("fields-container");
            m_SamplerTypeDropdown      = this.Q <ToolbarMenu>("sampler-type-dropdown");
            m_SamplerTypeDropdown.text = SamplerUtility.GetSamplerDisplayName(m_Sampler.GetType());;
            foreach (var samplerType in StaticData.samplerTypes)
            {
                var displayName = SamplerUtility.GetSamplerDisplayName(samplerType);;
                m_SamplerTypeDropdown.menu.AppendAction(
                    displayName,
                    a => { ReplaceSampler(samplerType); },
                    a => DropdownMenuAction.Status.Normal);
            }
            CreatePropertyFields();
        }
Ejemplo n.º 3
0
 static void DeserializeSampler(ISampler sampler, JObject samplerObj)
 {
     foreach (var samplerFieldPair in samplerObj)
     {
         if (samplerFieldPair.Key == "range")
         {
             var rangeObj = (JObject)samplerFieldPair.Value;
             var field    = sampler.GetType().GetField(samplerFieldPair.Key);
             var range    = new FloatRange(rangeObj["minimum"].ToObject <float>(), rangeObj["maximum"].ToObject <float>());
             field.SetValue(sampler, range);
         }
         else
         {
             var field = sampler.GetType().GetField(samplerFieldPair.Key);
             if (field != null)
             {
                 field.SetValue(sampler, JsonConvert.DeserializeObject(samplerFieldPair.Value.ToString(), field.FieldType));
             }
         }
     }
 }
Ejemplo n.º 4
0
        void CopyFloatRangeToNewSampler(ISampler newSampler)
        {
            if (m_RangeProperty == null)
            {
                return;
            }

            var rangeField = newSampler.GetType().GetField(m_RangeProperty.name);

            if (rangeField == null)
            {
                return;
            }

            var range = new FloatRange(
                m_RangeProperty.FindPropertyRelative("minimum").floatValue,
                m_RangeProperty.FindPropertyRelative("maximum").floatValue);

            rangeField.SetValue(newSampler, range);
        }
        static SamplerOptions SerializeSampler(ISampler sampler)
        {
            var samplerData = new SamplerOptions();

            if (sampler is Samplers.ConstantSampler constantSampler)
            {
                samplerData.defaultSampler = new ConstantSampler
                {
                    value = constantSampler.value
                }
            }
            ;
            else if (sampler is Samplers.UniformSampler uniformSampler)
            {
                samplerData.defaultSampler = new UniformSampler
                {
                    min = uniformSampler.range.minimum,
                    max = uniformSampler.range.maximum
                }
            }
            ;
            else if (sampler is Samplers.NormalSampler normalSampler)
            {
                samplerData.defaultSampler = new NormalSampler
                {
                    min  = normalSampler.range.minimum,
                    max  = normalSampler.range.maximum,
                    mean = normalSampler.mean,
                    standardDeviation = normalSampler.standardDeviation
                }
            }
            ;
            else
            {
                throw new ArgumentException($"Invalid sampler type ({sampler.GetType()})");
            }
            return(samplerData);
        }
        void CreatePropertyFields()
        {
            m_RangeProperty = null;
            m_Properties.Clear();
            var currentProperty     = m_Property.Copy();
            var nextSiblingProperty = m_Property.Copy();

            nextSiblingProperty.NextVisible(false);

            if (currentProperty.NextVisible(true))
            {
                do
                {
                    if (SerializedProperty.EqualContents(currentProperty, nextSiblingProperty))
                    {
                        break;
                    }
                    if (currentProperty.type == "FloatRange")
                    {
                        m_RangeProperty = currentProperty.Copy();
                        m_Properties.Add(new FloatRangeElement(m_RangeProperty));
                    }
                    else
                    {
                        var propertyField = new PropertyField(currentProperty.Copy());
                        propertyField.Bind(m_Property.serializedObject);
                        var originalField    = m_Sampler.GetType().GetField(currentProperty.name);
                        var tooltipAttribute = originalField.GetCustomAttributes(true).ToList().Find(att => att.GetType() == typeof(TooltipAttribute));
                        if (tooltipAttribute != null)
                        {
                            propertyField.tooltip = (tooltipAttribute as TooltipAttribute)?.tooltip;
                        }
                        m_Properties.Add(propertyField);
                    }
                }while (currentProperty.NextVisible(false));
            }
        }
        public static OperationsTracingOptions UseSampler(
            this OperationsTracingOptions options,
            ISampler sampler)
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            options.Context.Services.AddSingleton(sampler);

            options.Context.Messages.Add($"naos services builder: tracing sampler used (type={sampler.GetType().Name})");

            return(options);
        }
Ejemplo n.º 8
0
        public static void ConfigureServices(InstanceInfo instanceInfo, IServiceCollection services, bool isPublicWebService)
        {
            var jaegerAgentHost   = Environment.GetEnvironmentVariable("TRACING_AGENT_HOST");// ?? "localhost";
            var samplingRateParam = Environment.GetEnvironmentVariable("TRACING_RATE") ?? "100";

            int.TryParse(samplingRateParam, out var samplingRate);
            if (samplingRate > 100)
            {
                samplingRate = 100;
            }
            if (samplingRate < 0)
            {
                samplingRate = 0;
            }

            if (string.IsNullOrEmpty(jaegerAgentHost) || samplingRate == 0)
            {
                Console.WriteLine("Tracing is disabled (environments TRACING_AGENT_HOST, TRACING_RATE)");
                services.AddSingleton <ITracer>(new NoTracer());
                return;
            }

            services.AddSingleton <ITracer>(serviceProvider =>
            {
                var serviceName = Assembly.GetEntryAssembly().GetName().Name;

                //ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
                //var loggingReporter = new LoggingReporter(loggerFactory);

                var reporter = new RemoteReporter.Builder()
                               .WithSender(new UdpSender(jaegerAgentHost, 6831, 0))
                               .Build();

                //https://www.jaegertracing.io/docs/sampling/
                ISampler sampler = samplingRate == 100
                    ? new ConstSampler(sample: true) as ISampler
                    : new ProbabilisticSampler(samplingRate / 100) as ISampler;

                Logger().Information("Tracer use sampler {Sampler}", sampler.GetType().Name);

                ITracer tracer = new Tracer.Builder(serviceName)
                                 //.WithLoggerFactory(loggerFactory)
                                 .WithSampler(sampler)
                                 .WithReporter(reporter)
                                 //.WithReporter(new CompositeReporter(loggingReporter, reporter))
                                 .Build();

                GlobalTracer.Register(tracer);

                return(tracer);
            });

            // Prevent endless loops when OpenTracing is tracking HTTP requests to Jaeger.
            services.Configure <HttpHandlerDiagnosticOptions>(options =>
            {
                options.IgnorePatterns.Clear();
                options.IgnorePatterns.Add(request => request.RequestUri.Port == 8086 && request.RequestUri.PathAndQuery.Contains("write?db=appmetrics"));
            });

            // Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
            services.AddOpenTracing(builder =>
            {
                builder.ConfigureAspNetCore(options =>
                {
                    options.Hosting.ExtractEnabled = context => !isPublicWebService;     // never trust TraceId from user web-requests
                    options.Hosting.OnRequest      = (span, context) =>
                    {
                        span.SetTag("InstanceId", instanceInfo.Id.ToString());
                    };
                });
            }
                                    );
        }