Repository: microsoft/ApplicationInsights-node.js (ts)

Issue:
Wrong target reported when using AutoDependencyTracking and HTTP_PROXY environment variable
Follow up after #689 and #692.

The problem still persists, that when using request library together with HTTP_PROXY setting, all dependency requests that go through proxy have target of 443 (this are https requests).

Options object in [HttpDependencyParser._getUrlFromRequestOptions](https://github.com/microsoft/ApplicationInsights-node.js/blob/develop/AutoCollection/HttpDependencyParser.ts#L115) is of following structure:
 
```js
{
  host: "proxy-host",
   port: 3128,
   proxyAuth: null,
   headers: {
     Accept: "application/json",
     host: "destination-host:443",
   },
   method: "CONNECT",
   path: "destination-host:443",
   agent: false,
 }
 ```
 
And after `url.parse` in line 141, the protocol is "destination-host" and host is 443.
 
Anyway - the result of `_getUrlFromRequestOptions` function will be in this case still PROXY host, not the real one.
 
On the other hand, when I debug through `public static trackRequest(client: TelemetryClient, telemetry: Contracts.NodeHttpDependencyTelemetry) {`
 
I see requestParser.url set proxy host, but when callback is called `telemetry.request.on('response', (response: http.ClientResponse) => {` then `requestParser.url` is already set to target host and port, but without protocol. Can't find place, where it's actually changed.


Pull request description:
Use request.protocol when available in automatic dependency collection
Fixes #719 

Patch:
diff --git a/AutoCollection/HttpDependencyParser.ts b/AutoCollection/HttpDependencyParser.ts
index 49f4fac..6b814b7 100644
--- a/AutoCollection/HttpDependencyParser.ts
+++ b/AutoCollection/HttpDependencyParser.ts
@@ -163,7 +163,7 @@ class HttpDependencyParser extends RequestParser {
         }
 
         // Mix in default values used by http.request and others
-        options.protocol = options.protocol || ((<any>request).agent && (<any>request).agent.protocol) || undefined;
+        options.protocol = options.protocol || ((<any>request).agent && (<any>request).agent.protocol) || ((<any>request).protocol) || undefined;
         options.hostname = options.hostname || 'localhost';
 
         return url.format(options);


Test patch:
diff --git a/Tests/AutoCollection/HttpDependencyParser.tests.ts b/Tests/AutoCollection/HttpDependencyParser.tests.ts
index 3f24b04..05b9c0b 100644
--- a/Tests/AutoCollection/HttpDependencyParser.tests.ts
+++ b/Tests/AutoCollection/HttpDependencyParser.tests.ts
@@ -44,7 +44,7 @@ describe("AutoCollection/HttpDependencyParser", () => {
             assert.equal(dependencyTelemetry.data, "http://bing.com:123/search");
             assert.equal(dependencyTelemetry.target, "bing.com:123 | abcdefg");
         });
-        
+
         it("should return correct data for a URL without a protocol (https)", () => {
             (<any>request)["method"] = "GET";
             let parser = new HttpDependencyParser("a.bing.com:443/search", request);
@@ -59,7 +59,7 @@ describe("AutoCollection/HttpDependencyParser", () => {
             assert.equal(dependencyTelemetry.data, "https://a.bing.com:443/search");
             assert.equal(dependencyTelemetry.target, "a.bing.com:443");
         });
-        
+
         it("should return correct data for a URL without a protocol (http)", () => {
             (<any>request)["method"] = "GET";
             let parser = new HttpDependencyParser("a.bing.com:123/search", request);
@@ -99,8 +99,8 @@ describe("AutoCollection/HttpDependencyParser", () => {
             response.statusCode = 200;
             parser.onResponse(response);
 
-            const dependencyTelemetry1 = parser.getDependencyTelemetry({time: new Date(111111)});
-            const dependencyTelemetry2 = parser.getDependencyTelemetry({time: new Date(222222)});
+            const dependencyTelemetry1 = parser.getDependencyTelemetry({ time: new Date(111111) });
+            const dependencyTelemetry2 = parser.getDependencyTelemetry({ time: new Date(222222) });
             assert.deepEqual(dependencyTelemetry1.time, new Date(111111));
             assert.deepEqual(dependencyTelemetry2.time, new Date(222222));
             assert.notDeepEqual(dependencyTelemetry1, dependencyTelemetry2);
@@ -141,6 +141,29 @@ describe("AutoCollection/HttpDependencyParser", () => {
             assert.equal(dependencyTelemetry.target, "bing.com:8000");
         });
 
+        it("should return correct data for URL with protocol in request", () => {
+            let testRequest: http.ClientRequest = <any>{
+                agent: { protocol: undefined },
+                method: "GET",
+                protocol: "https:"
+            };
+            let requestOptions = {
+                host: "bing.com",
+                port: 8000,
+                path: "/search?q=test",
+            };
+
+            let parser = new HttpDependencyParser(requestOptions, testRequest);
+            response.statusCode = 200;
+            parser.onResponse(response);
+            let dependencyTelemetry = parser.getDependencyTelemetry();
+            assert.equal(dependencyTelemetry.dependencyTypeName, Contracts.RemoteDependencyDataConstants.TYPE_HTTP);
+            assert.equal(dependencyTelemetry.success, true);
+            assert.equal(dependencyTelemetry.name, "GET /search");
+            assert.equal(dependencyTelemetry.data, "https://bing.com:8000/search?q=test");
+            assert.equal(dependencyTelemetry.target, "bing.com:8000");
+        });
+
         it("should return correct data for a request options object", () => {
             var path = "/finance/info?client=ig&q=";