예제 #1
0
 public async Task Publish(Metric[] metrics)
 {
     foreach (var metric in metrics)
     {
         Publish(metric);
     }
 }
        public async void RestClient_posts_metrics()
        {
            var metricName = GetUniqueMetricName();

            var dataPoint = new DataPoint(DateTime.UtcNow.MillisecondsSinceEpoch(), 5L);

            var metric = new Metric(metricName)
                .AddTag("route_id", "1")
                .AddDataPoint(dataPoint);

            await _client.AddMetricsAsync(new[] {metric});

            var query = new QueryBuilder()
                .SetStart(TimeSpan.FromSeconds(5))
                .AddQueryMetric(new QueryMetric(metricName));

            Thread.Sleep(TimeSpan.FromSeconds(2));

            var response = await _client.QueryMetricsAsync(query);

            response.Queries.Should().HaveCount(1);
            response.Queries[0].SampleSize.Should().Be(1);
            response.Queries[0].Results.Should().HaveCount(1);
            response.Queries[0].Results[0].DataPoints.Single().ShouldBeEquivalentTo(dataPoint);
        }
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            AddPullToRefresh ();

            // add the nav button.
            UIBarButtonItem addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
            addButton.Clicked += async (sender, e) => {

                var addController = new AddCheckinViewController();
                this.NavigationController.PushViewController(addController,true);
                _dataSource.Clear();

                var result = await Buddy.RecordMetricAsync("adding_checkin", null, TimeSpan.FromDays(1));

                if (result.IsSuccess) {
                    _timedMetric = result.Value;
                }
            };

            this.NavigationItem.RightBarButtonItem = addButton;

            UIBarButtonItem logoutButton = new UIBarButtonItem ("Logout", UIBarButtonItemStyle.Plain, 
                async (s, e) => {
                    await Buddy.LogoutUserAsync();
                });

            this.NavigationItem.LeftBarButtonItem = logoutButton;

            _dataSource = new CheckinDataSource (this);
            this.checkinTable.Source = _dataSource;
        }
예제 #4
0
 private static void Log(StringBuilder builder, Metric metric, List<object> metricValues)
 {
     if (metricValues == null || metricValues.Count == 0)
         LogHelper(builder, metric);
     else
         LogHelper(builder, metric, metricValues.ToArray());
 }
        public async void QueryMetricsAsync_uses_sum_aggregator()
        {
            var metricName = GetUniqueMetricName();

            var time = DateTime.UtcNow.MillisecondsSinceEpoch();

            var dataPoint = new DataPoint(time, 10L);
            var dataPoint2 = new DataPoint(time + 1, 30L);

            var metric = new Metric(metricName)
                .AddTag("route_id", "1")
                .AddDataPoint(dataPoint2)
                .AddDataPoint(dataPoint);

            await _client.AddMetricsAsync(new[] { metric });

            var queryMetric = new QueryMetric(metricName)
                .AddAggregator(new SumAggregator(1, TimeUnit.Minutes));

            var query = new QueryBuilder()
                .SetStart(TimeSpan.FromSeconds(10))
                .AddQueryMetric(queryMetric);

            Thread.Sleep(TimeSpan.FromSeconds(2));

            var response = await _client.QueryMetricsAsync(query);

            response.Queries.Should().HaveCount(1);
            response.Queries[0].Results.Should().HaveCount(1);
            response.Queries[0].Results[0].DataPoints.Single().Value.Should().Be(40L);
        }
        /// <summary>
        ///		Create a new <see cref="PerformanceCounterMonitor"/> actor.
        /// </summary>
        /// <param name="seriesName">
        ///		The name of the data series that tracks the performance counter's value.
        /// </param>
        /// <param name="performanceCounterFactory">
        ///		A factory delegate that creates the <see cref="PerformanceCounter"/> to monitor.
        /// </param>
        public PerformanceCounterMonitor(string seriesName, Func<PerformanceCounter> performanceCounterFactory)
        {
            if (String.IsNullOrWhiteSpace(seriesName))
                throw new ArgumentException("Argument cannot be null, empty, or entirely componsed of whitespace: 'seriesName'.", nameof(seriesName));

            if (performanceCounterFactory == null)
                throw new ArgumentNullException(nameof(performanceCounterFactory));

            _seriesName = seriesName;
            _performanceCounterFactory = performanceCounterFactory;
            _cancelPublishing = new Cancelable(Context.System.Scheduler);

            Receive<GatherMetrics>(_ =>
            {
                Metric metric = new Metric(
                    _seriesName,
                    _performanceCounter.NextValue()
                );
                foreach (IActorRef subscriber in _subscribers)
                    subscriber.Tell(metric);
            });
            Receive<SubscribePerformanceCounter>(request =>
            {
                _subscribers.Add(request.Subscriber);
            });
            Receive<UnsubscribePerformanceCounter>(request =>
            {
                _subscribers.Remove(request.Subscriber);
            });
        }
예제 #7
0
파일: TheMetric.cs 프로젝트: jonnii/statr
 public static Action<StatrContext> IsRouted(Metric metric)
 {
     return context =>
     {
         var router = context.Container.Resolve<IMetricRouter>();
         router.Route(metric);
     };
 }
예제 #8
0
        public bool IsMetricMatch(Metric metric)
        {
            if (metric == null)
                return false;

            return this.typeCheck.IsMatch(metric.Type)
                && this.backend.SupportedTypes.Contains(metric.Type, StringComparer.OrdinalIgnoreCase);
        }
예제 #9
0
 public InstrumentationTarget(IMethodDetails target, string metricName, string name, string transactionNamingPriority, Metric metric)
 {
     this.Target = target;
     this.MetricName = metricName;
     this.Name = name;
     this.TransactionNamingPriority = transactionNamingPriority;
     this.Metric = metric;
 }
예제 #10
0
 public void NotifyMetric(Metric metric)
 {
     var handler = MetricReceived;
     if (handler != null)
     {
         handler(this, new MetricEventArgs(metric));
     }
 }
예제 #11
0
 public TracerFactory(string metricName, string name, string transactionNamingPriority, Metric metric)
     : this()
 {
     this.Name = name;
     this.TransactionNamingPriority = transactionNamingPriority;
     this.MetricName = metricName;
     this.Metric = metric;
 }
예제 #12
0
        public InstrumentAttribute(string metricName, string name, string transactionNamingPriority, Metric metric)
        {
            this.Scopes = InstrumentationScopes.All;

            this.Name = name;
            this.TransactionNamingPriority = transactionNamingPriority;
            this.MetricName = metricName;
            this.Metric = metric;
        }
예제 #13
0
		public void parsing(string input, string name, string value, MetricType type) {
			var result = new Metric();
			Check.That(
				() => Metric.TryParse(input, out result),
				() => result.Name == name,
				() => result.Value.ToString() == value,
				() => result.Value.Type == type
			);
		}
예제 #14
0
파일: Timer.cs 프로젝트: pmcgrath/metrics
        public Timer(
            Metric.Client.ITimingCompletionRecorder timingCompletionRecorder,
            string key)
        {
            this.c_timingCompletionRecorder = timingCompletionRecorder;
            this.c_key = key;

            this.c_stopWatch = Stopwatch.StartNew();
        }
