Esempio n. 1
0
        public static IMetricsRoot SetMetrics(InitAppMetricsModel initAppMetricsModel)
        {
            if (_metrics == null)
            {
                _metrics = InitAppMetrics(initAppMetricsModel);
            }

            return(_metrics);
        }
Esempio n. 2
0
        public static IMetricsRoot SetMetrics()
        {
            if (_metrics == null)
            {
                InitAppMetricsModel initAppMetricsModel = new InitAppMetricsModel();
                _metrics = InitAppMetrics(initAppMetricsModel);
            }

            return(_metrics);
        }
Esempio n. 3
0
        private static IMetricsRoot InitAppMetrics(InitAppMetricsModel initAppMetricsModel)
        {
            GlobalMetricTags globalMetricTags = new GlobalMetricTags();

            if (initAppMetricsModel.GlobalTags != null)
            {
                foreach (var item in initAppMetricsModel.GlobalTags)
                {
                    globalMetricTags.Add(item.Key, item.Value);
                }
            }

            var metrics = new MetricsBuilder()
                          .Configuration.Configure(options =>
            {
                options.DefaultContextLabel = initAppMetricsModel.DefaultContextLabel;
                options.AddAppTag(Assembly.GetExecutingAssembly().GetName().Name);
                options.AddServerTag(Environment.MachineName);
                options.AddEnvTag(initAppMetricsModel.EnvTag);
                options.GlobalTags = globalMetricTags;
            })
                          .Report.ToInfluxDb(options =>
            {
                options.InfluxDb.BaseUri                 = new Uri(initAppMetricsModel.BaseUri);
                options.InfluxDb.Database                = initAppMetricsModel.Database;
                options.InfluxDb.UserName                = initAppMetricsModel.UserName;
                options.InfluxDb.Password                = initAppMetricsModel.Password;
                options.HttpPolicy.BackoffPeriod         = TimeSpan.FromSeconds(30);
                options.HttpPolicy.FailuresBeforeBackoff = 5;
                options.HttpPolicy.Timeout               = TimeSpan.FromSeconds(3);
                options.FlushInterval = TimeSpan.FromSeconds(5);

                options.InfluxDb.CreateDataBaseIfNotExists = true;                //如果没有库,则创建
                options.MetricsOutputFormatter             = new MetricsInfluxDbLineProtocolOutputFormatter();
            })
                          .Build();

            return(metrics);
        }