Skip to content

Commit

Permalink
Updated go coverprofiles
Browse files Browse the repository at this point in the history
Commit: a5a4d71
Signed-off-by: bot-ahsoka[bot] <bot-ahsoka[bot]@users.noreply.github.com>
  • Loading branch information
bot-ahsoka[bot] committed Jan 4, 2025
1 parent d6dc55a commit acaf1d0
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@
<div id="legend">
<span>not tracked</span>

<span class="cov0">not covered</span>
<span class="cov8">covered</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>

</div>
</div>
Expand Down Expand Up @@ -103,96 +112,96 @@
Value float64
}

func NewWriteClient(endpoint, instance, job string, reg *prometheus.Registry) (*Client, error) <span class="cov8" title="1">{
if endpoint == "" </span><span class="cov8" title="1">{
func NewWriteClient(endpoint, instance, job string, reg *prometheus.Registry) (*Client, error) <span class="cov10" title="8">{
if endpoint == "" </span><span class="cov1" title="1">{
return nil, ErrMissingEndpoint{}
}</span>
<span class="cov8" title="1">if instance == "" </span><span class="cov8" title="1">{
<span class="cov9" title="7">if instance == "" </span><span class="cov1" title="1">{
return nil, ErrMissingInstance{}
}</span>
<span class="cov8" title="1">if job == "" </span><span class="cov8" title="1">{
<span class="cov8" title="6">if job == "" </span><span class="cov1" title="1">{
return nil, ErrMissingJob{}
}</span>
<span class="cov8" title="1">if reg == nil </span><span class="cov8" title="1">{
<span class="cov7" title="5">if reg == nil </span><span class="cov1" title="1">{
return nil, ErrMissingRegistry{}
}</span>
<span class="cov8" title="1">return &amp;Client{
<span class="cov7" title="4">return &amp;Client{
endpoint: endpoint,
instance: instance,
job: job,
registry: reg,
}, nil</span>
}

func (c *Client) Endpoint() string <span class="cov8" title="1">{
if c == nil </span><span class="cov8" title="1">{
func (c *Client) Endpoint() string <span class="cov7" title="4">{
if c == nil </span><span class="cov1" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.endpoint</span>
<span class="cov5" title="3">return c.endpoint</span>
}

func (c *Client) Registry() *prometheus.Registry <span class="cov8" title="1">{
if c == nil </span><span class="cov8" title="1">{
func (c *Client) Registry() *prometheus.Registry <span class="cov4" title="2">{
if c == nil </span><span class="cov1" title="1">{
return nil
}</span>
<span class="cov8" title="1">return c.registry</span>
<span class="cov1" title="1">return c.registry</span>
}

// Set credentials needed for basic auth, return error if not provided
func (c *Client) SetBasicAuth(username, password string) error <span class="cov8" title="1">{
if username == "" || password == "" </span><span class="cov8" title="1">{
func (c *Client) SetBasicAuth(username, password string) error <span class="cov7" title="4">{
if username == "" || password == "" </span><span class="cov5" title="3">{
return ErrMissingAuthCredentials{}
}</span>
<span class="cov8" title="1">c.username = username
<span class="cov1" title="1">c.username = username
c.password = password
return nil</span>
}

// Send TimeSeries to remote_write endpoint
func (c *Client) post(ts []prompb.TimeSeries) error <span class="cov8" title="1">{
func (c *Client) post(ts []prompb.TimeSeries) error <span class="cov4" title="2">{
wr := prompb.WriteRequest{Timeseries: ts}
data, err := wr.Marshal()
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">body := snappy.Encode(nil, data)
<span class="cov4" title="2">body := snappy.Encode(nil, data)

req, err := http.NewRequest(http.MethodPost, c.Endpoint(), bytes.NewReader(body))
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">req.Header.Add("Content-Encoding", "snappy")
<span class="cov4" title="2">req.Header.Add("Content-Encoding", "snappy")
req.Header.Add("Content-Type", "application/x-protobuf")
req.Header.Set("X-Prometheus-Remote-Read-Version", "0.1.0")
if c.username != "" </span><span class="cov8" title="1">{
if c.username != "" </span><span class="cov1" title="1">{
req.SetBasicAuth(c.username, c.password)
}</span>

<span class="cov8" title="1">httpClient := http.Client{
<span class="cov4" title="2">httpClient := http.Client{
Timeout: time.Duration(10 * time.Second),
}
res, err := httpClient.Do(req)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">defer res.Body.Close()
<span class="cov4" title="2">defer res.Body.Close()
if res.StatusCode != http.StatusOK </span><span class="cov0" title="0">{
return NewErrRemoteWriteFailed(res.StatusCode, req.Body)
}</span>

<span class="cov8" title="1">return nil</span>
<span class="cov4" title="2">return nil</span>
}

// Collect metrics from registry and convert them to TimeSeries
func (c *Client) collect() ([]prompb.TimeSeries, error) <span class="cov8" title="1">{
func (c *Client) collect() ([]prompb.TimeSeries, error) <span class="cov1" title="1">{
ch := make(chan prometheus.Metric)
go func() </span><span class="cov8" title="1">{
go func() </span><span class="cov1" title="1">{
c.registry.Collect(ch)
close(ch)
}</span>()

<span class="cov8" title="1">var res []prompb.TimeSeries
for metric := range ch </span><span class="cov8" title="1">{
<span class="cov1" title="1">var res []prompb.TimeSeries
for metric := range ch </span><span class="cov1" title="1">{
// Extract name of metric
regex := regexp.MustCompile("fqName: \"([a-zA-Z_:][a-zA-Z0-9_:]*)\"")
fqName := regex.FindStringSubmatch(metric.Desc().String())
Expand All @@ -201,14 +210,14 @@
}</span>

// Convert metric to readable format
<span class="cov8" title="1">m := &amp;dto.Metric{}
<span class="cov1" title="1">m := &amp;dto.Metric{}
err := metric.Write(m)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>

// Extract lables
<span class="cov8" title="1">labels := make([]prompb.Label, len(m.Label)+3)
<span class="cov1" title="1">labels := make([]prompb.Label, len(m.Label)+3)
labels[0] = prompb.Label{
Name: "__name__",
Value: fqName[1],
Expand All @@ -221,29 +230,29 @@
Name: "job",
Value: c.job,
}
for i, l := range m.Label </span><span class="cov8" title="1">{
for i, l := range m.Label </span><span class="cov5" title="3">{
labels[i+3] = prompb.Label{
Name: l.GetName(),
Value: l.GetValue(),
}
}</span>

<span class="cov8" title="1">ts := prompb.TimeSeries{
<span class="cov1" title="1">ts := prompb.TimeSeries{
Labels: labels,
}

// Extract value and timestamp
var value float64
if m.Counter != nil </span><span class="cov0" title="0">{
value = m.Counter.GetValue()
}</span> else<span class="cov8" title="1"> if m.Gauge != nil </span><span class="cov8" title="1">{
}</span> else<span class="cov1" title="1"> if m.Gauge != nil </span><span class="cov1" title="1">{
value = m.Gauge.GetValue()
}</span> else<span class="cov0" title="0"> if m.Untyped != nil </span><span class="cov0" title="0">{
value = m.Counter.GetValue()
}</span> else<span class="cov0" title="0"> {
return nil, fmt.Errorf("Unknown metric type")
}</span>
<span class="cov8" title="1">ts.Samples = []prompb.Sample{
<span class="cov1" title="1">ts.Samples = []prompb.Sample{
{
Value: value,
Timestamp: timestamp.FromTime(time.Now()),
Expand All @@ -252,7 +261,7 @@

res = append(res, ts)</span>
}
<span class="cov8" title="1">return res, nil</span>
<span class="cov1" title="1">return res, nil</span>
}

// Collect metrics and send them to remote server in interval.
Expand Down

0 comments on commit acaf1d0

Please sign in to comment.