@@ -548,6 +548,98 @@ post_data(customer_id, shared_key, body, log_type)
548
548
```
549
549
550
550
551
+ ### Java sample
552
+
553
+ ``` java
554
+
555
+ import org.apache.http.HttpResponse ;
556
+ import org.apache.http.client.methods.HttpPost ;
557
+ import org.apache.http.entity.StringEntity ;
558
+ import org.apache.http.impl.client.CloseableHttpClient ;
559
+ import org.apache.http.impl.client.HttpClients ;
560
+ import org.springframework.http.MediaType ;
561
+
562
+ import javax.crypto.Mac ;
563
+ import javax.crypto.spec.SecretKeySpec ;
564
+ import java.io.IOException ;
565
+ import java.nio.charset.StandardCharsets ;
566
+ import java.security.InvalidKeyException ;
567
+ import java.security.NoSuchAlgorithmException ;
568
+ import java.text.SimpleDateFormat ;
569
+ import java.util.Base64 ;
570
+ import java.util.Calendar ;
571
+ import java.util.TimeZone ;
572
+
573
+ import static org.springframework.http.HttpHeaders.CONTENT_TYPE ;
574
+
575
+ public class ApiExample {
576
+
577
+ private static final String workspaceId = " xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
578
+ private static final String sharedKey = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
579
+ private static final String logName = " DemoExample" ;
580
+ /*
581
+ You can use an optional field to specify the timestamp from the data. If the time field is not specified,
582
+ Azure Monitor assumes the time is the message ingestion time
583
+ */
584
+ private static final String timestamp = " " ;
585
+ private static final String json = " {\" name\" : \" test\" ,\n " + " \" id\" : 1\n " + " }" ;
586
+ private static final String RFC_1123_DATE = " EEE, dd MMM yyyy HH:mm:ss z" ;
587
+
588
+ public static void main (String [] args ) throws IOException , NoSuchAlgorithmException , InvalidKeyException {
589
+ String dateString = getServerTime();
590
+ String httpMethod = " POST" ;
591
+ String contentType = " application/json" ;
592
+ String xmsDate = " x-ms-date:" + dateString;
593
+ String resource = " /api/logs" ;
594
+ String stringToHash = String
595
+ .join(" \n " , httpMethod, String . valueOf(json. getBytes(StandardCharsets . UTF_8 ). length), contentType,
596
+ xmsDate , resource);
597
+ String hashedString = getHMAC254(stringToHash, sharedKey);
598
+ String signature = " SharedKey " + workspaceId + " :" + hashedString;
599
+
600
+ postData(signature, dateString, json);
601
+ }
602
+
603
+ private static String getServerTime () {
604
+ Calendar calendar = Calendar . getInstance();
605
+ SimpleDateFormat dateFormat = new SimpleDateFormat (RFC_1123_DATE );
606
+ dateFormat. setTimeZone(TimeZone . getTimeZone(" GMT" ));
607
+ return dateFormat. format(calendar. getTime());
608
+ }
609
+
610
+ private static void postData (String signature , String dateString , String json ) throws IOException {
611
+ String url = " https://" + workspaceId + " .ods.opinsights.azure.com/api/logs?api-version=2016-04-01" ;
612
+ HttpPost httpPost = new HttpPost (url);
613
+ httpPost. setHeader(" Authorization" , signature);
614
+ httpPost. setHeader(CONTENT_TYPE , MediaType . APPLICATION_JSON_VALUE );
615
+ httpPost. setHeader(" Log-Type" , logName);
616
+ httpPost. setHeader(" x-ms-date" , dateString);
617
+ httpPost. setHeader(" time-generated-field" , timestamp);
618
+ httpPost. setEntity(new StringEntity (json));
619
+ try (CloseableHttpClient httpClient = HttpClients . createDefault()){
620
+ HttpResponse response = httpClient. execute(httpPost);
621
+ int statusCode = response. getStatusLine(). getStatusCode();
622
+ System . out. println(" Status code: " + statusCode);
623
+ }
624
+ }
625
+
626
+ private static String getHMAC254 (String input , String key ) throws InvalidKeyException , NoSuchAlgorithmException {
627
+ String hash;
628
+ Mac sha254HMAC = Mac . getInstance(" HmacSHA256" );
629
+ Base64 . Decoder decoder = Base64 . getDecoder();
630
+ SecretKeySpec secretKey = new SecretKeySpec (decoder. decode(key. getBytes(StandardCharsets . UTF_8 )), " HmacSHA256" );
631
+ sha254HMAC. init(secretKey);
632
+ Base64 . Encoder encoder = Base64 . getEncoder();
633
+ hash = new String (encoder. encode(sha254HMAC. doFinal(input. getBytes(StandardCharsets . UTF_8 ))));
634
+ return hash;
635
+ }
636
+
637
+ }
638
+
639
+
640
+ ```
641
+
642
+
551
643
## Alternatives and considerations
552
644
While the Data Collector API should cover most of your needs to collect free-form data into Azure Logs, there are instances where an alternative might be required to overcome some of the limitations of the API. All your options are as follows, major considerations included:
553
645
0 commit comments