public void CompareLambdaContextExtraction(string jsonContextFile) { var context = ContextUtils.FromJsonFile(jsonContextFile); var dpServerless = BuildDataPoint(); var dpLambda = BuildDataPoint(); dpServerless.AddDefaultDimensions(context); dpLambda.dimensions.AddRange(MetricWrapper.GetDefaultDimensions(context.ExtractCommonTags())); // Sort dimensions to make easy to compare. dpServerless.dimensions.Sort((d0, d1) => string.Compare(d0.key, d1.key)); dpLambda.dimensions.Sort((d0, d1) => string.Compare(d0.key, d1.key)); Assert.Equal(dpServerless.dimensions.Count, dpLambda.dimensions.Count); for (int i = 0; i < dpLambda.dimensions.Count; i++) { Assert.Equal(dpLambda.dimensions[i].key, dpServerless.dimensions[i].key); Assert.Equal(dpLambda.dimensions[i].value, dpServerless.dimensions[i].value); } }
public void RecommendedUsage(bool throwException) { var context = ContextUtils.FromJsonFile("./SampleContexts/lambda-context.json"); var mockSender = new Mock <ISignalFxReporter>(); DataPointUploadMessage actualDataPointMsg = null; mockSender.Setup(m => m.Send(It.IsNotNull <DataPointUploadMessage>())) .Callback <DataPointUploadMessage>(msg => actualDataPointMsg = msg); using (var wrapper = new MetricWrapper(context, mockSender.Object)) { // Add a custom data point. DataPoint dp = new DataPoint(); dp.metric = "custom.gauge"; dp.metricType = MetricType.GAUGE; dp.value = new Datum { intValue = 1 }; dp.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); wrapper.AddDataPoint(dp); try { if (throwException) { throw new ApplicationException("test"); } } catch (Exception) { wrapper.Error(); } } Assert.NotNull(actualDataPointMsg); var expectedDimensions = new Dictionary <string, string> { { "aws_execution_env", "" }, // Expected to be empty since the env var is not defined. { "aws_function_name", "sample-lambda-functions" }, { "aws_function_version", "$LATEST" }, { "aws_region", "us-west-2" }, { "aws_account_id", "123456789012" }, { "lambda_arn", "arn:aws:lambda:us-west-2:123456789012:function:sample-lambda-functions:$LATEST" }, // { "function_wrapper_version", "signalfx_lambda_3.0.1.0" }, { "metric_source", "lambda_wrapper" }, }; // It is hard to control for cold starts, it is exists check for the dimensions and remove it. var coldStartMetric = actualDataPointMsg.datapoints.FirstOrDefault(dp => dp.metric == "function.cold_starts"); if (coldStartMetric != null) { AssertDimensions(coldStartMetric, expectedDimensions); actualDataPointMsg.datapoints.Remove(coldStartMetric); } var expectedMetrics = new List <dynamic> { new { Name = "function.invocations", MetricType = MetricType.COUNTER }, new { Name = "function.duration", MetricType = MetricType.GAUGE }, new { Name = "custom.gauge", MetricType = MetricType.GAUGE }, }; if (throwException) { expectedMetrics.Add(new { Name = "function.errors", MetricType = MetricType.COUNTER }); } Assert.Equal(actualDataPointMsg.datapoints.Count, expectedMetrics.Count); foreach (var expectedMetric in expectedMetrics) { var actualMetric = actualDataPointMsg.datapoints.FirstOrDefault(dp => dp.metric == expectedMetric.Name); Assert.True(actualMetric != null, $"Expected metric {expectedMetric.Name} was not found."); Assert.Equal(expectedMetric.MetricType, actualMetric.metricType); AssertDimensions(actualMetric, expectedDimensions); } void AssertDimensions(DataPoint actualMetric, dynamic expectedDimensions) { Assert.Equal(actualMetric.dimensions.Count, expectedDimensions.Count); foreach (var expectedDimension in expectedDimensions) { var actualDimension = actualMetric.dimensions.FirstOrDefault(dim => dim.key == expectedDimension.Key); Assert.True(actualDimension != null, $"Expected dimension {expectedDimension.Key} was not found."); Assert.Equal(expectedDimension.Value, actualDimension.value); } } }