github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/loki/http_client.go (about)

     1  // Copyright 2022 Ewout Prangsma
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Author Ewout Prangsma
    16  //
    17  
    18  package loki
    19  
    20  import (
    21  	"bytes"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"time"
    25  )
    26  
    27  const LOG_ENTRIES_CHAN_SIZE = 5000
    28  
    29  type clientConfig struct {
    30  	// E.g. http://localhost:3100/api/prom/push
    31  	PushURL string
    32  	// E.g. "{job=\"somejob\"}"
    33  	Labels             map[string]string
    34  	BatchWait          time.Duration
    35  	BatchEntriesNumber int
    36  }
    37  
    38  // http.Client wrapper for adding new methods, particularly sendJsonReq
    39  type httpClient struct {
    40  	parent http.Client
    41  }
    42  
    43  // A bit more convenient method for sending requests to the HTTP server
    44  func (client *httpClient) sendJsonReq(method, url string, ctype string, reqBody []byte) (resp *http.Response, resBody []byte, err error) {
    45  	req, err := http.NewRequest(method, url, bytes.NewBuffer(reqBody))
    46  	if err != nil {
    47  		return nil, nil, err
    48  	}
    49  
    50  	req.Header.Set("Content-Type", ctype)
    51  
    52  	resp, err = client.parent.Do(req)
    53  	if err != nil {
    54  		return nil, nil, err
    55  	}
    56  	defer resp.Body.Close()
    57  
    58  	resBody, err = ioutil.ReadAll(resp.Body)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	return resp, resBody, nil
    64  }