Exemple #1
0
        public static MetricsOptions AddRuntimeTag(this MetricsOptions options)
        {
            options.GlobalTags["dotnet_runtime"] = RuntimeInformation.FrameworkDescription;
            options.GlobalTags["version"]        = Assembly.GetEntryAssembly()?.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "unknown";

            return(options);
        }
Exemple #2
0
        public static MetricsOptions WithGlobalTags(
            this MetricsOptions options,
            Action <Dictionary <string, string>, EnvironmentInfo> setupAction)
        {
            setupAction(options.GlobalTags, EnvInfo);

            return(options);
        }
Exemple #3
0
 internal MetricsConfigurationBuilder(
     IMetricsBuilder metricsBuilder,
     MetricsOptions currentOptions,
     Action <MetricsOptions> setupAction)
 {
     Builder      = metricsBuilder ?? throw new ArgumentNullException(nameof(metricsBuilder));
     _setupAction = setupAction ?? throw new ArgumentNullException(nameof(setupAction));
     _options     = currentOptions ?? new MetricsOptions();
 }
Exemple #4
0
        public static MetricsOptions AddGitTag(this MetricsOptions options)
        {
            var metadata = Assembly.GetEntryAssembly()?.GetCustomAttributes <AssemblyMetadataAttribute>().ToList();

            options.GlobalTags["branch"]   = metadata?.SingleOrDefault(a => a.Key == "GitInfo.Branch")?.Value ?? "unknown";
            options.GlobalTags["sha"]      = metadata?.SingleOrDefault(a => a.Key == "GitInfo.Sha")?.Value ?? "unknown";
            options.GlobalTags["is_dirty"] = metadata?.SingleOrDefault(a => a.Key == "GitInfo.IsDirty")?.Value ?? "unknown";

            return(options);
        }
        /// <inheritdoc />
        public IMetricsRoot Build()
        {
            IMetricsRegistry registry = new NullMetricsRegistry();

            if (_options == null)
            {
                _options = new MetricsOptions();
            }

            if (_options.Enabled)
            {
                registry = new DefaultMetricsRegistry(_options.DefaultContextLabel, _clock, ContextRegistry);
            }

            if (_metricsOutputFormatters.Count == 0)
            {
                _metricsOutputFormatters.Add(new MetricsTextOutputFormatter());
            }

            if (_envFormatters.Count == 0)
            {
                _envFormatters.Add(new EnvInfoTextOutputFormatter());
            }

            var builderFactory = new DefaultMetricsBuilderFactory(_defaultSamplingReservoir);
            var measure        = new DefaultMeasureMetricsProvider(registry, builderFactory, _clock);
            var provider       = new DefaultMetricsProvider(registry, builderFactory, _clock);
            var snapshot       = new DefaultMetricValuesProvider(_metricsFilter, registry);
            var manage         = new DefaultMetricsManager(registry);
            var metrics        = new DefaultMetrics(_clock, _metricsFilter, measure, builderFactory, provider, snapshot, manage);
            var defaultMetricsOutputFormatter = _defaultMetricsOutputFormatter ?? _metricsOutputFormatters.FirstOrDefault();
            var defaultEnvOutputFormatter     = _defauEnvOutputFormatter ?? _envFormatters.FirstOrDefault();

            if (CanReport())
            {
                _metricsReportRunner = new DefaultMetricsReportRunner(metrics, _reporters);
            }

            return(new MetricsRoot(
                       metrics,
                       _options,
                       _metricsOutputFormatters,
                       _envFormatters,
                       defaultMetricsOutputFormatter,
                       defaultEnvOutputFormatter,
                       _environmentInfoProvider,
                       _reporters,
                       _metricsReportRunner));

            IMetricContextRegistry ContextRegistry(string context) =>
            new DefaultMetricContextRegistry(context, new GlobalMetricTags(_options.GlobalTags));
        }
