コード例 #1
0
        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(string input, ILambdaContext context)
        {
            var envProvider  = new EnvironmentProvider(EnvironmentConfigurationProvider.Config, new ResourceFetcher());
            var logger       = new MetricsLogger();
            var dimensionSet = new DimensionSet();

            dimensionSet.AddDimension("Service", "Aggregator");
            dimensionSet.AddDimension("Region", "us-west-2");
            logger.PutDimensions(dimensionSet);
            logger.SetNamespace("EMFLambda");
            logger.PutMetric("ProcessingLatency", 101, Unit.MILLISECONDS);
            logger.PutMetric("ProcessingLatency", 100, Unit.MILLISECONDS);
            logger.PutMetric("ProcessingLatency", 99, Unit.MILLISECONDS);
            logger.PutMetric("Count", 3, Unit.COUNT);
            logger.PutProperty("AccountId", "123456789");
            logger.PutProperty("RequestId", "422b1569-16f6-4a03-b8f0-fe3fd9b100f8");
            logger.PutProperty("DeviceId", "61270781-c6ac-46f1-baf7-22c808af8162");
            Dictionary <string, object> payLoad = new Dictionary <string, object>
            {
                { "sampleTime", 123456789 },
                { "temperature", 273.0 },
                { "pressure", 101.3 }
            };

            logger.PutProperty("Payload", payLoad);
            logger.Flush();
            return(input?.ToUpper());
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var init = true;

            var configuration = new Configuration
            {
                LogGroupName        = "/Canary/Dotnet/CloudWatchAgent/Metrics",
                EnvironmentOverride = Environments.ECS,
                AgentEndPoint       = "tcp://127.0.0.1:25888"
            };

            var loggerFactory = LoggerFactory.Create(builder =>
                                                     builder
                                                     .SetMinimumLevel(LogLevel.Information)
                                                     .AddConsole());

            EnvironmentConfigurationProvider.Config = configuration;

            // get the assembly version (this does not reflect NuGet pre-releases)
            var packageVersion = GetPackageVersion();

            while (true)
            {
                using (var logger = new MetricsLogger(loggerFactory))
                {
                    logger.SetNamespace("Canary");

                    var dimensionSet = new DimensionSet();
                    dimensionSet.AddDimension("Runtime", "Dotnet");
                    dimensionSet.AddDimension("Platform", "ECS");
                    dimensionSet.AddDimension("Agent", "CloudWatchAgent");
                    dimensionSet.AddDimension("Version", packageVersion);
                    logger.SetDimensions(dimensionSet);

                    using (var currentProcess = System.Diagnostics.Process.GetCurrentProcess())
                    {
                        // https://github.com/dotnet/corefx/blob/3633ea2c6bf9d52029681efeedd84fd7a06eb6ba/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs#L137
                        logger.PutMetric("Memory.RSS", currentProcess.WorkingSet64, Unit.BYTES);
                    }

                    logger.PutMetric("Invoke", 1, Unit.NONE);

                    if (init)
                    {
                        init = false;
                        logger.PutMetric("Init", 1, Unit.NONE);
                    }

                    logger.PutMetric("Memory.HeapUsed", GC.GetTotalMemory(false), Unit.BYTES);
                }
                Thread.Sleep(1_000);
            }
        }
コード例 #3
0
        public void TestPutMetricWithNoUnit()
        {
            string expectedKey   = "test";
            double expectedValue = 2.0;

            _metricsLogger.PutMetric(expectedKey, expectedValue);
            _metricsLogger.Flush();

            var metricDirective = typeof(MetricsContext)
                                  .GetField("_metricDirective", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                                  .GetValue(_sink.MetricsContext) as MetricDirective;

            var metricDefinition = metricDirective.Metrics.FirstOrDefault(m => m.Name == expectedKey);

            Assert.Equal(expectedValue, metricDefinition.Values[0]);
            Assert.Equal(Unit.NONE, metricDefinition.Unit);
        }
コード例 #4
0
        private void LogMetric(String metricName)
        {
            MetricsLogger logger = new MetricsLogger(new EnvironmentProvider(EnvironmentConfigurationProvider.Config, new ResourceFetcher()), NullLoggerFactory.Instance);

            logger.PutDimensions(_dimensions);
            logger.PutMetric(metricName, 100, Unit.MILLISECONDS);
            logger.Flush();
        }