本文整理汇总了Golang中github.com/prometheus/client_golang/text.Parser类的典型用法代码示例。如果您正苦于以下问题:Golang Parser类的具体用法?Golang Parser怎么用?Golang Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parser类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ProcessSingle
func (t *processor004) ProcessSingle(i io.Reader, out Ingester, o *ProcessOptions) error {
var parser text.Parser
metricFamilies, err := parser.TextToMetricFamilies(i)
if err != nil {
return err
}
for _, metricFamily := range metricFamilies {
if err := extractMetricFamily(out, o, metricFamily); err != nil {
return err
}
}
return nil
}
开发者ID:alena1108,项目名称:kubernetes,代码行数:13,代码来源:textprocessor.go
示例2: fetchMetricFamilies
func fetchMetricFamilies(url string, ch chan<- *dto.MetricFamily) {
defer close(ch)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("creating GET request for URL %q failed: %s", url, err)
}
req.Header.Add("Accept", acceptHeader)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("executing GET request for URL %q failed: %s", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalf("GET request for URL %q returned HTTP status %s", url, resp.Status)
}
mediatype, params, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if err == nil && mediatype == "application/vnd.google.protobuf" &&
params["encoding"] == "delimited" &&
params["proto"] == "io.prometheus.client.MetricFamily" {
for {
mf := &dto.MetricFamily{}
if _, err = pbutil.ReadDelimited(resp.Body, mf); err != nil {
if err == io.EOF {
break
}
log.Fatalln("reading metric family protocol buffer failed:", err)
}
ch <- mf
}
} else {
// We could do further content-type checks here, but the
// fallback for now will anyway be the text format
// version 0.0.4, so just go for it and see if it works.
var parser text.Parser
metricFamilies, err := parser.TextToMetricFamilies(resp.Body)
if err != nil {
log.Fatalln("reading text format failed:", err)
}
for _, mf := range metricFamilies {
ch <- mf
}
}
}
开发者ID:gonzaloserrano,项目名称:prom2json,代码行数:44,代码来源:main.go
示例3: Push
// Push returns an http.Handler which accepts samples over HTTP and stores them
// in the MetricStore. If replace is true, all metrics for the job and instance
// given by the request are deleted before new ones are stored.
//
// The returned handler is already instrumented for Prometheus.
func Push(
ms storage.MetricStore, replace bool,
) func(http.ResponseWriter, *http.Request, httprouter.Params) {
var ps httprouter.Params
var mtx sync.Mutex // Protects ps.
instrumentedHandlerFunc := prometheus.InstrumentHandlerFunc(
"push",
func(w http.ResponseWriter, r *http.Request) {
job := ps.ByName("job")
labelsString := ps.ByName("labels")
mtx.Unlock()
labels, err := splitLabels(labelsString)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if job == "" {
http.Error(w, "job name is required", http.StatusBadRequest)
return
}
labels["job"] = job
if replace {
ms.SubmitWriteRequest(storage.WriteRequest{
Labels: labels,
Timestamp: time.Now(),
})
}
var metricFamilies map[string]*dto.MetricFamily
ctMediatype, ctParams, ctErr := mime.ParseMediaType(r.Header.Get("Content-Type"))
if ctErr == nil && ctMediatype == "application/vnd.google.protobuf" &&
ctParams["encoding"] == "delimited" &&
ctParams["proto"] == "io.prometheus.client.MetricFamily" {
metricFamilies = map[string]*dto.MetricFamily{}
for {
mf := &dto.MetricFamily{}
if _, err = pbutil.ReadDelimited(r.Body, mf); err != nil {
if err == io.EOF {
err = nil
}
break
}
metricFamilies[mf.GetName()] = mf
}
} else {
// We could do further content-type checks here, but the
// fallback for now will anyway be the text format
// version 0.0.4, so just go for it and see if it works.
var parser text.Parser
metricFamilies, err = parser.TextToMetricFamilies(r.Body)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sanitizeLabels(metricFamilies, labels)
ms.SubmitWriteRequest(storage.WriteRequest{
Labels: labels,
Timestamp: time.Now(),
MetricFamilies: metricFamilies,
})
w.WriteHeader(http.StatusAccepted)
},
)
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
mtx.Lock()
ps = params
instrumentedHandlerFunc(w, r)
}
}
开发者ID:xbglowx,项目名称:pushgateway,代码行数:79,代码来源:push.go
示例4: parseTextFiles
func parseTextFiles() []*dto.MetricFamily {
var parser text.Parser
error := 0.0
metricFamilies := make([]*dto.MetricFamily, 0)
mtimes := map[string]time.Time{}
// Iterate over files and accumulate their metrics.
files, _ := ioutil.ReadDir(*textFileDirectory)
for _, f := range files {
if !strings.HasSuffix(f.Name(), ".prom") {
continue
}
path := filepath.Join(*textFileDirectory, f.Name())
file, err := os.Open(path)
if err != nil {
log.Errorf("Error opening %s: %v", path, err)
error = 1.0
continue
}
parsedFamilies, err := parser.TextToMetricFamilies(file)
if err != nil {
log.Errorf("Error parsing %s: %v", path, err)
error = 1.0
continue
}
// Only set this once it has been parsed, so that
// a failure does not appear fresh.
mtimes[f.Name()] = f.ModTime()
for _, mf := range parsedFamilies {
if mf.Help == nil {
help := fmt.Sprintf("Metric read from %s", path)
mf.Help = &help
}
metricFamilies = append(metricFamilies, mf)
}
}
// Export the mtimes of the successful files.
if len(mtimes) > 0 {
mtimeMetricFamily := dto.MetricFamily{
Name: proto.String("node_textfile_mtime"),
Help: proto.String("Unixtime mtime of textfiles successfully read."),
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{},
}
for name, mtime := range mtimes {
mtimeMetricFamily.Metric = append(mtimeMetricFamily.Metric,
&dto.Metric{
Label: []*dto.LabelPair{
&dto.LabelPair{
Name: proto.String("file"),
Value: &name,
},
},
Gauge: &dto.Gauge{Value: proto.Float64(float64(mtime.UnixNano()) / 1e9)},
},
)
}
metricFamilies = append(metricFamilies, &mtimeMetricFamily)
}
// Export if there were errors.
metricFamilies = append(metricFamilies, &dto.MetricFamily{
Name: proto.String("node_textfile_scrape_error"),
Help: proto.String("1 if there was an error opening or reading a file, 0 otherwise"),
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{
&dto.Metric{
Gauge: &dto.Gauge{Value: &error},
},
},
})
return metricFamilies
}
开发者ID:fin09pcap,项目名称:node_exporter,代码行数:74,代码来源:textfile.go
注:本文中的github.com/prometheus/client_golang/text.Parser类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论