예제 #15
0
 public bool TryAppend(Metric metric, Encoding encoding)
 {
     try {
         position += metric.GetBytes(encoding, bytes, position);
         return true;
     } catch(ArgumentException) {
         return false;
     }
 }
예제 #16
0
        public AggregatedMetric Aggregate(AggregatedMetric original, Metric metric)
        {
            var countMetric = metric;

            return new AggregatedMetric
            {
                LastValue = countMetric.Value,
                NumMetrics = ++original.NumMetrics,
                Value = original.Value + countMetric.Value
            };
        }
예제 #17
0
        public Metric Parse(string block)
        {
            var metric = new Metric {
                Name = ExtractName(block),
                Value = resultCounter.Matches(block).Count,
                Warning = block.Contains("<h3>Warning:"),
                Error = block.Contains("<h3>Error:")
            };

            return metric;
        }
예제 #18
0
        public void Submit(Metric metric, string metricsPrefix)
        {
            if (this.disposed)
                throw new ObjectDisposedException(typeof(StatsDBackend).Name);

            if (!supportedTypes.Contains(metric.Type, StringComparer.OrdinalIgnoreCase))
                throw new ArgumentException("Metric type not supported.", "metric");

            this.graphiteChannel.Report(
                Helper.BuildKey(metric.Key, metricsPrefix),
                (int)metric.Value);
        }
        /// <summary>
        /// Initializes a new instance of the PSMetric class.
        /// </summary>
        /// <param name="metric">The input Metric object</param>
        public PSMetricNoDetails(Metric metric)
        {
            // Keep the original value (localized string, Dictionary, List) in the base
            base.Name = metric.Name;

            this.EndTime = metric.EndTime;
            this.Name = metric.Name == null ? null : metric.Name.Value;
            this.MetricValues = metric.MetricValues;
            this.StartTime = metric.StartTime;
            this.TimeGrain = metric.TimeGrain;
            this.MetricUnit = metric.MetricUnit;
        }
예제 #20
0
 public static void SendMetric(Metric metric, object metricObject)
 {
     try
     {
         AnalyticsSoapClient client = new AnalyticsSoapClient();
         client.UpdateAnalyticAsync(getGuid(), metric.ToString(), metricObject.ToString());
     }
     catch (Exception e)
     {
         Debug.WriteLine(e.StackTrace);
     }
 }
        public void ShouldCallFormatterOnceWhenPassedOneStatistic()
        {
            var statistics = new Metric[] {
                new Metric { Name = "Test Metric" }
            };

            var builder = new TeamCityInfoBuilder(_formatterMock.Object);

            builder.AddStatistics(statistics);

            _formatterMock.Verify(m => m.Format(It.IsAny<string>()), Times.Once());
        }
예제 #22
0
        public Recorder(
            Metric.Client.StatsdPipe pipe,
            string keyPrefix = null,
            string keySuffix = null)
        {
            this.c_pipe = pipe;
            this.c_keyPrefix = keyPrefix;
            this.c_keySuffix = keySuffix;

            if (!string.IsNullOrWhiteSpace(this.c_keyPrefix))	{ this.c_keyPrefix += "."; }
            if (!string.IsNullOrWhiteSpace(this.c_keySuffix))	{ this.c_keySuffix = "." + this.c_keySuffix; }
        }
예제 #23
0
 public BulldozerWindow(GeneticAlgorithm<TreeChromosome> alg, Metric[] metrics, int iterationShowRate)
 {
     InitializeComponent();
     TexFormulaParser.Initialize();
     _alg = alg;
     _metrics = metrics;
     _iterationShowRate = iterationShowRate;
     _dispatcher = new GaDispatcher(alg);
     _manualStop = false;
     _alg.IterationCallBack += AlgIterationCallBack;
     FillPoolPanel();
     FillOptionalPanel();
 }
        public void ShouldBeASuccessfulBuild()
        {
            var statistics = new Metric[] {
                new Metric { Name = "Test Metric", Error = false, Warning = false },
            };

            var builder = new TeamCityInfoBuilder(_formatterMock.Object);

            builder.AddStatistics(statistics);
            builder.GenerateStatusInfo();

            var actual = builder.GetTeamCityInfo();
            Assert.AreEqual(BuildStatus.Success, actual.Status);
        }
    public void Emit(IDictionary<string, string> properties, Metric[] metrics)
    {
        // Wrap up properties and metrics that we want to send.
        var objectToSend = new
        {
            Properties = properties,
            Metrics = metrics
        };

        // Convert to JSON.
        var json = JsonConvert.SerializeObject(objectToSend);

        // Send!
        httpService.Post(postUrl, json);
    }
예제 #26
0
        public void Route(Metric metric)
        {
            Logger.DebugFormat("Routing {0}", metric);

            var matchingRoutes = routes.Keys.Where(
                k => Regex.IsMatch(metric.Name, k.Pattern));

            foreach (var matchingRoute in matchingRoutes)
            {
                IMetricRoute route;
                if (routes.TryGetValue(matchingRoute, out route))
                {
                    route.NotifyMetric(metric);
                }
            }
        }
예제 #27
0
    public static void GetRank(RankRetrieved callback, string level, Metric metric, float score)
    {
        rankCallback = callback;

        string url = "http://www.pillowdrift.com/swift/?type=rank&metric={0}&level={1}&{0}={2}";
        string metricString;

        if (metric == Metric.SPEED)
            metricString = "speed";
        else if (metric == Metric.TIME)
            metricString = "time";
        else return;

        // Make web request
        WWW www = new WWW(String.Format(url, metricString, level.Replace(" ", "%20"), score));

        instance.StartCoroutine("WaitForRank", www);
    }
예제 #28
0
 private static void LogHelper(StringBuilder builder, Metric metric, params object[] metricValues)
 {
     builder.AppendFormat(CultureInfo.InvariantCulture, "{0} = ", metric);
     if (metricValues == null)
     {
         builder.Append(ObjectToString(metricValues));
     }
     else
     {
         for (int i = 0; i < metricValues.Length; i++)
         {
             object metricValue = metricValues[i];
             string metricValueString = ObjectToString(metricValue);
             if (i > 0)
                 builder.Append(", ");
             builder.Append(metricValueString);
         }
     }
     builder.Append("; ");
 }
예제 #29
0
        /// <summary>
        /// A constructor to setup all the applicable fields.
        /// </summary>
        /// <param name="IO"></param>
        /// <param name="WorldLoader"></param>
        /// <param name="Algorithm"></param>
        /// <param name="Weight"></param>
        public AlgorithmRunner( System.IO.TextReader IO,
            GridWorldLoader<GenericGridWorldStaticState,
            GenericGridWorldDynamicState> WorldLoader, String Algorithm,
            Metric Weight, bool Batch, StateVisualizer<GenericGridWorldStaticState,
            GenericGridWorldDynamicState> vis, int Seed)
        {
            gen = new Random( Seed );
              this.vis = vis;
              this.Goal = new DestinationsReachedGoal( );
              this.Algorithm = new GenericAlgorithmFactory<GenericGridWorldStaticState,
            GenericGridWorldDynamicState>( ).SetRealTimeData(
            new SingleUnitOctileDistanceHeuristic( ), vis,
            new SingleUnitTransformer( ), Weight, 0.1 ).GetAlgorithm(
            Algorithm, null, 0 );

              GenericGridWorldDynamicState InitialDynamicState;
              WorldLoader.Load( IO, out StaticState, out InitialDynamicState );
              IO.Close( );
              this.Actions = GridWorldOperator.Ops;
        }
