Skip to content

Commit

Permalink
[HttpClient] Add net.peer.name and net.peer.port on metric dimens…
Browse files Browse the repository at this point in the history
…ions (#3907)

* Add net.peer.name and net.peer.port on metric

* changelog

* rmv todo
  • Loading branch information
vishweshbankwar authored Nov 15, 2022
1 parent a758c60 commit 2e7b7a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## Unreleased

* **Breaking change** `http.host` will no longer be populated. `net.peer.name`
and `net.peer.port` attributes will be populated instead.
* Added `net.peer.name` and `net.peer.port` as dimensions on
`http.client.duration` metric.
([#3907](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3907))

* **Breaking change** `http.host` will no longer be populated on activity.
`net.peer.name` and `net.peer.port` attributes will be populated instead.
([#3832](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3832))

## 1.0.0-rc9.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,30 @@ public override void OnEventWritten(string name, object payload)
{
var request = response.RequestMessage;

// TODO: This is just a minimal set of attributes. See the spec for additional attributes:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#http-client
var tags = new KeyValuePair<string, object>[]
TagList tags;
if (!request.RequestUri.IsDefaultPort)
{
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)),
};
tags = new TagList
{
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)),
new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host),
new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port),
};
}
else
{
tags = new TagList
{
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode),
new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)),
new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host),
};
}

this.httpClientDuration.Record(activity.Duration.TotalMilliseconds, tags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,15 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut
var scheme = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, "http");
var statusCode = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, tc.ResponseCode == 0 ? 200 : tc.ResponseCode);
var flavor = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, "2.0");
var hostName = new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerName, host);
var portNumber = new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerPort, port);
Assert.Contains(method, attributes);
Assert.Contains(scheme, attributes);
Assert.Contains(statusCode, attributes);
Assert.Contains(flavor, attributes);
Assert.Equal(4, attributes.Length);
Assert.Contains(hostName, attributes);
Assert.Contains(portNumber, attributes);
Assert.Equal(6, attributes.Length);
#endif
}
else
Expand Down

0 comments on commit 2e7b7a9

Please sign in to comment.