func (c *LogClient) PostMessages(messages []string, timestamp time.Time) error {
	if timestamp.IsZero() {
		timestamp = time.Now().UTC()
	}

	var logs []map[string]string
	for _, m := range messages {
		log := make(map[string]string, len(c.metadata)+1)
		for item := range c.metadata {
			log[item] = c.metadata[item]
		}
		log["message"] = m
		log["Timestamp"] = timestamp.Format(time.RFC3339)

		logs = append(logs, log)
	}

	body, _ := json.Marshal(logs)
	req, _ := http.NewRequest(http.MethodPost, c.apiLogsURL, bytes.NewReader(body))

	date := time.Now().In(locationGMT).Format(time.RFC1123)
	stringToSign := "POST\n" + strconv.FormatInt(req.ContentLength, 10) + "\napplication/json\n" + "x-ms-date:" + date + "\n/api/logs"

	signature := computeHmac256(stringToSign, c.signingKey)

	req.Header.Set("Authorization", fmt.Sprintf("SharedKey %s:%s", c.workspaceID, signature))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Log-Type", c.logType)
	req.Header.Set("x-ms-date", date)
	req.Header.Set("time-generated-field", "Timestamp")

	response, err := c.httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("Failed to post request: %v", err)
	}

	if response.StatusCode != 200 {
		defer response.Body.Close()
		buf, _ := ioutil.ReadAll(response.Body)

		time.AfterFunc(
			time.Second*15,
			func() {
				err := c.PostMessages(messages, timestamp)
				if err != nil {
					fmt.Printf("[LOG2OMS][%s] Retry failed, will keep retrying", time.Now().UTC().Format(time.RFC3339))
				} else {
					fmt.Printf("[LOG2OMS][%s] Posted %d messages.\n", time.Now().UTC().Format(time.RFC3339), len(logs))
				}
			})

		return fmt.Errorf("[LOG2OMS][%s] Post log request failed with status: %d %s", time.Now().UTC().Format(time.RFC3339), response.StatusCode, string(buf))
	}

	fmt.Printf("[LOG2OMS][%s] Posted %d messages.\n", time.Now().UTC().Format(time.RFC3339), len(logs))

	return nil
}
// PostMessages logs an array of messages to log analytics service
PostMessages logs an array of messages to log analytics service
// PostMessages logs an array of messages to log analytics service
func (c *LogClient) PostMessages(messages []string, timestamp time.Time) error {
	if timestamp.IsZero() {
		timestamp = time.Now().UTC()
	}
	var logs []map[string]string
	for _, m := range messages {
		log := make(map[string]string, len(c.metadata)+1)
		for item := range c.metadata {
			log[item] = c.metadata[item]
		}
		log["message"] = m
		log["Timestamp"] = timestamp.Format(time.RFC3339)
		logs = append(logs, log)
	}
	body, _ := json.Marshal(logs)
	req, _ := http.NewRequest(http.MethodPost, c.apiLogsURL, bytes.NewReader(body))
	date := time.Now().In(locationGMT).Format(time.RFC1123)
	stringToSign := "POST\n" + strconv.FormatInt(req.ContentLength, 10) + "\napplication/json\n" + "x-ms-date:" + date + "\n/api/logs"
	signature := computeHmac256(stringToSign, c.signingKey)
	req.Header.Set("Authorization", fmt.Sprintf("SharedKey %s:%s", c.workspaceID, signature))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Log-Type", c.logType)
	req.Header.Set("x-ms-date", date)
	req.Header.Set("time-generated-field", "Timestamp")
	response, err := c.httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("Failed to post request: %v", err)
	}
	if response.StatusCode != 200 {
		defer response.Body.Close()
		buf, _ := ioutil.ReadAll(response.Body)
		time.AfterFunc(
			time.Second*15,
			func() {
				err := c.PostMessages(messages, timestamp)
				if err != nil {
					fmt.Printf("[LOG2OMS][%s] Retry failed, will keep retrying", time.Now().UTC().Format(time.RFC3339))
				} else {
					fmt.Printf("[LOG2OMS][%s] Posted %d messages.\n", time.Now().UTC().Format(time.RFC3339), len(logs))
				}
			})
		return fmt.Errorf("[LOG2OMS][%s] Post log request failed with status: %d %s", time.Now().UTC().Format(time.RFC3339), response.StatusCode, string(buf))
	}
	fmt.Printf("[LOG2OMS][%s] Posted %d messages.\n", time.Now().UTC().Format(time.RFC3339), len(logs))
	return nil
}
func ( c * LogClient ) PostMessages ( messages [ ] string , timestamp time . Time ) error { if timestamp . IsZero ( ) { timestamp = time . Now ( ) . UTC ( ) } var logs [ ] map [ string ] string for _ , m := range messages { log := make ( map [ string ] string , len ( c . metadata ) + 1 ) for item := range c . metadata { log [ item ] = c . metadata [ item ] } log [ " " ] = m log [ " " ] = timestamp . Format ( time . RFC3339 ) logs = append ( logs , log ) } body , _ := json . Marshal ( logs ) req , _ := http . NewRequest ( http . MethodPost , c . apiLogsURL , bytes . NewReader ( body ) ) date := time . Now ( ) . In ( locationGMT ) . Format ( time . RFC1123 ) stringToSign := " \n " + strconv . FormatInt ( req . ContentLength , 10 ) + " \n \n " + " " + date + " \n " signature := computeHmac256 ( stringToSign , c . signingKey ) req . Header . Set ( " " , fmt . Sprintf ( " " , c . workspaceID , signature ) ) req . Header . Set ( " " , " " ) req . Header . Set ( " " , c . logType ) req . Header . Set ( " " , date ) req . Header . Set ( " " , " " ) response , err := c . httpClient . Do ( req ) if err != nil { return fmt . Errorf ( " " , err ) } if response . StatusCode != 200 { defer response . Body . Close ( ) buf , _ := ioutil . ReadAll ( response . Body ) time . AfterFunc ( time . Second * 15 , func ( ) { err := c . PostMessages ( messages , timestamp ) if err != nil { fmt . Printf ( " " , time . Now ( ) . UTC ( ) . Format ( time . RFC3339 ) ) } else { fmt . Printf ( " \n " , time . Now ( ) . UTC ( ) . Format ( time . RFC3339 ) , len ( logs ) ) } } ) return fmt . Errorf ( " " , time . Now ( ) . UTC ( ) . Format ( time . RFC3339 ) , response . StatusCode , string ( buf ) ) } fmt . Printf ( " \n " , time . Now ( ) . UTC ( ) . Format ( time . RFC3339 ) , len ( logs ) ) return nil }
PostMessages logs an array of messages to log analytics service
PostMessages logs an array of messages to log analytics service