예제 #30
0
        private static void Publish(Metric metric)
        {
            if (metric.IsCount)
            {
                var countMetric = (Metric.Count)metric;
                Console.WriteLine(
                    @"Count metric ({0}):
Max             : {1}
Min             : {2}
Average         : {3}
Total           : {4}
Sample Size     : {5}",
                    countMetric.Name,
                    countMetric.Max,
                    countMetric.Min,
                    countMetric.Average,
                    countMetric.Sum,
                    countMetric.SampleCount);
            }
            else
            {
                var timeMetric = (Metric.TimeSpan)metric;
                var rawTimes = timeMetric.Item.Raw;
                Console.WriteLine(
                    @"TimeSpan metric ({0}):
Max             : {1}ms
Min             : {2}ms
Average         : {3}ms
Total           : {4}ms
Sample Size     : {5}
95 percentile   : {6}ms",
                    timeMetric.Name,
                    timeMetric.Max,
                    timeMetric.Min,
                    timeMetric.Average,
                    timeMetric.Sum,
                    timeMetric.SampleCount,
                    GetNinetyFivePercentile(rawTimes).TotalMilliseconds);

            }
        }
예제 #31
0
        public string GetReadyForNumerical(bool saveLoadedData = true)
        {
            if (ReadyForNumerical)
            {
                return("Is ready.");
            }

            StringBuilder log = new StringBuilder();

            Utils.StartTimer();

            log.AppendLine(Utils.PrintHeading("Create R_train/R_test sets from " + DataSetFile));
            Utils.LoadMovieLensSplitByCount(DataSetFile, out R_train,
                                            out R_test, MinCountOfRatings, MaxCountOfRatings, CountOfRatingsForTrain, ShuffleData, Seed);

            Console.WriteLine(R_train.DatasetBrief("Train set"));
            Console.WriteLine(R_test.DatasetBrief("Test set"));
            log.AppendLine(R_train.DatasetBrief("Train set"));
            log.AppendLine(R_test.DatasetBrief("Test set"));

            R_unknown = R_test.IndexesOfNonZeroElements();

            log.AppendLine(Utils.PrintValue("Relevant item criteria", RelevantItemCriteria.ToString("0.0")));
            RelevantItemsByUser = ItemRecommendationCore.GetRelevantItemsByUser(R_test, RelevantItemCriteria);
            log.AppendLine(Utils.PrintValue("Mean # of relevant items per user",
                                            RelevantItemsByUser.Average(k => k.Value.Count).ToString("0")));
            log.AppendLine(Utils.StopTimer());

            #region Prepare similarity data
            if (File.Exists(GetDataFileName("USR")) &&
                File.Exists(GetDataFileName("ISR")) &&
                File.Exists(GetDataFileName("SSIIR")))
            {
                Utils.StartTimer();
                Utils.PrintHeading("Load user-user similarities (rating based)");
                UserSimilaritiesOfRating = Utils.IO <SimilarityData> .LoadObject(GetDataFileName("USR"));

                Utils.StopTimer();

                Utils.StartTimer();
                Utils.PrintHeading("Load item-item similarities (rating based)");
                ItemSimilaritiesOfRating = Utils.IO <SimilarityData> .LoadObject(GetDataFileName("ISR"));

                Utils.StopTimer();

                Utils.StartTimer();
                Utils.PrintHeading("Load item-item strong similarity indicators (rating based)");
                StrongSimilarityIndicatorsByItemRating = Utils.IO <HashSet <Tuple <int, int> > > .LoadObject(GetDataFileName("SSIIR"));

                Utils.StopTimer();
            }
            else
            {
                Utils.StartTimer();
                Utils.PrintHeading("Compute user-user similarities (rating based)");
                Metric.GetPearsonOfRows(R_train, MaxCountOfNeighbors, StrongSimilarityThreshold,
                                        out UserSimilaritiesOfRating);
                if (saveLoadedData)
                {
                    Utils.IO <SimilarityData> .SaveObject(UserSimilaritiesOfRating, GetDataFileName("USR"));
                }
                Utils.StopTimer();

                Utils.StartTimer();
                Utils.PrintHeading("Compute item-item similarities (rating based)");
                Metric.GetPearsonOfColumns(R_train, MaxCountOfNeighbors, StrongSimilarityThreshold,
                                           out ItemSimilaritiesOfRating, out StrongSimilarityIndicatorsByItemRating);
                if (saveLoadedData)
                {
                    Utils.IO <SimilarityData> .SaveObject(ItemSimilaritiesOfRating, GetDataFileName("ISR"));

                    Utils.IO <HashSet <Tuple <int, int> > >
                    .SaveObject(StrongSimilarityIndicatorsByItemRating, GetDataFileName("SSIIR"));
                }
                Utils.StopTimer();
            }
            #endregion

            ReadyForNumerical = true;

            return(log.ToString());
        }
예제 #32
0
 public void Timer(string name, int milliseconds)
 {
     Metric.Timer(name, Unit.Calls, SamplingType.SlidingWindow, TimeUnit.Milliseconds).Record(milliseconds, TimeUnit.Milliseconds);
 }
