Labsco
microsoft logo

azure-monitor-ingestion-java

✓ Official2,700

by microsoft · part of microsoft/skills

Client library for sending custom logs to Azure Monitor using the Logs Ingestion API via Data Collection Rules.

🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 7 skills in the microsoft/skills package — works on its own, and pairs well with its siblings.

Client library for sending custom logs to Azure Monitor using the Logs Ingestion API via Data Collection Rules.

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by microsoft

Client library for sending custom logs to Azure Monitor using the Logs Ingestion API via Data Collection Rules. npx skills add https://github.com/microsoft/agent-skills --skill azure-monitor-ingestion-java Download ZIPGitHub2.7k

Azure Monitor Ingestion SDK for Java

Client library for sending custom logs to Azure Monitor using the Logs Ingestion API via Data Collection Rules.

Environment Variables

Copy & paste — that's it
DATA_COLLECTION_ENDPOINT=https:// . .ingest.monitor.azure.com # Required for all auth methods
DATA_COLLECTION_RULE_ID=dcr-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Required for log upload routing
STREAM_NAME=Custom-MyTable_CL # Required for the target DCR stream
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production

Client Creation

Synchronous Client

Copy & paste — that's it
import com.azure.core.credential.TokenCredential;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.azure.monitor.ingestion.LogsIngestionClient;
import com.azure.monitor.ingestion.LogsIngestionClientBuilder;

// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS= 
TokenCredential credential = new DefaultAzureCredentialBuilder()
 .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
 .build();
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes
// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();

LogsIngestionClient client = new LogsIngestionClientBuilder()
 .endpoint(" ")
 .credential(credential)
 .buildClient();

Asynchronous Client

Copy & paste — that's it
import com.azure.monitor.ingestion.LogsIngestionAsyncClient;

LogsIngestionAsyncClient asyncClient = new LogsIngestionClientBuilder()
 .endpoint(" ")
 .credential(credential)
 .buildAsyncClient();

Key Concepts

Concept Description Data Collection Endpoint (DCE) Ingestion endpoint URL for your region Data Collection Rule (DCR) Defines data transformation and routing to tables Stream Name Target stream in the DCR (e.g., Custom-MyTable_CL) Log Analytics Workspace Destination for ingested logs

Core Operations

Upload Custom Logs

Copy & paste — that's it
import java.util.List;
import java.util.ArrayList;

List logs = new ArrayList<>();
logs.add(new MyLogEntry("2024-01-15T10:30:00Z", "INFO", "Application started"));
logs.add(new MyLogEntry("2024-01-15T10:30:05Z", "DEBUG", "Processing request"));

client.upload(" ", " ", logs);
System.out.println("Logs uploaded successfully");

Upload with Concurrency

For large log collections, enable concurrent uploads:

Copy & paste — that's it
import com.azure.monitor.ingestion.models.LogsUploadOptions;
import com.azure.core.util.Context;

List logs = getLargeLogs(); // Large collection

LogsUploadOptions options = new LogsUploadOptions()
 .setMaxConcurrency(3);

client.upload(" ", " ", logs, options, Context.NONE);

Upload with Error Handling

Handle partial upload failures gracefully:

Copy & paste — that's it
LogsUploadOptions options = new LogsUploadOptions()
 .setLogsUploadErrorConsumer(uploadError -> {
 System.err.println("Upload error: " + uploadError.getResponseException().getMessage());
 System.err.println("Failed logs count: " + uploadError.getFailedLogs().size());
 
 // Option 1: Log and continue
 // Option 2: Throw to abort remaining uploads
 // throw uploadError.getResponseException();
 });

client.upload(" ", " ", logs, options, Context.NONE);

Async Upload with Reactor

Copy & paste — that's it
import reactor.core.publisher.Mono;

List logs = getLogs();

asyncClient.upload(" ", " ", logs)
 .doOnSuccess(v -> System.out.println("Upload completed"))
 .doOnError(e -> System.err.println("Upload failed: " + e.getMessage()))
 .subscribe();

Log Entry Model Example

Copy & paste — that's it
public class MyLogEntry {
 private String timeGenerated;
 private String level;
 private String message;
 
 public MyLogEntry(String timeGenerated, String level, String message) {
 this.timeGenerated = timeGenerated;
 this.level = level;
 this.message = message;
 }
 
 // Getters required for JSON serialization
 public String getTimeGenerated() { return timeGenerated; }
 public String getLevel() { return level; }
 public String getMessage() { return message; }
}

Error Handling

Copy & paste — that's it
import com.azure.core.exception.HttpResponseException;

try {
 client.upload(ruleId, streamName, logs);
} catch (HttpResponseException e) {
 System.err.println("HTTP Status: " + e.getResponse().getStatusCode());
 System.err.println("Error: " + e.getMessage());
 
 if (e.getResponse().getStatusCode() == 403) {
 System.err.println("Check DCR permissions and managed identity");
 } else if (e.getResponse().getStatusCode() == 404) {
 System.err.println("Verify DCE endpoint and DCR ID");
 }
}

Best Practices

  • Batch logs — Upload in batches rather than one at a time

  • Use concurrency — Set maxConcurrency for large uploads

  • Handle partial failures — Use error consumer to log failed entries

  • Match DCR schema — Log entry fields must match DCR transformation expectations

  • Include TimeGenerated — Most tables require a timestamp field

  • Reuse client — Create once, reuse throughout application

  • Use async for high throughputLogsIngestionAsyncClient for reactive patterns

Querying Uploaded Logs

Use azure-monitor-query to query ingested logs:

Copy & paste — that's it
// See azure-monitor-query skill for LogsQueryClient usage
String query = "MyTable_CL | where TimeGenerated > ago(1h) | limit 10";

Reference Links

Resource URL Maven Package https://central.sonatype.com/artifact/com.azure/azure-monitor-ingestion GitHub https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-ingestion Product Docs https://learn.microsoft.com/azure/azure-monitor/logs/logs-ingestion-api-overview DCE Overview https://learn.microsoft.com/azure/azure-monitor/essentials/data-collection-endpoint-overview DCR Overview https://learn.microsoft.com/azure/azure-monitor/essentials/data-collection-rule-overview Troubleshooting https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-ingestion/TROUBLESHOOTING.md