Using Fiddler for Application Insights codeless java agent with docker on WSL2
Tags: fiddler, wsl2, applicationinsights, java
Published: 2020-12-17 09:24:36

I'm working on [Application Insights - Java codeless agent](https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-in-process-agent) working on a docker container on WSL2. It works on the other guys machine, however, on my laptop, it sometimes work, most of the times, it doesn't work. 

My colleague gave me an advice to use [Fiddler](https://www.telerik.com/fiddler). I use it for watching network requests that is coming from a docker container on WSL2.

Let's see how to configure it. 

# Strategy Overview
We are configure the Java Application Insights agent for proxying to the Fiddler. We need to answer these questions.


* How to extract a root certificate from Fiddler and import it to JVM KeyStore on Docker. (I use [zulu](https://www.azul.com/))
* How to send request from WSL2 to Windows 10
* How to configure Application Insights agent to proxy to the Fiddler.
* Is there any other configuration required?

# Export a root certificate from Fiddler
Go to `Tools > Options > HTTPS > Actions` then select `Export Root Certificate to Desktop`. Then move the file to WSL2 side. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6z2r39bt0819qezb14k1.png)

From the wsl2 side I do this after the moving the file to Download.

```bash
$ mv /mnt/c/Users/tsushi/Download/FiddlerRoot.crt
```

# Import the certificate to the KeyStore on JVM
We can use `keytool` for import the certificate to the JVM KeyStore. In case of `zulu`,  the KeyStore is `/usr/lib/jvm/zre-8-azure-amd64/lib/security/cacerts` by default. The default password of the KeyStore is `changeit`. 

```dockerfile
COPY FiddlerRoot.cer /
RUN /usr/lib/jvm/zre-8-azure-amd64/bin/keytool -import -noprompt -trustcacerts -alias FiddlerRoot -file /FiddlerRoot.cer -keystore /usr/lib/jvm/zre-8-azure-amd64/lib/security/cacerts -storepass changeit
```

# Configure Application Insights Codeless Agent for Proxy

## Find IPAddress for accessing from WSL2 to Windows 10
To accessing from WSL2 to Windows 10, you can open `Network Connection` from your `Type here to search` box on your Windows 10. 
You can find a network adopter `vEthernet (WSL)`, Find the IP address. The IP Address is the address that is seen it as Windows10 from WSL2. In my case it is, `172.20.64.1`. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g299fm5s9nvt3vkvom2r.png)

You can find the Fiddler port number in here.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9dwcbl5b3wxcunm2t9ze.png)

## Configure the codeless agent

Configure the `proxy` section with the IP Address and port number. Then the Java codeless agent will send request to the proxy (Fiddler).

```json
{
  "proxy":{
    "host": "172.20.64.1",
    "port": 8888
  },
  "selfDiagnostics": {
    "destination": "file+console",
    "level": "INFO",
    "file": {
      "path": "/var/log/applicationinsights/applicationinsights.log",
      "maxSizeMb":  5,
      "maxHistory": 1
    }
  }
}
```

# Configure Fiddler to accept the request from WSL2
Configure the `Allow remote computers to connect` From the Windows 10 Fiddler, WSL2 is different computer. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7s0a41xs6tofeqtyihg2.png)

Start the Fiddler, and run the Docker container. You will find the two requests. These are the Application Insights telemetry. 

* rt.services.visualstudio.com
* {location}-{number}.in.applicationinsights.azure.com

# Resources

* [How to capture https request on Java: Japanese](https://code-examples.net/ja/q/827575)
* [Http Proxy - Configuration options - Azure Monitor Application Insights for Java](https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#http-proxy)