예제 #33
0
 public Task TimerAsync(string name, int milliseconds)
 {
     Metric.Timer(name, Unit.Calls, SamplingType.SlidingWindow, TimeUnit.Milliseconds).Record(milliseconds, TimeUnit.Milliseconds);
     return(Task.CompletedTask);
 }
        /// <summary>
        /// Handles the message received event
        /// </summary>
        /// <param name="sender">The <see cref="object"/> representation of the event sender</param>
        /// <param name="eventArgs">A <see cref="BasicDeliverEventArgs"/> containing event information</param>
        private void consumer_OnReceived(object sender, BasicDeliverEventArgs eventArgs)
        {
            if (eventArgs.Body == null || !eventArgs.Body.Any())
            {
                ExecutionLog.WarnFormat("A message with {0} body received. Aborting message processing", eventArgs.Body == null ? "null" : "empty");
                return;
            }

            var receivedAt = SdkInfo.ToEpochTime(DateTime.Now);

            // NOT used for GetRawMessage()
            var         sessionName = _interest == null ? "system" : _interest.Name;
            string      messageBody = null;
            FeedMessage feedMessage;
            IProducer   producer;
            string      messageName;

            try
            {
                using (var t = Metric.Context("FEED")
                               .Timer("Message deserialization time", Unit.Items, SamplingType.Default, TimeUnit.Minutes)
                               .NewContext(eventArgs.RoutingKey))
                {
                    messageBody = Encoding.UTF8.GetString(eventArgs.Body);
                    feedMessage = _deserializer.Deserialize(new MemoryStream(eventArgs.Body));
                    producer    = _producerManager.Get(feedMessage.ProducerId);
                    messageName = feedMessage.GetType().Name;
                    if (!string.IsNullOrEmpty(feedMessage.EventId) && URN.TryParse(feedMessage.EventId, out URN eventUrn))
                    {
                        feedMessage.EventURN = eventUrn;
                    }
                    if (_keyParser.TryGetSportId(eventArgs.RoutingKey, messageName, out var sportId))
                    {
                        feedMessage.SportId = sportId;
                    }
                    if (t.Elapsed.TotalMilliseconds > 300)
                    {
                        var marketCounts  = 0;
                        var outcomeCounts = 0;
                        if (feedMessage is odds_change oddsChange)
                        {
                            marketCounts  = oddsChange.odds?.market?.Length ?? 0;
                            outcomeCounts = oddsChange.odds?.market?.Where(w => w.outcome != null).SelectMany(list => list.outcome).Count() ?? 0;
                        }

                        if (feedMessage is bet_settlement betSettlement)
                        {
                            marketCounts  = betSettlement.outcomes?.Length ?? 0;
                            outcomeCounts = betSettlement.outcomes?.Where(w => w.Items != null).SelectMany(list => list.Items).Count() ?? 0;
                        }

                        ExecutionLog.Debug(
                            $"Deserialization of {feedMessage.GetType().Name} for {feedMessage.EventId} ({feedMessage.GeneratedAt}) and sport {sportId} took {t.Elapsed.TotalMilliseconds}ms. Markets={marketCounts}, Outcomes={outcomeCounts}");
                    }
                }

                if (producer.IsAvailable && !producer.IsDisabled)
                {
                    FeedLog.Info($"<~> {sessionName} <~> {eventArgs.RoutingKey} <~> {messageBody}");
                }
                else
                {
                    if (FeedLog.IsDebugEnabled)
                    {
                        FeedLog.Debug($"<~> {sessionName} <~> {eventArgs.RoutingKey} <~> {producer.Id}");
                    }
                }

                if (eventArgs.BasicProperties?.Headers != null)
                {
                    feedMessage.SentAt = eventArgs.BasicProperties.Headers.ContainsKey("timestamp_in_ms")
                                             ? long.Parse(eventArgs.BasicProperties.Headers["timestamp_in_ms"].ToString())
                                             : feedMessage.GeneratedAt;
                }
                feedMessage.ReceivedAt = receivedAt;
                Metric.Context("FEED").Meter("Message received", Unit.Items).Mark(messageName);
            }
            catch (DeserializationException ex)
            {
                ExecutionLog.Error($"Failed to parse message. RoutingKey={eventArgs.RoutingKey} Message: {messageBody}", ex);
                Metric.Context("FEED").Meter("Message deserialization exception", Unit.Items).Mark();
                RaiseDeserializationFailed(eventArgs.Body);
                return;
            }
            catch (Exception ex)
            {
                ExecutionLog.Error($"Error consuming feed message. RoutingKey={eventArgs.RoutingKey} Message: {messageBody}", ex);
                Metric.Context("FEED").Meter("Exception consuming feed message", Unit.Items).Mark(eventArgs.RoutingKey);
                RaiseDeserializationFailed(eventArgs.Body);
                return;
            }

            // send RawFeedMessage if needed
            try
            {
                if (producer.IsAvailable && !producer.IsDisabled)
                {
                    //ExecutionLog.LogDebug($"Raw msg [{_interest}]: {feedMessage.GetType().Name} for event {feedMessage.EventId}.");
                    var args = new RawFeedMessageEventArgs(eventArgs.RoutingKey, feedMessage, sessionName);
                    RawFeedMessageReceived?.Invoke(this, args);
                }
            }
            catch (Exception e)
            {
                ExecutionLog.Error($"Error dispatching raw message for {feedMessage.EventId}", e);
            }
            // continue normal processing

            if (!_producerManager.Exists(feedMessage.ProducerId))
            {
                ExecutionLog.Warn($"A message for producer which is not defined was received. Producer={feedMessage.ProducerId}");
                return;
            }

            if (!_useReplay && (!producer.IsAvailable || producer.IsDisabled))
            {
                ExecutionLog.Debug($"A message for producer which is disabled was received. Producer={producer}, MessageType={messageName}");
                return;
            }

            ExecutionLog.Info($"Message received. Message=[{feedMessage}].");
            if (feedMessage.IsEventRelated)
            {
                if (!string.IsNullOrEmpty(eventArgs.RoutingKey) && _keyParser.TryGetSportId(eventArgs.RoutingKey, messageName, out var sportId))
                {
                    feedMessage.SportId = sportId;
                }
                else
                {
                    ExecutionLog.Warn($"Failed to parse the SportId from the routing key. RoutingKey={eventArgs.RoutingKey}, message=[{feedMessage}]. SportId will not be set.");
                }
            }

            RaiseMessageReceived(feedMessage, eventArgs.Body);
        }
예제 #35
0
 public SampleMetrics()
 {
     // define a simple gauge that will provide the instant value of this.someValue when requested
     Metric.Gauge("SampleMetrics.DataValue", () => this.someValue, new Unit("$"));
 }
예제 #36
0
 public Task GaugeAsync(string name, double value)
 {
     Metric.Gauge(name, () => value, Unit.None);
     return(Task.CompletedTask);
 }
예제 #37
0
 public virtual void Visit(Metric data)
 {
 }
 public Task Log(Metric metric)
 {
     Metrics.Add(metric);
     return(Task.CompletedTask);
 }
