GoPLS Viewer

Home|gopls/internal/event/export/prometheus/prometheus.go
1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package prometheus
6
7import (
8    "bytes"
9    "context"
10    "fmt"
11    "net/http"
12    "sort"
13    "sync"
14
15    "golang.org/x/tools/internal/event"
16    "golang.org/x/tools/internal/event/core"
17    "golang.org/x/tools/internal/event/export/metric"
18    "golang.org/x/tools/internal/event/label"
19)
20
21func New() *Exporter {
22    return &Exporter{}
23}
24
25type Exporter struct {
26    mu      sync.Mutex
27    metrics []metric.Data
28}
29
30func (e *ExporterProcessEvent(ctx context.Contextev core.Eventlm label.Mapcontext.Context {
31    if !event.IsMetric(ev) {
32        return ctx
33    }
34    e.mu.Lock()
35    defer e.mu.Unlock()
36    metrics := metric.Entries.Get(lm).([]metric.Data)
37    for _data := range metrics {
38        name := data.Handle()
39        // We keep the metrics in name sorted order so the page is stable and easy
40        // to read. We do this with an insertion sort rather than sorting the list
41        // each time
42        index := sort.Search(len(e.metrics), func(i intbool {
43            return e.metrics[i].Handle() >= name
44        })
45        if index >= len(e.metrics) || e.metrics[index].Handle() != name {
46            // we have a new metric, so we need to make a space for it
47            old := e.metrics
48            e.metrics = make([]metric.Datalen(old)+1)
49            copy(e.metricsold[:index])
50            copy(e.metrics[index+1:], old[index:])
51        }
52        e.metrics[index] = data
53    }
54    return ctx
55}
56
57func (e *Exporterheader(w http.ResponseWriternamedescription stringisGaugeisHistogram bool) {
58    kind := "counter"
59    if isGauge {
60        kind = "gauge"
61    }
62    if isHistogram {
63        kind = "histogram"
64    }
65    fmt.Fprintf(w"# HELP %s %s\n"namedescription)
66    fmt.Fprintf(w"# TYPE %s %s\n"namekind)
67}
68
69func (e *Exporterrow(w http.ResponseWritername stringgroup []label.Labelextra stringvalue interface{}) {
70    fmt.Fprint(wname)
71    buf := &bytes.Buffer{}
72    fmt.Fprint(bufgroup)
73    if extra != "" {
74        if buf.Len() > 0 {
75            fmt.Fprint(buf",")
76        }
77        fmt.Fprint(bufextra)
78    }
79    if buf.Len() > 0 {
80        fmt.Fprint(w"{")
81        buf.WriteTo(w)
82        fmt.Fprint(w"}")
83    }
84    fmt.Fprintf(w" %v\n"value)
85}
86
87func (e *ExporterServe(w http.ResponseWriterr *http.Request) {
88    e.mu.Lock()
89    defer e.mu.Unlock()
90    for _data := range e.metrics {
91        switch data := data.(type) {
92        case *metric.Int64Data:
93            e.header(wdata.Info.Namedata.Info.Descriptiondata.IsGaugefalse)
94            for igroup := range data.Groups() {
95                e.row(wdata.Info.Namegroup""data.Rows[i])
96            }
97
98        case *metric.Float64Data:
99            e.header(wdata.Info.Namedata.Info.Descriptiondata.IsGaugefalse)
100            for igroup := range data.Groups() {
101                e.row(wdata.Info.Namegroup""data.Rows[i])
102            }
103
104        case *metric.HistogramInt64Data:
105            e.header(wdata.Info.Namedata.Info.Descriptionfalsetrue)
106            for igroup := range data.Groups() {
107                row := data.Rows[i]
108                for jb := range data.Info.Buckets {
109                    e.row(wdata.Info.Name+"_bucket"groupfmt.Sprintf(`le="%v"`b), row.Values[j])
110                }
111                e.row(wdata.Info.Name+"_bucket"group`le="+Inf"`row.Count)
112                e.row(wdata.Info.Name+"_count"group""row.Count)
113                e.row(wdata.Info.Name+"_sum"group""row.Sum)
114            }
115
116        case *metric.HistogramFloat64Data:
117            e.header(wdata.Info.Namedata.Info.Descriptionfalsetrue)
118            for igroup := range data.Groups() {
119                row := data.Rows[i]
120                for jb := range data.Info.Buckets {
121                    e.row(wdata.Info.Name+"_bucket"groupfmt.Sprintf(`le="%v"`b), row.Values[j])
122                }
123                e.row(wdata.Info.Name+"_bucket"group`le="+Inf"`row.Count)
124                e.row(wdata.Info.Name+"_count"group""row.Count)
125                e.row(wdata.Info.Name+"_sum"group""row.Sum)
126            }
127        }
128    }
129}
130
MembersX
Exporter.ProcessEvent.RangeStmt_765.BlockStmt.name
context
label
Exporter.mu
Exporter.header.kind
Exporter.Serve.e
Exporter.row.buf
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2582.i
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2785.BlockStmt.RangeStmt_2851.j
http
Exporter.ProcessEvent.e
Exporter.ProcessEvent.lm
Exporter
Exporter.Serve
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2378.group
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2785.i
sync
metric
Exporter.row.extra
Exporter.Serve.r
Exporter.ProcessEvent.ctx
Exporter.header
Exporter.header.w
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2785.group
Exporter.ProcessEvent.RangeStmt_765.BlockStmt.index
Exporter.Serve.w
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2785.BlockStmt.RangeStmt_2851.b
Exporter.header.name
Exporter.row.e
Exporter.Serve.RangeStmt_2210.data
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2582.group
Exporter.ProcessEvent.RangeStmt_765.data
Exporter.ProcessEvent.RangeStmt_765.BlockStmt.BlockStmt.old
Exporter.header.e
Exporter.header.isHistogram
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_2378.i
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_3283.group
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_3283.BlockStmt.RangeStmt_3349.j
New
Exporter.header.description
Exporter.header.isGauge
Exporter.row.value
event
Exporter.metrics
Exporter.ProcessEvent.ev
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_3283.i
Exporter.Serve.RangeStmt_2210.BlockStmt.BlockStmt.RangeStmt_3283.BlockStmt.RangeStmt_3349.b
bytes
fmt
Exporter.row.w
sort
core
Exporter.row
Exporter.ProcessEvent
Exporter.row.name
Exporter.row.group
Members
X