private static void EmitMetrics(ILogger logger, IMetricsLogger metrics) { var dimensionSet = new DimensionSet(); dimensionSet.AddDimension("Service", "Aggregator"); dimensionSet.AddDimension("Region", "us-west-2"); metrics.SetDimensions(dimensionSet); metrics.PutMetric("ProcessingLatency", 101, Unit.MILLISECONDS); metrics.PutMetric("ProcessingLatency", 100, Unit.MILLISECONDS); metrics.PutMetric("ProcessingLatency", 99, Unit.MILLISECONDS); metrics.PutMetric("Count", 3, Unit.COUNT); metrics.PutProperty("AccountId", "123456789"); metrics.PutProperty("RequestId", "422b1569-16f6-4a03-b8f0-fe3fd9b100f8"); metrics.PutProperty("DeviceId", "61270781-c6ac-46f1-baf7-22c808af8162"); Dictionary <string, object> payLoad = new Dictionary <string, object> { { "sampleTime", 123456789 }, { "temperature", 273.0 }, { "pressure", 101.3 } }; metrics.PutProperty("Payload", payLoad); logger.LogInformation("Flushing"); metrics.Flush(); }
/// <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()); }
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); } }
/// <summary> /// Adds default dimensions and properties from the specified environment into the specified metrics context. /// </summary> /// <param name="context">the context to configure with environment information</param> private void ConfigureContextForEnvironment(MetricsContext context) { if (context.HasDefaultDimensions) { return; } var defaultDimensions = new DimensionSet(); defaultDimensions.AddDimension("ServiceName", _environment.Name); defaultDimensions.AddDimension("ServiceType", _environment.Type); context.DefaultDimensions = defaultDimensions; _environment.ConfigureContext(context); }
public void MetricDirective_AddDimensions_Returns_ValidJson() { MetricDirective metricDirective = new MetricDirective(); DimensionSet ds = new DimensionSet("Region", "US-West-2"); ds.AddDimension("Instance", "instance-1"); metricDirective.CustomDimensionSets.Add(ds); string jsonString = JsonConvert.SerializeObject(metricDirective); Assert.Equal("{\"Namespace\":\"aws-embedded-metrics\",\"Metrics\":[],\"Dimensions\":[[\"Region\",\"Instance\"]]}", jsonString); }