|
| 1 | +# TraceRoot Log4j2 Local Example |
| 2 | + |
| 3 | +This example demonstrates how to use the TraceRoot SDK with Log4j2 as the logging framework in a standalone Java application with local file logging. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Java 11 or higher |
| 8 | +- Maven 3.6+ |
| 9 | +- TraceRoot OTLP endpoint (for trace export) |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +### 1. Build and Install the Java Environment |
| 14 | + |
| 15 | +From the repository root: |
| 16 | + |
| 17 | +```bash |
| 18 | +mvn clean install -Dgpg.skip=true |
| 19 | +``` |
| 20 | + |
| 21 | +### 2. Configure Environment Variables |
| 22 | + |
| 23 | +Copy the example environment file and populate it with your values: |
| 24 | + |
| 25 | +```bash |
| 26 | +cp .env.example .env |
| 27 | +``` |
| 28 | + |
| 29 | +Edit `.env` and set: |
| 30 | + |
| 31 | +- `TRACEROOT_OTLP_ENDPOINT` - Your OTLP endpoint |
| 32 | +- `TRACEROOT_ROOT_PATH` - Absolute path to this project directory |
| 33 | + |
| 34 | +### 3. Run the Application |
| 35 | + |
| 36 | +```bash |
| 37 | +mvn clean compile exec:java |
| 38 | +``` |
| 39 | + |
| 40 | +## Setting Up Jaeger on Tencent CVM |
| 41 | + |
| 42 | +This section guides you through setting up a Jaeger instance on Tencent CVM to collect and visualize traces from your application. |
| 43 | + |
| 44 | +### Step 1: Install Docker |
| 45 | + |
| 46 | +SSH into your Tencent CVM and install Docker: |
| 47 | + |
| 48 | +```bash |
| 49 | +# Install Docker |
| 50 | +sudo apt-get update |
| 51 | +sudo apt-get install -y docker.io |
| 52 | + |
| 53 | +# Start Docker and enable on boot |
| 54 | +sudo systemctl start docker |
| 55 | +sudo systemctl enable docker |
| 56 | + |
| 57 | +# Add current user to docker group (optional, avoids needing sudo) |
| 58 | +sudo usermod -aG docker $USER |
| 59 | +# Note: Log out and log back in for group changes to take effect |
| 60 | +``` |
| 61 | + |
| 62 | +### Step 2: Start Jaeger Container |
| 63 | + |
| 64 | +Run Jaeger All-in-One with OTLP support enabled: |
| 65 | + |
| 66 | +```bash |
| 67 | +docker run -d --name jaeger \ |
| 68 | + --restart unless-stopped \ |
| 69 | + -e COLLECTOR_OTLP_ENABLED=true \ |
| 70 | + -p 16686:16686 \ |
| 71 | + -p 14268:14268 \ |
| 72 | + -p 14250:14250 \ |
| 73 | + -p 4317:4317 \ |
| 74 | + -p 4318:4318 \ |
| 75 | + cr.jaegertracing.io/jaegertracing/jaeger:2.8.0 |
| 76 | +``` |
| 77 | + |
| 78 | +**Port Mappings:** |
| 79 | + |
| 80 | +- `16686` - Jaeger UI |
| 81 | +- `4318` - OTLP HTTP endpoint (used by TraceRoot SDK) |
| 82 | +- `4317` - OTLP gRPC endpoint |
| 83 | +- `14268` - Jaeger Collector HTTP |
| 84 | +- `14250` - Jaeger Collector gRPC |
| 85 | + |
| 86 | +### Step 3: Verify Jaeger is Running |
| 87 | + |
| 88 | +Check that the container is running properly: |
| 89 | + |
| 90 | +```bash |
| 91 | +# Check container status |
| 92 | +sudo docker ps | grep jaeger |
| 93 | + |
| 94 | +# Test Jaeger API endpoint |
| 95 | +curl http://localhost:16686/api/services |
| 96 | +``` |
| 97 | + |
| 98 | +**Expected output:** `{"data":null}` or `{"data":[]}` (empty because no traces have been sent yet) |
| 99 | + |
| 100 | +## Setting Up Tencent CLS LogListener |
| 101 | + |
| 102 | +This section guides you through setting up Tencent CLS LogListener agent to automatically collect local log files and send them to CLS (Cloud Log Service). |
| 103 | + |
| 104 | +### Step 1: Install LogListener Agent |
| 105 | + |
| 106 | +SSH into your Tencent CVM and install the LogListener agent: |
| 107 | + |
| 108 | +```bash |
| 109 | +# Download LogListener installer |
| 110 | +cd /tmp |
| 111 | +wget http://mirrors.tencent.com/install/cls/loglistener-linux-x64.tar.gz |
| 112 | + |
| 113 | +# Extract to /usr/local |
| 114 | +sudo tar -zxvf loglistener-linux-x64.tar.gz -C /usr/local/ |
| 115 | + |
| 116 | +# Navigate to tools directory |
| 117 | +cd /usr/local/loglistener/tools |
| 118 | + |
| 119 | +# Install the agent |
| 120 | +sudo ./loglistener.sh install |
| 121 | +``` |
| 122 | + |
| 123 | +### Step 2: Initialize LogListener with Your Credentials |
| 124 | + |
| 125 | +Initialize the agent with your Tencent Cloud credentials: |
| 126 | + |
| 127 | +```bash |
| 128 | +# Initialize with your credentials |
| 129 | +sudo ./loglistener.sh init \ |
| 130 | + --secretid YOUR_SECRET_ID \ |
| 131 | + --secretkey YOUR_SECRET_KEY \ |
| 132 | + --region YOUR_LOCATION \ |
| 133 | + --network internet |
| 134 | +``` |
| 135 | + |
| 136 | +### Step 3: Start LogListener Service |
| 137 | + |
| 138 | +Start and verify the LogListener daemon: |
| 139 | + |
| 140 | +```bash |
| 141 | +# Start the service |
| 142 | +sudo systemctl start loglistenerd |
| 143 | + |
| 144 | +# Check service status |
| 145 | +sudo systemctl status loglistenerd |
| 146 | + |
| 147 | +# Enable on boot (optional) |
| 148 | +sudo systemctl enable loglistenerd |
| 149 | +``` |
| 150 | + |
| 151 | +### Step 4: Configure Collection in CLS Console |
| 152 | + |
| 153 | +Configure the log collection in Tencent CLS Console: |
| 154 | + |
| 155 | +1. **Navigate to CLS Console** → Select your **Log Topic** → **Collection Configuration** |
| 156 | + |
| 157 | +1. **Create Machine Group**: |
| 158 | + |
| 159 | + - Go to **Machine Group** section |
| 160 | + - Click **Create Machine Group** |
| 161 | + - Add your CVM's **private IP address** |
| 162 | + - Save the machine group |
| 163 | + |
| 164 | +1. **Create Collection Configuration**: |
| 165 | + |
| 166 | + - Click **Create Collection Config** |
| 167 | + - **Collection Path**: |
| 168 | + ``` |
| 169 | + /path/to/traceroot-sdk-java/examples/log4j2-local-example/logs/traceroot-sdk.log |
| 170 | + ``` |
| 171 | + - **Key-Value Extraction Mode**: Select `JSON` |
| 172 | + - **Associate Machine Group**: Select the machine group you created |
| 173 | + - Click **Save** |
0 commit comments