Skip to content

Commit

Permalink
Merge 7b2a077 into a590ea3
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Mar 20, 2019
2 parents a590ea3 + 7b2a077 commit bbe204e
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions mackerel-plugin-accesslog/lib/accesslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -99,20 +100,26 @@ func (p *AccesslogPlugin) getReadCloser() (io.ReadCloser, bool, error) {
}
posfile := p.getPosPath()
fi, err := os.Stat(posfile)
// don't output count metrics when the pos file doesn't exist or is too old
takeCount := err == nil && fi.ModTime().After(time.Now().Add(-2*time.Minute))
// don't output any metrics when the pos file doesn't exist or is too old
takeMetrics := err == nil && fi.ModTime().After(time.Now().Add(-2*time.Minute))
rc, err := postailer.Open(p.file, posfile)
return rc, takeCount, err
return rc, takeMetrics, err
}

// FetchMetrics interface for mackerelplugin
func (p *AccesslogPlugin) FetchMetrics() (map[string]float64, error) {
rc, takeCount, err := p.getReadCloser()
rc, takeMetrics, err := p.getReadCloser()
if err != nil {
return nil, err
}
defer rc.Close()

if !takeMetrics {
// discard existing contents to seek position
_, err := ioutil.ReadAll(rc)
return map[string]float64{}, err
}

countMetrics := []string{"total_count", "2xx_count", "3xx_count", "4xx_count", "5xx_count"}
ret := make(map[string]float64)
for _, k := range countMetrics {
Expand Down Expand Up @@ -171,11 +178,6 @@ func (p *AccesslogPlugin) FetchMetrics() (map[string]float64, error) {
ret[fmt.Sprintf("%d", v)+"_percentile"], _ = stats.Percentile(reqtimes, float64(v))
}
}
if !takeCount {
for _, k := range countMetrics {
delete(ret, k)
}
}
return ret, nil
}

Expand Down

0 comments on commit bbe204e

Please sign in to comment.