private void GenerateAndCollectSqlTrace(ImmutableTransaction immutableTransaction, TransactionMetricName transactionMetricName, TransactionMetricStatsCollection txStats) { if (!_configurationService.Configuration.SlowSqlEnabled) { return; } var txSqlTrStats = new SqlTraceStatsCollection(); foreach (var segment in immutableTransaction.Segments.Where(s => s.Data is DatastoreSegmentData)) { var datastoreSegmentData = (DatastoreSegmentData)segment.Data; if (datastoreSegmentData.CommandText != null && segment.Duration >= _configurationService.Configuration.SqlExplainPlanThreshold) { AddSqlTraceStats(txSqlTrStats, _sqlTraceMaker.TryGetSqlTrace(immutableTransaction, transactionMetricName, segment)); } } if (txSqlTrStats.Collection.Count > 0) { using (_agentTimerService.StartNew("CollectSqlTrace")) { _sqlTraceAggregator.Collect(txSqlTrStats); } MetricBuilder.TryBuildSqlTracesCollectedMetric(txSqlTrStats.TracesCollected, txStats); } }
public void BuildSummaryMetric() { var timestamp = DateTime.UtcNow; var interval = 33L; var value = MetricSummaryValue.Create(10d, 64, 3, 15); var attributes = new Dictionary <string, object> { { "attrKey", "attrValue" } }; var metricBuilder = MetricBuilder.CreateSummaryMetric("metricname") .WithValue(value) .WithTimestamp(timestamp) .WithIntervalMs(interval) .WithAttribute("adsfasdf", 12) .WithAttributes(attributes); var metric = metricBuilder.Build(); Assert.AreEqual("metricname", metric.Name); Assert.AreEqual("summary", metric.Type); Assert.AreEqual(value, metric.Value); Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(timestamp), metric.Timestamp); Assert.AreEqual(interval, metric.IntervalMs); Assert.AreEqual(12, metric.Attributes["adsfasdf"]); Assert.AreEqual("attrValue", metric.Attributes["attrKey"]); }
public override void AddMetricStats(Segment segment, TimeSpan durationOfChildren, TransactionMetricStatsCollection txStats, IConfigurationService configService) { var duration = segment.Duration.Value; var exclusiveDuration = TimeSpanMath.Max(TimeSpan.Zero, duration - durationOfChildren); MetricBuilder.TryBuildMessageBrokerSegmentMetric(Vendor, Destination, DestinationType, Action, duration, exclusiveDuration, txStats); }
public void WhenMoreThanTenDimensionsAreProvidedThenOnlyTenAreRecorded() { var serialiser = new Serialiser(new TestClock(), new MetricLoggerOptions()); var metric = MetricBuilder .Create() .SpecifyMetric("time", 100, Unit.Milliseconds) .AddDimension("One", "1") .AddDimension("Two", "2") .AddDimension("Three", "3") .AddDimension("Four", "4") .AddDimension("Five", "5") .AddDimension("Six", "6") .AddDimension("Seven", "7") .AddDimension("Eight", "8") .AddDimension("Nine", "9") .AddDimension("Ten", "10") .AddDimension("Eleven", "11") .AddDimension("Twelve", "12") .AddDimension("Thirteen", "13") .Build(); var json = serialiser.SerialiseMetric(metric); JObject .Parse(json) .SelectToken("$._aws.CloudWatchMetrics[0].Dimensions[0]") .ToObject <string[]>() .Should() .HaveCount(10, "CloudWatch accepts a maximum of 10 dimensions."); }
public override void AddMetricStats(Segment segment, TimeSpan durationOfChildren, TransactionMetricStatsCollection txStats, IConfigurationService configService) { var duration = segment.Duration.Value; var exclusiveDuration = TimeSpanMath.Max(TimeSpan.Zero, duration - durationOfChildren); MetricBuilder.TryBuildExternalRollupMetrics(Uri.Host, duration, txStats); // The CAT response data will be null if the agent did not receive a response that contained CAT headers (e.g. if the request went to an app that isn't monitored by a supported New Relic agent). // According to the agent spec, in the event when CAT response data is present, the agent generates ExternalTransaction/{host}/{cross_process_id}/{transaction_name} scoped metric to replace External/{host}/{method} scoped metric. if (CrossApplicationResponseData == null) { // Generate scoped and unscoped external metrics as CAT not present. MetricBuilder.TryBuildExternalSegmentMetric(Uri.Host, Method, duration, exclusiveDuration, txStats, false); } else { // Only generate unscoped metric for response with CAT headers because segments should only produce a single scoped metric and the CAT metric is more interesting than the external segment metric. MetricBuilder.TryBuildExternalSegmentMetric(Uri.Host, Method, duration, exclusiveDuration, txStats, true); var externalCrossProcessId = CrossApplicationResponseData.CrossProcessId; var externalTransactionName = CrossApplicationResponseData.TransactionName; MetricBuilder.TryBuildExternalAppMetric(Uri.Host, externalCrossProcessId, exclusiveDuration, txStats); MetricBuilder.TryBuildExternalTransactionMetric(Uri.Host, externalCrossProcessId, externalTransactionName, duration, exclusiveDuration, txStats); } }
public override Response pushMetrics(MetricBuilder builder, ExpectResponse expectResponse) { if (builder == null) { throw new Exception("QueryBuilder 不能为空"); } int tryTime = 1; SimpleHttpResponse response = null; while (tryTime <= maxTryTimes) { response = httpClient.doPost(buildUrl(serviceUrl, GetAuthUrl(PUT_POST_API), expectResponse), builder.build()); if (response.isSuccess()) { break; } else { log.ErrorFormat("[InspurTSDB]Get Response Error,Try Times:{0} ,Status Code: {1},Content: {2}", tryTime, response.getStatusCode(), response.getContent()); if (response.getStatusCode() == 401) { deleteCurrentToken(); } tryTime++; } } return(getResponse(response)); }
// Constructor public ApiCallMetricBuilder(string measurement = null) { // TODO: [TESTS] (ApiCallMetricBuilder.ApiCallMetricBuilder) Add tests _builder = new MetricBuilder(MetricSource.ApiCall, measurement) // Tags .WithTag(Tags.Controller, MetricPlaceholder.Unset) .WithTag(Tags.Action, MetricPlaceholder.Unset) .WithTag(Tags.RequestMethod, MetricPlaceholder.Unset) .WithTag(Tags.RequestContentType, MetricPlaceholder.None) .WithTag(Tags.RequestProtocol, MetricPlaceholder.Unset) .WithTag(Tags.RequestScheme, MetricPlaceholder.Unset) .WithTag(Tags.RequestHost, MetricPlaceholder.None) .WithTag(Tags.ResponseCode, 0) .WithTag(Tags.ResponseContentType, MetricPlaceholder.Unset) .WithTag(Tags.RanAction, false) .WithTag(Tags.RanResult, false) // Fields .WithField(CoreMetricField.Value, (double)0) .WithField(Fields.ActionTime, (double)0) .WithField(Fields.ResultTime, (double)0) .WithField(Fields.MiddlewareTime, (double)0) .WithField(Fields.ExceptionTime, (double)0) .WithField(Fields.RequestContentLength, (long)0) .WithField(Fields.RequestCookieCount, 0) .WithField(Fields.RequestHeaderCount, 0) .WithField(Fields.RequestPort, 0) .WithField(Fields.ResponseContentLength, (long)0) .WithField(Fields.ResponseHeaderCount, 0); }
public override void AddMetricStats(Segment segment, TimeSpan durationOfChildren, TransactionMetricStatsCollection txStats, IConfigurationService configService) { var duration = segment.Duration.Value; var exclusiveDuration = TimeSpanMath.Max(TimeSpan.Zero, duration - durationOfChildren); MetricBuilder.TryBuildCustomSegmentMetrics(Name, duration, exclusiveDuration, txStats); }
public void MetricBatchWithCommonPropertiesAndMetrics() { var timestamp = DateTime.UtcNow; var interval = 125L; var commonAttrs = new Dictionary <string, object>() { { "attr1Key", "attr1Value" } }; var countValue = 88d; var gaugeValue = 213d; var metricBatch = MetricBatchBuilder.Create() .WithTimestamp(timestamp) .WithIntervalMs(interval) .WithAttributes(commonAttrs) .WithMetric(MetricBuilder.CreateCountMetric("CountMetric") .WithValue(countValue) .Build()) .WithMetric(MetricBuilder.CreateGaugeMetric("GaugeMetric") .WithValue(gaugeValue) .Build()) .Build(); Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(timestamp), metricBatch.CommonProperties.Timestamp, $"MetricBatch CommonProperties [Timestamp] - expected: {timestamp}, actual: {metricBatch.CommonProperties.Timestamp}"); Assert.AreEqual(interval, metricBatch.CommonProperties.IntervalMs, $"MetricBatch CommonProperties [IntervalMs] - expected: {interval}, actual: {metricBatch.CommonProperties.IntervalMs}"); Assert.AreEqual(2, metricBatch.Metrics.Count, $"MetricBatch Metrics count - expected: 2, actual: {metricBatch.Metrics.Count}"); Assert.AreEqual("count", metricBatch.Metrics[0].Type, $"MetricBatch Metrics[0].Type - expected: count, actual: {metricBatch.Metrics[0].Type}"); Assert.AreEqual("gauge", metricBatch.Metrics[1].Type, $"MetricBatch Metrics[1].Type - expected: gauge, actual: {metricBatch.Metrics[1].Type}"); }
public void MetricBatchAllowsCommonAndSpecificSameNamedFields() { var commonTimestamp = DateTime.UtcNow; var commonInterval = 125L; var metricTimestamp = DateTime.UtcNow + TimeSpan.FromSeconds(60); var metricInterval = 312L; var countValue = 88d; var metricBatch = MetricBatchBuilder.Create() .WithTimestamp(commonTimestamp) .WithIntervalMs(commonInterval) .WithAttribute("Attr1Key", "comAttr1Value") .WithMetric(MetricBuilder.CreateCountMetric("CountMetric") .WithTimestamp(metricTimestamp) .WithIntervalMs(metricInterval) .WithAttribute("Attr1Key", "metAttr1Value") .WithValue(countValue) .Build()) .Build(); Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(commonTimestamp), metricBatch.CommonProperties.Timestamp, $"MetricBatch CommonProperties [Timestamp] - expected: {commonTimestamp}, actual: {metricBatch.CommonProperties.Timestamp}"); Assert.AreEqual(commonInterval, metricBatch.CommonProperties.IntervalMs, $"MetricBatch CommonProperties [IntervalMs] - expected: {commonInterval}, actual: {metricBatch.CommonProperties.IntervalMs}"); Assert.AreEqual("comAttr1Value", metricBatch.CommonProperties.Attributes["Attr1Key"], $"MetricBatch CommonProperties Attributes value - expected: comAttr1Value, actual: {metricBatch.CommonProperties.Attributes["Attr1Key"]}"); Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(metricTimestamp), metricBatch.Metrics[0].Timestamp, $"MetricBatch Metric [Timestamp] - expected: {DateTimeExtensions.ToUnixTimeMilliseconds(metricTimestamp)}, actual: {metricBatch.Metrics[0].Timestamp}"); Assert.AreEqual(metricInterval, metricBatch.Metrics[0].IntervalMs, $"MetricBatch Metric [IntervalMs] - expected: {metricInterval}, actual: {metricBatch.Metrics[0].IntervalMs}"); Assert.AreEqual("metAttr1Value", metricBatch.Metrics[0].Attributes["Attr1Key"], $"MetricBatch Metric Attributes value - expected: metAttr1Value, actual: {metricBatch.Metrics[0].Attributes["Attr1Key"]}"); }
public void WhenInvalidMetricNameIsProvidedThenTheMetricCannotBeBuilt() => MetricBuilder .Create() .SpecifyMetric(string.Empty, 0.0) .Invoking(bm => bm.Build()) .Should() .Throw <ArgumentException>() .WithMessage("*MetricName*");
public void WhenNoUnitIsSpecifiedNoneIsUsed() => MetricBuilder .Create() .SpecifyMetric("Latency", 42.0) .Build() .Unit .Should() .Be("None");
public void ThenTheMetricUnitIsSpecified() => MetricBuilder .Create() .SpecifyMetric("Latency", 42.0, Unit.Gigabits) .Build() .Unit .Should() .Be("Gigabits");
public void ThenTheMetricValueIsSpecified() => MetricBuilder .Create() .SpecifyMetric("Latency", 42.0) .Build() .Value .Should() .Be(42.0);
public void ThenTheMetricNameIsSpecified() => MetricBuilder .Create() .SpecifyMetric("Latency", 0.0) .Build() .Name .Should() .Be("Latency");
public void WhenAUnitIsSpecifiedThenItIsAValidCloudWatchUnitValue(Unit unit, string expectedUnit) => MetricBuilder .Create() .SpecifyMetric("Arthur", 42, unit) .Build() .Unit .Should() .Be(expectedUnit);
public void ThenNamespaceIsSpecified() => MetricBuilder .Create() .SpecifyMetric("Latency", 42, Unit.Seconds) .WithNamespace("Heart-Of-Gold") .Build() .Namespace .Should() .Be("Heart-Of-Gold");
public void ThenDimensionsAreSpecified() => MetricBuilder .Create() .SpecifyMetric("Foo", 42) .AddDimension("TowelId", "abc") .Build() .Dimensions .Should() .Contain(new KeyValuePair <string, string>("TowelId", "abc"));
public void ThenPropertiesAreSpecified() => MetricBuilder .Create() .SpecifyMetric("Foo", 42) .AddProperty("Zaphod", "Beeblebrox") .Build() .Properties .Should() .Contain(new KeyValuePair <string, string>("Zaphod", "Beeblebrox"));
public void ToJson_NonEmptyMetricBatch() { // Arrange var metricBatch = MetricBatchBuilder.Create() .WithIntervalMs(interval) .WithTimestamp(timestamp) .WithMetric(MetricBuilder.CreateCountMetric("metric1") .WithValue(countValue) .WithAttributes(CustomAttributes) .Build()) .WithMetric(MetricBuilder.CreateSummaryMetric("metric2") .WithValue(summaryValue) .Build()) .Build(); // Act var jsonString = metricBatch.ToJson(); // Assert var resultMetricBatches = TestHelpers.DeserializeArray(jsonString); TestHelpers.AssertForCollectionLength(resultMetricBatches, 1); // CountMetric var resultMetricBatch = resultMetricBatches.First(); var resultCommonProps = TestHelpers.DeserializeObject(resultMetricBatch["common"]); TestHelpers.AssertForAttribValue(resultCommonProps, "timestamp", timestampL); TestHelpers.AssertForAttribValue(resultCommonProps, "interval.ms", interval); var resultMetrics = TestHelpers.DeserializeArray(resultMetricBatch["metrics"]); TestHelpers.AssertForCollectionLength(resultMetrics, 2); var countMetric = resultMetrics.FirstOrDefault(); TestHelpers.AssertForAttribCount(countMetric, 4); TestHelpers.AssertForAttribValue(countMetric, "name", "metric1"); TestHelpers.AssertForAttribValue(countMetric, "type", "count"); TestHelpers.AssertForAttribValue(countMetric, "value", countValue); var countMetricAttribs = TestHelpers.DeserializeObject(countMetric["attributes"]); TestHelpers.AssertForAttribCount(countMetricAttribs, 1); TestHelpers.AssertForAttribValue(countMetricAttribs, "attr1Key", "attr1Value"); // SummaryMetric var summaryMetric = resultMetrics[1]; TestHelpers.AssertForAttribCount(summaryMetric, 3); TestHelpers.AssertForAttribValue(summaryMetric, "name", "metric2"); TestHelpers.AssertForAttribValue(summaryMetric, "type", "summary"); TestHelpers.AssertForAttribValue(summaryMetric, "value", summaryValue); }
public async Task SubmitPointAsync(MetricBuilder builder) { // TODO: [TESTS] (MetricService.SubmitPointAsync) Add tests if (!Enabled) { return; } await SubmitPointAsync(builder.Build(_dateTime.UtcNow)); }
public static MetricBuilder GetBuilder(this Reporter reporter, string alias) { var builder = new MetricBuilder(); builder.WithTimestamp() .WithPath(GeneratePath(alias)) .WithAlias(alias); return(builder); }
// Interface methods public void SubmitPoint(MetricBuilder builder) { // TODO: [TESTS] (MetricService.SubmitPoint) Add tests if (!Enabled) { return; } SubmitPointAsync(builder) .ConfigureAwait(false) .GetAwaiter() .GetResult(); }
public MetricTimingToken(MetricBuilder builder, string fieldName) { _builder = builder; FieldName = fieldName; // If there was no field name provided, fall back to "value" if (string.IsNullOrWhiteSpace(FieldName)) { FieldName = CoreMetricField.Value; } _stopwatch = Stopwatch.StartNew(); }
private void GetApdexMetrics(ImmutableTransaction immutableTransaction, TimeSpan apdexT, string transactionApdexMetricName, TransactionMetricStatsCollection txStats) { var isWebTransaction = immutableTransaction.IsWebTransaction(); if (immutableTransaction.TransactionMetadata.ReadOnlyTransactionErrorState.HasError && !immutableTransaction.TransactionMetadata.ReadOnlyTransactionErrorState.ErrorData.IsExpected) { MetricBuilder.TryBuildFrustratedApdexMetrics(isWebTransaction, transactionApdexMetricName, txStats); } else { MetricBuilder.TryBuildApdexMetrics(transactionApdexMetricName, isWebTransaction, immutableTransaction.ResponseTimeOrDuration, apdexT, txStats); } }
public void ThenTheSerialisedMetricIsFormattedCorrectly() { var serialiser = new Serialiser(new TestClock(new DateTimeOffset(2015, 09, 28, 09, 00, 00, TimeSpan.Zero)), new MetricLoggerOptions()); var metric = MetricBuilder .Create() .SpecifyMetric("time", 100, Unit.Milliseconds) .AddDimension("functionVersion", "$LATEST") .AddDimension("functionName", "LambdaFunction") .AddProperty("requestId", "12345678-9ace-4817-a57c-e4dd734019ee") .AddProperty("requestPath", "/foo/bar") .WithNamespace("lambda-function-metrics") .Build(); var json = serialiser.SerialiseMetric(metric); JObject .Parse(json) .Should() .BeEquivalentTo(@" { ""_aws"": { ""Timestamp"": 1443430800, ""CloudWatchMetrics"": [ { ""Namespace"": ""lambda-function-metrics"", ""Dimensions"": [ [ ""functionVersion"", ""functionName"" ] ], ""Metrics"": [ { ""Name"": ""time"", ""Unit"": ""Milliseconds"" } ] } ] }, ""functionVersion"": ""$LATEST"", ""functionName"": ""LambdaFunction"", ""time"": 100, ""requestId"": ""12345678-9ace-4817-a57c-e4dd734019ee"", ""requestPath"": ""/foo/bar"" } "); }
public void SendANonEmptyMetricBatch() { var metricBatch = MetricBatchBuilder.Create() .WithMetric(MetricBuilder.CreateGaugeMetric("TestMetric").Build()) .Build(); var dataSender = new MetricDataSender(new TelemetryConfiguration().WithApiKey("123456")); dataSender.WithHttpHandlerImpl((serializedJson) => { var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); return(Task.FromResult(response)); }); var response = dataSender.SendDataAsync(metricBatch).Result; Assert.AreEqual(NewRelicResponseStatus.Success, response.ResponseStatus); }
public static void Enqueue(this Reporter reporter, string alias, Dictionary <string, object> data = null) { var builder = new MetricBuilder(); builder.WithTimestamp() .WithAlias(alias) .WithPath(GeneratePath(alias)); if (data != null && data.Any()) { foreach (var pair in data) { builder.WithData(pair.Key, pair.Value); } } reporter.Enqueue(builder.Build()); }
public void WhenNoOptionsAreSpecifiedThenTheDefaultOptionsAreUsed() { var sc = new ServiceCollection().AddMetricLogger().BuildServiceProvider(); sc.GetService <IMetricLogger>() .Should() .NotBeNull() .And .BeOfType <MetricLogger>() .Which .Log(MetricBuilder.Create().SpecifyMetric("foo", 42.0, Unit.CountPerSecond).Build()); JObject .Parse(_logOutput.ToString()) .SelectToken("_aws.CloudWatchMetrics[0].Namespace") .ToObject <string>() .Should() .Be("aws-embedded-metrics"); }
public static void Enqueue(this Reporter reporter, string providerName, string endpoint, long value, string responseCode) { var builder = new MetricBuilder(); var itemName = endpoint; if (!string.IsNullOrEmpty(providerName)) { itemName = $"{providerName}.{endpoint}"; } var metric = builder.WithTimestamp() .WithValue(value) .WithPath(GeneratePath(itemName)) .WithAlias(itemName) .WithMessage(responseCode).Build(); reporter.Enqueue(metric); }