예제 #39
0
 /// <summary>
 /// MultiAnalysisParam defines one kind of analysis to run in a MultiAnalysis request.
 /// </summary>
 /// <param name="label">A user defined string that acts as a name for the analysis.
 /// This will be returned in the results so the various analyses are easily identifiable.</param>
 /// <param name="analysis">The metric type.</param>
 public MultiAnalysisParam(string label, Metric analysis)
 {
     Label          = label;
     Analysis       = analysis;
     TargetProperty = analysis.TargetProperty;
 }
 public TDescriptor Metric(Metric metric)
 {
     Self.Metric = metric;
     return((TDescriptor)this);
 }
        /// <summary>
        /// The List Metric operation lists the metric value sets for the
        /// resource metrics.
        /// </summary>
        /// <param name='resourceUri'>
        /// Required. The resource identifier of the target resource to get
        /// metrics for.
        /// </param>
        /// <param name='filterString'>
        /// Optional. An OData $filter expression that supports querying by the
        /// name, startTime, endTime and timeGrain of the metric value sets.
        /// For example, "(name.value eq 'Percentage CPU') and startTime eq
        /// 2014-07-02T01:00Z and endTime eq 2014-08-21T01:00:00Z and
        /// timeGrain eq duration'PT1H'". In the expression, startTime,
        /// endTime and timeGrain are required. Name is optional.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Metric values operation response.
        /// </returns>
        public async Task <MetricListResponse> GetMetricsAsync(string resourceUri, string filterString, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceUri", resourceUri);
                tracingParameters.Add("filterString", filterString);
                TracingAdapter.Enter(invocationId, this, "GetMetricsAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/";
            url = url + resourceUri;
            url = url + "/metrics";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2014-04-01");
            List <string> odataFilter = new List <string>();

            if (filterString != null)
            {
                odataFilter.Add(Uri.EscapeDataString(filterString));
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-04-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MetricListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new MetricListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            MetricCollection metricCollectionInstance = new MetricCollection();
                            result.MetricCollection = metricCollectionInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Metric metricInstance = new Metric();
                                    metricCollectionInstance.Value.Add(metricInstance);

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString nameInstance = new LocalizableString();
                                        metricInstance.Name = nameInstance;

                                        JToken valueValue2 = nameValue["value"];
                                        if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue2);
                                            nameInstance.Value = valueInstance;
                                        }

                                        JToken localizedValueValue = nameValue["localizedValue"];
                                        if (localizedValueValue != null && localizedValueValue.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance = ((string)localizedValueValue);
                                            nameInstance.LocalizedValue = localizedValueInstance;
                                        }
                                    }

                                    JToken unitValue = valueValue["unit"];
                                    if (unitValue != null && unitValue.Type != JTokenType.Null)
                                    {
                                        Unit unitInstance = ((Unit)Enum.Parse(typeof(Unit), ((string)unitValue), true));
                                        metricInstance.Unit = unitInstance;
                                    }

                                    JToken timeGrainValue = valueValue["timeGrain"];
                                    if (timeGrainValue != null && timeGrainValue.Type != JTokenType.Null)
                                    {
                                        TimeSpan timeGrainInstance = XmlConvert.ToTimeSpan(((string)timeGrainValue));
                                        metricInstance.TimeGrain = timeGrainInstance;
                                    }

                                    JToken startTimeValue = valueValue["startTime"];
                                    if (startTimeValue != null && startTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTime startTimeInstance = ((DateTime)startTimeValue);
                                        metricInstance.StartTime = startTimeInstance;
                                    }

                                    JToken endTimeValue = valueValue["endTime"];
                                    if (endTimeValue != null && endTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTime endTimeInstance = ((DateTime)endTimeValue);
                                        metricInstance.EndTime = endTimeInstance;
                                    }

                                    JToken metricValuesArray = valueValue["metricValues"];
                                    if (metricValuesArray != null && metricValuesArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken metricValuesValue in ((JArray)metricValuesArray))
                                        {
                                            MetricValue metricValueInstance = new MetricValue();
                                            metricInstance.MetricValues.Add(metricValueInstance);

                                            JToken timestampValue = metricValuesValue["timestamp"];
                                            if (timestampValue != null && timestampValue.Type != JTokenType.Null)
                                            {
                                                DateTime timestampInstance = ((DateTime)timestampValue);
                                                metricValueInstance.Timestamp = timestampInstance;
                                            }

                                            JToken averageValue = metricValuesValue["average"];
                                            if (averageValue != null && averageValue.Type != JTokenType.Null)
                                            {
                                                double averageInstance = ((double)averageValue);
                                                metricValueInstance.Average = averageInstance;
                                            }

                                            JToken minimumValue = metricValuesValue["minimum"];
                                            if (minimumValue != null && minimumValue.Type != JTokenType.Null)
                                            {
                                                double minimumInstance = ((double)minimumValue);
                                                metricValueInstance.Minimum = minimumInstance;
                                            }

                                            JToken maximumValue = metricValuesValue["maximum"];
                                            if (maximumValue != null && maximumValue.Type != JTokenType.Null)
                                            {
                                                double maximumInstance = ((double)maximumValue);
                                                metricValueInstance.Maximum = maximumInstance;
                                            }

                                            JToken totalValue = metricValuesValue["total"];
                                            if (totalValue != null && totalValue.Type != JTokenType.Null)
                                            {
                                                double totalInstance = ((double)totalValue);
                                                metricValueInstance.Total = totalInstance;
                                            }

                                            JToken countValue = metricValuesValue["count"];
                                            if (countValue != null && countValue.Type != JTokenType.Null)
                                            {
                                                long countInstance = ((long)countValue);
                                                metricValueInstance.Count = countInstance;
                                            }

                                            JToken lastValue = metricValuesValue["last"];
                                            if (lastValue != null && lastValue.Type != JTokenType.Null)
                                            {
                                                double lastInstance = ((double)lastValue);
                                                metricValueInstance.Last = lastInstance;
                                            }

                                            JToken propertiesSequenceElement = ((JToken)metricValuesValue["properties"]);
                                            if (propertiesSequenceElement != null && propertiesSequenceElement.Type != JTokenType.Null)
                                            {
                                                foreach (JProperty property in propertiesSequenceElement)
                                                {
                                                    string propertiesKey   = ((string)property.Name);
                                                    string propertiesValue = ((string)property.Value);
                                                    metricValueInstance.Properties.Add(propertiesKey, propertiesValue);
                                                }
                                            }
                                        }
                                    }

                                    JToken resourceIdValue = valueValue["resourceId"];
                                    if (resourceIdValue != null && resourceIdValue.Type != JTokenType.Null)
                                    {
                                        string resourceIdInstance = ((string)resourceIdValue);
                                        metricInstance.ResourceId = resourceIdInstance;
                                    }

                                    JToken propertiesSequenceElement2 = ((JToken)valueValue["properties"]);
                                    if (propertiesSequenceElement2 != null && propertiesSequenceElement2.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property2 in propertiesSequenceElement2)
                                        {
                                            string propertiesKey2   = ((string)property2.Name);
                                            string propertiesValue2 = ((string)property2.Value);
                                            metricInstance.Properties.Add(propertiesKey2, propertiesValue2);
                                        }
                                    }

                                    JToken dimensionNameValue = valueValue["dimensionName"];
                                    if (dimensionNameValue != null && dimensionNameValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString dimensionNameInstance = new LocalizableString();
                                        metricInstance.DimensionName = dimensionNameInstance;

                                        JToken valueValue3 = dimensionNameValue["value"];
                                        if (valueValue3 != null && valueValue3.Type != JTokenType.Null)
                                        {
                                            string valueInstance2 = ((string)valueValue3);
                                            dimensionNameInstance.Value = valueInstance2;
                                        }

                                        JToken localizedValueValue2 = dimensionNameValue["localizedValue"];
                                        if (localizedValueValue2 != null && localizedValueValue2.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance2 = ((string)localizedValueValue2);
                                            dimensionNameInstance.LocalizedValue = localizedValueInstance2;
                                        }
                                    }

                                    JToken dimensionValueValue = valueValue["dimensionValue"];
                                    if (dimensionValueValue != null && dimensionValueValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString dimensionValueInstance = new LocalizableString();
                                        metricInstance.DimensionValue = dimensionValueInstance;

                                        JToken valueValue4 = dimensionValueValue["value"];
                                        if (valueValue4 != null && valueValue4.Type != JTokenType.Null)
                                        {
                                            string valueInstance3 = ((string)valueValue4);
                                            dimensionValueInstance.Value = valueInstance3;
                                        }

                                        JToken localizedValueValue3 = dimensionValueValue["localizedValue"];
                                        if (localizedValueValue3 != null && localizedValueValue3.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance3 = ((string)localizedValueValue3);
                                            dimensionValueInstance.LocalizedValue = localizedValueInstance3;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
예제 #42
0
        public override void Load()
        {
            //Need to be initialized before using any regex!
            new RegexTimeoutInitializer().Init();
            Kernel.Bind(typeof(DisposableCollection <,>)).ToSelf().InSingletonScope();
            if (Kernel.CanResolve <Func <long, DateTime> >() == false)
            {
                Kernel.Load <FuncModule>();
            }

            this.BindClassesAsSingleton(
                NonSingletonBaseTypes,
                typeof(ConfigurationAssembly),
                typeof(ServiceProxyAssembly));

            this.BindInterfacesAsSingleton(
                NonSingletonBaseTypes,
                new List <Type> {
                typeof(ILog)
            },
                typeof(ConfigurationAssembly),
                typeof(ServiceProxyAssembly),
                typeof(SharedLogicAssembly),
                typeof(ServiceDiscoveryAssembly));


            Bind <IRemoteHostPoolFactory>().ToFactory();

            Kernel.BindPerKey <string, ReportingStrategy, IPassiveAggregatingHealthCheck, PassiveAggregatingHealthCheck>();

            Kernel.BindPerKey <string, ReachabilityCheck, IMultiEnvironmentServiceDiscovery, MultiEnvironmentServiceDiscovery>();
            Kernel.BindPerKey <string, ReachabilityChecker, IServiceDiscovery, ServiceDiscovery.ServiceDiscovery>();
            Kernel.Bind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => HttpClientConfiguration =>
            {
                var clientHandler = new HttpClientHandler();
                if (HttpClientConfiguration.UseHttps)
                {
                    var httpAuthenticator = c.Kernel.Get <IHttpsAuthenticator>();
                    httpAuthenticator.AddHttpMessageHandlerAuthentication(clientHandler, HttpClientConfiguration);
                }
                return(clientHandler);
            });
            Kernel.BindPerString <IServiceProxyProvider, ServiceProxyProvider>();
            Kernel.BindPerString <AggregatingHealthStatus>();

            Rebind <MetricsContext>()
            .ToMethod(c => Metric.Context(GetTypeOfTarget(c).Name))
            .InScope(GetTypeOfTarget);

            Rebind <IServiceDiscoverySource>().To <ConsulDiscoverySource>().InTransientScope();
            Bind <IServiceDiscoverySource>().To <LocalDiscoverySource>().InTransientScope();
            Bind <IServiceDiscoverySource>().To <ConfigDiscoverySource>().InTransientScope();

            Bind <INodeSourceFactory>().To <ConsulNodeSourceFactory>().InTransientScope();
            Rebind <ILoadBalancer>().To <LoadBalancer>().InTransientScope();
            Rebind <NodeMonitoringState>().ToSelf().InTransientScope();
            Bind <IDiscovery>().To <Discovery>().InSingletonScope();

            Rebind <ServiceDiscovery.Rewrite.ConsulClient, ServiceDiscovery.Rewrite.IConsulClient>()
            .To <ServiceDiscovery.Rewrite.ConsulClient>().InSingletonScope();


            Kernel.Rebind <IConsulClient>().To <ConsulClient>().InTransientScope();
            Kernel.Load <ServiceProxyModule>();

            Kernel.Rebind <IConfigObjectsCache>().To <ConfigObjectsCache>().InSingletonScope();
            Kernel.Rebind <IConfigObjectCreator>().To <ConfigObjectCreator>().InTransientScope();
            Kernel.Bind <IConfigEventFactory>().To <ConfigEventFactory>();
            Kernel.Bind <IConfigFuncFactory>().ToFactory();

            // ServiceSchema is at ServiceContracts, and cannot be depended on IServiceInterfaceMapper, which belongs to Microdot
            Kernel.Rebind <ServiceSchema>()
            .ToMethod(c => new ServiceSchema(c.Kernel.Get <IServiceInterfaceMapper>().ServiceInterfaceTypes.ToArray())).InSingletonScope();

            Kernel.Rebind <SystemInitializer.SystemInitializer>().ToSelf().InSingletonScope();
        }
예제 #43
0
 public void Add <TCommandType, T>(string name, T value, double sampleRate = 1.0, string[] tags = null) where TCommandType : Metric
 {
     _commands.Add(Metric.GetCommand <TCommandType, T>(_prefix, name, value, sampleRate, _constantTags, tags));
 }
        /// <summary>
        /// Pre-initialize this extractor.
        /// </summary>
        /// <param name="metricTelemetryClient">The <c>TelemetryClient</c> to be used for sending extracted metrics.</param>
        public void InitializeExtractor(TelemetryClient metricTelemetryClient)
        {
            if (metricTelemetryClient == null)
            {
                this.dependencyCallDurationMetric = null;
                return;
            }

            if (!this.isInitialized)
            {
                lock (this.lockObject)
                {
                    if (!this.isInitialized)
                    {
                        this.dimensionExtractors.Add(new DependencyMetricIdDimensionExtractor());
                        this.dimensionExtractors.Add(new SuccessDimensionExtractor());
                        this.dimensionExtractors.Add(new DependencyDurationBucketExtractor());
                        this.dimensionExtractors.Add(new SyntheticDimensionExtractor());
                        this.dimensionExtractors.Add(new TypeDimensionExtractor()
                        {
                            MaxValues = this.MaxDependencyTypesToDiscover
                        });
                        this.dimensionExtractors.Add(new TargetDimensionExtractor()
                        {
                            MaxValues = this.MaxDependencyTargetValuesToDiscover
                        });
                        this.dimensionExtractors.Add(new CloudRoleInstanceDimensionExtractor()
                        {
                            MaxValues = this.MaxCloudRoleInstanceValuesToDiscover
                        });
                        this.dimensionExtractors.Add(new CloudRoleNameDimensionExtractor()
                        {
                            MaxValues = this.MaxCloudRoleNameValuesToDiscover
                        });

                        int   seriesCountLimit        = 1;
                        int[] valuesPerDimensionLimit = new int[this.dimensionExtractors.Count];
                        int   i = 0;

                        foreach (var dim in this.dimensionExtractors)
                        {
                            int dimLimit = 1;

                            dimLimit                     = dim.MaxValues == 0 ? 1 : dim.MaxValues;
                            seriesCountLimit             = seriesCountLimit * (1 + dimLimit);
                            valuesPerDimensionLimit[i++] = dimLimit;
                        }

                        MetricConfiguration config = new MetricConfigurationForMeasurement(
                            seriesCountLimit,
                            valuesPerDimensionLimit,
                            new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false));
                        config.ApplyDimensionCapping = true;
                        config.DimensionCappedString = MetricTerms.Autocollection.Common.PropertyValues.DimensionCapFallbackValue;

                        IList <string> dimensionNames = new List <string>(this.dimensionExtractors.Count);
                        for (i = 0; i < this.dimensionExtractors.Count; i++)
                        {
                            dimensionNames.Add(this.dimensionExtractors[i].Name);
                        }

                        MetricIdentifier metricIdentifier = new MetricIdentifier(MetricIdentifier.DefaultMetricNamespace,
                                                                                 MetricTerms.Autocollection.Metric.DependencyCallDuration.Name,
                                                                                 dimensionNames);

                        this.dependencyCallDurationMetric = metricTelemetryClient.GetMetric(
                            metricIdentifier: metricIdentifier,
                            metricConfiguration: config,
                            aggregationScope: MetricAggregationScope.TelemetryClient);
                        this.isInitialized = true;
                    }
                }
            }
        }