Exemple #6
0
        public static MetricsOptions AddEnvTag(this MetricsOptions options, string envName = null)
        {
            if (!options.GlobalTags.ContainsKey("env"))
            {
                options.GlobalTags.Add("env", envName ?? EnvInfo.RunningEnvironment);
            }
            else
            {
                options.GlobalTags["env"] = envName ?? EnvInfo.RunningEnvironment;
            }

            return(options);
        }
Exemple #7
0
        public static MetricsOptions AddServerTag(this MetricsOptions options, string serverName = null)
        {
            if (!options.GlobalTags.ContainsKey("server"))
            {
                options.GlobalTags.Add("server", serverName ?? EnvInfo.MachineName);
            }
            else
            {
                options.GlobalTags["server"] = serverName ?? EnvInfo.MachineName;
            }

            return(options);
        }
Exemple #8
0
        public static MetricsOptions AddAppTag(this MetricsOptions options, string appName = null)
        {
            if (!options.GlobalTags.ContainsKey("app"))
            {
                options.GlobalTags.Add("app", appName ?? EnvInfo.EntryAssemblyName);
            }
            else
            {
                options.GlobalTags["app"] = appName ?? EnvInfo.EntryAssemblyName;
            }

            return(options);
        }
Exemple #9
0
        /// <inheritdoc />
        public IMetricsBuilder Configure(MetricsOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _setupAction(options);

            _options = options;

            return(Builder);
        }
Exemple #10
0
        /// <inheritdoc />
        public IMetricsBuilder Configure(IEnumerable <KeyValuePair <string, string> > optionValues)
        {
            if (optionValues == null)
            {
                throw new ArgumentNullException(nameof(optionValues));
            }

            var mergedOptions = new KeyValuePairMetricsOptions(_options, optionValues).AsOptions();

            _setupAction(mergedOptions);

            _options = mergedOptions;

            return(Builder);
        }
Exemple #11
0
        /// <inheritdoc />
        public IMetricsBuilder Extend(MetricsOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var optionsValuesToExtend = options.ToKeyValue();
            var extendedOptions       = new KeyValuePairMetricsOptions(_options, optionsValuesToExtend).AsOptions(true);

            _setupAction(extendedOptions);

            _options = extendedOptions;

            return(Builder);
        }
Exemple #12
0
 public MetricsRoot(
     IMetrics metrics,
     MetricsOptions options,
     MetricsFormatterCollection metricsOutputFormatters,
     EnvFormatterCollection envOutputFormatters,
     IMetricsOutputFormatter defaultMetricsOutputFormatter,
     IEnvOutputFormatter defaultEnvOutputFormatter,
     EnvironmentInfoProvider environmentInfoProvider,
     MetricsReporterCollection reporterCollection,
     IRunMetricsReports reporter)
 {
     Options                       = options ?? throw new ArgumentNullException(nameof(options));
     _metrics                      = metrics ?? throw new ArgumentNullException(nameof(metrics));
     ReportRunner                  = reporter ?? throw new ArgumentNullException(nameof(reporter));
     _environmentInfoProvider      = new EnvironmentInfoProvider();
     Reporters                     = reporterCollection ?? new MetricsReporterCollection();
     OutputMetricsFormatters       = metricsOutputFormatters ?? new MetricsFormatterCollection();
     OutputEnvFormatters           = envOutputFormatters ?? new EnvFormatterCollection();
     DefaultOutputMetricsFormatter = defaultMetricsOutputFormatter;
     DefaultOutputEnvFormatter     = defaultEnvOutputFormatter;
     _environmentInfoProvider      = environmentInfoProvider;
 }
Exemple #13
0
        public static MetricsOptions AddUuidTag(this MetricsOptions options, Guid uuid)
        {
            options.GlobalTags["uuid"] = uuid.ToString();

            return(options);
        }
Exemple #14
0
        public static MetricsOptions AddMachineNameTag(this MetricsOptions options, string?machineName = null)
        {
            options.GlobalTags["machine"] = machineName ?? EnvInfo.MachineName;

            return(options);
        }