Skip to content

Commit

Permalink
Merge pull request #176 from shrimma/metricpropertynamesupport
Browse files Browse the repository at this point in the history
Add support for metricNameProperty to metric metadata
  • Loading branch information
yantang-msft authored Jan 31, 2018
2 parents c5882e0 + 2f7171a commit 4d3e61c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -812,12 +812,14 @@ Metrics are named time series of floating-point values. Metric metadata defines
| Field | Values/Types | Required | Description |
| :---- | :-------------- | :------: | :---------- |
| `metadata` | "metric" | Yes | Indicates metric metadata definition; must be "metric". |
| `metricName` | string | Yes | The name of the metric. |
| `metricName` | string | (see Remarks) | The name of the metric. |
| `metricNameProperty` | string | (see Remarks) | The name of the event property that holds metric name. |
| `metricValue` | double | (see Remarks) | The value of the metric. This is useful for "counter" type of metric when each occurrence of a particular event should result in an increment of the counter. |
| `metricValueProperty` | string | (see Remarks) | The name of the event property that holds the metric value. |

Remarks:
1. Either `metricValue` or `metricValueProperty` must be specified.
1. Either `metricName` or `metricNameProperty` must be specified.
2. Either `metricValue` or `metricValueProperty` must be specified.

**Request metadata type**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MetricData
{
public static readonly string MetricMetadataKind = "metric";
public static readonly string MetricNameMoniker = "metricName";
public static readonly string MetricNamePropertyMoniker = "metricNameProperty";
public static readonly string MetricValueMoniker = "metricValue";
public static readonly string MetricValuePropertyMoniker = "metricValueProperty";

Expand All @@ -31,10 +32,23 @@ public static DataRetrievalResult TryGetData(EventData eventData, EventMetadata
return DataRetrievalResult.InvalidMetadataType(metricMetadata.MetadataType, MetricMetadataKind);
}

string metricName = metricMetadata[MetricNameMoniker];
if (string.IsNullOrEmpty(metricName))
string metricName = string.Empty;

string metricNameProperty = metricMetadata[MetricNamePropertyMoniker];
if (string.IsNullOrEmpty(metricNameProperty))
{
metricName = metricMetadata[MetricNameMoniker];
if (string.IsNullOrEmpty(metricName))
{
return DataRetrievalResult.MissingMetadataProperty(MetricNameMoniker);
}
}
else
{
return DataRetrievalResult.MissingMetadataProperty(MetricNameMoniker);
if (!eventData.GetValueFromPayload<string>(metricNameProperty, (v) => metricName = v))
{
return DataRetrievalResult.DataMissingOrInvalid(metricNameProperty);
}
}

double value = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Defines core interfaces and types that comprise Microsoft.Diagnostics.EventFlow library.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<VersionPrefix>1.1.10</VersionPrefix>
<VersionPrefix>1.1.11</VersionPrefix>
<Authors>Microsoft</Authors>
<TargetFrameworks>netstandard1.6;net451</TargetFrameworks>
<DelaySign>true</DelaySign>
Expand Down
18 changes: 17 additions & 1 deletion test/Microsoft.Diagnostics.EventFlow.Core.Tests/MetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public void MetricDataReadSuccessfully()
result = MetricData.TryGetData(eventData, metricMetadata, out md);
Assert.Equal(DataRetrievalStatus.Success, result.Status);
Assert.Equal(3.14, md.Value, DoublePrecisionTolerance);

//metric name value property
eventData.Payload.Add("metricName", "customMetricName");
metricMetadata.Properties.Remove(MetricData.MetricNameMoniker);
metricMetadata.Properties.Add(MetricData.MetricNamePropertyMoniker, "metricName");

result = MetricData.TryGetData(eventData, metricMetadata, out md);
Assert.Equal(DataRetrievalStatus.Success, result.Status);
Assert.Equal(3.14, md.Value, DoublePrecisionTolerance);
Assert.Equal("customMetricName", md.MetricName);
}

[Fact]
Expand All @@ -55,13 +65,19 @@ public void MetricDataExpectedReadFailures()
var result = MetricData.TryGetData(eventData, metricMetadata, out MetricData md);
Assert.Equal(DataRetrievalStatus.InvalidMetadataType, result.Status);

// Missing metric name property
// No metricName or metricNameProperty on the metadata
metricMetadata = new EventMetadata(MetricData.MetricMetadataKind);
result = MetricData.TryGetData(eventData, metricMetadata, out md);
Assert.Equal(DataRetrievalStatus.MetadataPropertyMissing, result.Status);
Assert.Contains("Expected property 'metricName'", result.Message);

// metricNameProperty points to a property that does not exist
metricMetadata.Properties.Add(MetricData.MetricNamePropertyMoniker, "customMetricName");
result = MetricData.TryGetData(eventData, metricMetadata, out md);
Assert.Equal(DataRetrievalStatus.DataMissingOrInvalid, result.Status);

// No metricValue or metricValueProperty on the metadata
metricMetadata.Properties.Remove(MetricData.MetricNamePropertyMoniker);
metricMetadata.Properties.Add(MetricData.MetricNameMoniker, "SomeMetric");
result = MetricData.TryGetData(eventData, metricMetadata, out md);
Assert.Equal(DataRetrievalStatus.MetadataPropertyMissing, result.Status);
Expand Down

0 comments on commit 4d3e61c

Please sign in to comment.