예제 #45
0
        /// <summary>
        /// Initializes a new instance of the type
        /// </summary>
        /// <param name="metric">The metric the writer is associated with</param>
        /// <param name="labels">The labels that the metric has</param>
        internal DimensionalWriter(Metric metric, string[] labels)
        {
            _metric = metric;
            _labels = labels;

            switch (_labels?.Length ?? 0)
            {
            case 0:
                _labelRecorderLong   = RecordMetric0LabelsLong;
                _labelRecorderDouble = RecordMetric0LabelsDouble;
                break;

            case 1:
                _labelRecorderLong   = RecordMetric1LabelsLong;
                _labelRecorderDouble = RecordMetric1LabelsDouble;
                break;

            case 2:
                _labelRecorderLong   = RecordMetric2LabelsLong;
                _labelRecorderDouble = RecordMetric2LabelsDouble;
                break;

            case 3:
                _labelRecorderLong   = RecordMetric3LabelsLong;
                _labelRecorderDouble = RecordMetric3LabelsDouble;
                break;

            case 4:
                _labelRecorderLong   = RecordMetric4LabelsLong;
                _labelRecorderDouble = RecordMetric4LabelsDouble;
                break;

            case 5:
                _labelRecorderLong   = RecordMetric5LabelsLong;
                _labelRecorderDouble = RecordMetric5LabelsDouble;
                break;

            case 6:
                _labelRecorderLong   = RecordMetric6LabelsLong;
                _labelRecorderDouble = RecordMetric6LabelsDouble;
                break;

            case 7:
                _labelRecorderLong   = RecordMetric7LabelsLong;
                _labelRecorderDouble = RecordMetric7LabelsDouble;
                break;

            case 8:
                _labelRecorderLong   = RecordMetric8LabelsLong;
                _labelRecorderDouble = RecordMetric8LabelsDouble;
                break;

            case 9:
                _labelRecorderLong   = RecordMetric9LabelsLong;
                _labelRecorderDouble = RecordMetric9LabelsDouble;
                break;

            default:
                _labelRecorderLong   = RecordMetric10LabelsLong;
                _labelRecorderDouble = RecordMetric10LabelsDouble;
                break;
            }
        }
예제 #46
0
 protected abstract void Populate(Metric metric);
예제 #47
0
        /// <summary>
        /// Train the model with training dataset, for certain number of iterations and using batch size
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="numIterations"></param>
        /// <param name="batchSize"></param>
        public void Train(NDArray x, NDArray y, int numIterations, int batchSize)
        {
            //Initialise bacch loss and metric list for temporary holding of result
            List <float> batchLoss    = new List <float>();
            List <float> batchMetrics = new List <float>();

            Stopwatch sw = new Stopwatch();

            //Loop through till the end of specified iterations
            for (int i = 1; i <= numIterations; i++)
            {
                sw.Start();

                //Initialize local variables
                int currentIndex = 0;
                batchLoss.Clear();
                batchMetrics.Clear();

                //Loop untill the data is exhauted for every batch selected
                while (x.Next(currentIndex, batchSize))
                {
                    //Get the batch data based on the specified batch size
                    var xtrain = x.Slice(currentIndex, batchSize);
                    var ytrain = y.Slice(currentIndex, batchSize);

                    //Run forward for all the layers to predict the value for the training set
                    var ypred = Forward(xtrain);

                    //Find the loss/cost value for the prediction wrt expected result
                    var costVal = Cost.Forward(ypred, ytrain);
                    batchLoss.AddRange(costVal.Data);

                    //Find the metric value for the prediction wrt expected result
                    if (Metric != null)
                    {
                        var metric = Metric.Calculate(ypred, ytrain);
                        batchMetrics.AddRange(metric.Data);
                    }

                    //Get the gradient of the cost function which is the passed to the layers during back-propagation
                    var grad = Cost.Backward(ypred, ytrain);

                    //Run back-propagation accross all the layers
                    Backward(grad);

                    //Now time to update the neural network weights using the specified optimizer function
                    foreach (var layer in Layers)
                    {
                        Optimizer.Update(i, layer);
                    }

                    currentIndex = currentIndex + batchSize;;
                }

                sw.Stop();
                //Collect the result and fire the event
                float batchLossAvg = (float)Math.Round(batchLoss.Average(), 2);

                float batchMetricAvg = Metric != null ? (float)Math.Round(batchMetrics.Average(), 2) : 0;

                TrainingLoss.Add(batchLossAvg);

                if (batchMetrics.Count > 0)
                {
                    TrainingMetrics.Add(batchMetricAvg);
                }

                EpochEndEventArgs eventArgs = new EpochEndEventArgs(i, batchLossAvg, batchMetricAvg, sw.ElapsedMilliseconds);
                EpochEnd?.Invoke(i, eventArgs);
                sw.Reset();
            }
        }
예제 #48
0
 public void ChangeMetric(Metric metric)
 {
     InfantMetric = metric;
 }
예제 #49
0
 public virtual void TearDown()
 {
     //clear TracingContext for testing only
     CallContext.FreeNamedDataSlot("#ORL_RC");
     Metric.ShutdownContext(ServiceProxyProvider.METRICS_CONTEXT_NAME);
 }
예제 #50
0
 public void Counter(string name, int value = 1)
 {
     Metric.Counter(name, Unit.None).Increment();
 }
예제 #51
0
        public async Task TestTagHashing()
        {
            /* Setup mocks */
            Metric[] testData = new Metric[0];
            var      scraper  = new Mock <IMetricsScraper>();

            scraper.Setup(s => s.ScrapeEndpointsAsync(CancellationToken.None)).ReturnsAsync(() => testData);

            var storage = new Mock <IMetricsStorage>();
            IEnumerable <Metric> storedValues = Enumerable.Empty <Metric>();

            storage.Setup(s => s.StoreMetricsAsync(It.IsAny <IEnumerable <Metric> >())).Callback((Action <IEnumerable <Metric> >)(data => storedValues = data)).Returns(Task.CompletedTask);

            var uploader = new Mock <IMetricsPublisher>();

            MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object);

            // test id hash
            var tags = new Dictionary <string, string>
            {
                { "ms_telemetry", true.ToString() },
                { "not_hashed", "1" },
                { "id", "my_device1/my_module_1" },
            };

            testData = new Metric[] { new Metric(DateTime.UtcNow, "test_metric", 0, tags) };
            await worker.Scrape(CancellationToken.None);

            Assert.Contains(new KeyValuePair <string, string>("not_hashed", "1"), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("id", "device/Ut4Ug5Wg2qMCvwtG08RIi0k10DkoNMqQ7AmTUKy/pMs="), storedValues.Single().Tags);

            // test id not hash edgeAgent
            tags = new Dictionary <string, string>
            {
                { "ms_telemetry", true.ToString() },
                { "not_hashed", "1" },
                { "id", "my_device1/$edgeAgent" },
            };
            testData = new Metric[] { new Metric(DateTime.UtcNow, "test_metric", 0, tags) };
            await worker.Scrape(CancellationToken.None);

            Assert.Contains(new KeyValuePair <string, string>("not_hashed", "1"), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("id", "device/$edgeAgent"), storedValues.Single().Tags);

            // test module_name hash
            tags = new Dictionary <string, string>
            {
                { "ms_telemetry", true.ToString() },
                { "not_hashed", "1" },
                { "module_name", "my_module" },
            };
            testData = new Metric[] { new Metric(DateTime.UtcNow, "test_metric", 0, tags) };
            await worker.Scrape(CancellationToken.None);

            Assert.Contains(new KeyValuePair <string, string>("not_hashed", "1"), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("module_name", "rPbHx4uTZz/x2x8rSxfPxL4egT61y7B1dlsSgWpHh6s="), storedValues.Single().Tags);

            // test module name not hash edgeAgent
            tags = new Dictionary <string, string>
            {
                { "ms_telemetry", true.ToString() },
                { "not_hashed", "1" },
                { "module_name", "$edgeAgent" },
            };
            testData = new Metric[] { new Metric(DateTime.UtcNow, "test_metric", 0, tags) };
            await worker.Scrape(CancellationToken.None);

            Assert.Contains(new KeyValuePair <string, string>("not_hashed", "1"), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("module_name", "$edgeAgent"), storedValues.Single().Tags);

            // test to from hash
            tags = new Dictionary <string, string>
            {
                { "ms_telemetry", true.ToString() },
                { "not_hashed", "1" },
                { "to", "my_module_1" },
                { "from", "my_module_2" },
                { "to_route_input", "my_module_1" },
                { "from_route_output", "my_module_2" },
            };
            testData = new Metric[] { new Metric(DateTime.UtcNow, "test_metric", 0, tags) };
            await worker.Scrape(CancellationToken.None);

            Assert.Contains(new KeyValuePair <string, string>("not_hashed", "1"), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("to", "Ut4Ug5Wg2qMCvwtG08RIi0k10DkoNMqQ7AmTUKy/pMs="), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("from", "t+TD1s4uqQrTHY7Xe/lJqasX1biQ9yK4ev5ZnScMcpk="), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("to_route_input", "Ut4Ug5Wg2qMCvwtG08RIi0k10DkoNMqQ7AmTUKy/pMs="), storedValues.Single().Tags);
            Assert.Contains(new KeyValuePair <string, string>("from_route_output", "t+TD1s4uqQrTHY7Xe/lJqasX1biQ9yK4ev5ZnScMcpk="), storedValues.Single().Tags);
        }
예제 #52
0
 public MetricDef(Metric metric, bool upperBound, long upperValue)
     : this(metric, upperBound, upperValue, false, 0)
 {
 }
예제 #53
0
 protected override void Populate(Metric metric)
 {
     metric.gauge       = new Advanced.DataContracts.Gauge();
     metric.gauge.value = Value;
 }
예제 #54
0
        public string GetReadyForOrdinal(bool saveLoadedData = true)
        {
            if (!ReadyForNumerical)
            {
                GetReadyForNumerical();
            }
            if (ReadyForOrdinal)
            {
                return("Is ready.");
            }

            StringBuilder log = new StringBuilder();

            Utils.StartTimer();
            log.AppendLine(Utils.PrintHeading("Prepare preferecen relation data"));

            Console.WriteLine("Converting R_train into PR_train");
            log.AppendLine("Converting R_train into PR_train");
            PR_train = PrefRelations.CreateDiscrete(R_train);

            //Console.WriteLine("Converting R_test into PR_test");
            //log.AppendLine("Converting R_test into PR_test");
            //PR_test = PrefRelations.CreateDiscrete(R_test);

            log.AppendLine(Utils.StopTimer());

            #region Prepare similarity data
            if (File.Exists(GetDataFileName("USP")) &&
                File.Exists(GetDataFileName("ISP")) &&
                File.Exists(GetDataFileName("SSIIP")))
            {
                Utils.StartTimer();
                Utils.PrintHeading("Load user, item, indicators variables (Pref based)");
                UserSimilaritiesOfPref = Utils.IO <SimilarityData> .LoadObject(GetDataFileName("USP"));

                ItemSimilaritiesOfPref = Utils.IO <SimilarityData> .LoadObject(GetDataFileName("ISP"));

                StrongSimilarityIndicatorsByItemPref = Utils.IO <HashSet <Tuple <int, int> > > .LoadObject(GetDataFileName("SSIIP"));

                Utils.StopTimer();
            }
            else
            {
                Utils.StartTimer();
                Utils.PrintHeading("Compute user-user similarities (Pref based)");
                Metric.GetCosineOfPrefRelations(PR_train, MaxCountOfNeighbors,
                                                StrongSimilarityThreshold, out UserSimilaritiesOfPref);
                Utils.StopTimer();

                // For the moment, we use user-wise preferences to compute
                // item-item similarities, it is not the same as user-user pref similarities
                Utils.StartTimer();
                Utils.PrintHeading("Compute item-item similarities (Pref based)");
                DataMatrix PR_userwise_preferences = new DataMatrix(PR_train.GetPositionMatrix());
                Metric.GetPearsonOfColumns(PR_userwise_preferences, MaxCountOfNeighbors, StrongSimilarityThreshold,
                                           out ItemSimilaritiesOfPref, out StrongSimilarityIndicatorsByItemPref);
                Utils.StopTimer();

                if (saveLoadedData)
                {
                    Utils.IO <SimilarityData> .SaveObject(UserSimilaritiesOfPref, GetDataFileName("USP"));

                    Utils.IO <SimilarityData> .SaveObject(ItemSimilaritiesOfPref, GetDataFileName("ISP"));

                    Utils.IO <HashSet <Tuple <int, int> > >
                    .SaveObject(StrongSimilarityIndicatorsByItemPref, GetDataFileName("SSIIP"));
                }
                Utils.StopTimer();
            }
            #endregion



            ReadyForOrdinal = true;

            return(log.ToString());
        }
예제 #55
0
 public Task Close()
 {
     Metric.ShutdownContext("Silo");
     return(TaskDone.Done);
 }
예제 #56
0
 public void Gauge(string name, double value)
 {
     Metric.Gauge(name, () => value, Unit.None);
 }
예제 #57
0
 // adds a metric to the logger using a metric object
 public void AddMetricToLogger(Metric metric)
 {
     AddMetric(metric.name, metric.value);
 }
예제 #58
0
 public MetricDef(Metric metric)
     : this(metric, false, 0, false, 0)
 {
 }
예제 #59
0
 public virtual void TearDown()
 {
     _testinghost.Stop();
     _stopTask.Wait();
     Metric.ShutdownContext("Service");
 }
예제 #60
0
 public Task CounterAsync(string name, int value = 1)
 {
     Metric.Counter(name, Unit.None).Increment();
     return(Task.CompletedTask);
 }