aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/profile_events.go
blob: 734b7760e77eb60680e17a009393cd90d9dbf3e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package proto

import (
	"time"

	"github.com/go-faster/errors"
)

// ProfileEvents is data of ServerProfileEvents packet.
type ProfileEvents struct {
	Host     ColStr
	Time     ColDateTime
	ThreadID ColUInt64
	Type     ColInt8
	Name     ColStr
	Value    ColAuto // UInt64 or Int64 depending on version
}

func (d *ProfileEvents) All() ([]ProfileEvent, error) {
	var out []ProfileEvent
	for i := range d.Type {
		e := ProfileEvent{
			Time:     d.Time.Row(i),
			Host:     d.Host.Row(i),
			ThreadID: d.ThreadID[i],
			Type:     ProfileEventType(d.Type[i]),
			Name:     d.Name.Row(i),
		}
		switch data := d.Value.Data.(type) {
		case *ColInt64:
			e.Value = (*data)[i]
		case *ColUInt64:
			e.Value = int64((*data)[i])
		default:
			return nil, errors.Errorf("unexpected type %q for metric column", data.Type())
		}
		out = append(out, e)
	}
	return out, nil
}

func (d *ProfileEvents) Result() Results {
	return Results{
		{Name: "host_name", Data: &d.Host},
		{Name: "current_time", Data: &d.Time},
		{Name: "thread_id", Data: &d.ThreadID},
		{Name: "type", Data: &d.Type},
		{Name: "name", Data: &d.Name},
		{Name: "value", Data: &d.Value},
	}
}

//go:generate go run github.com/dmarkham/enumer -type ProfileEventType -trimprefix Profile -output profile_enum.go

type ProfileEventType byte

const (
	ProfileIncrement ProfileEventType = 1
	ProfileGauge     ProfileEventType = 2
)

// ProfileEvent is detailed profiling event from Server.
type ProfileEvent struct {
	ThreadID uint64
	Host     string
	Time     time.Time
	Type     ProfileEventType
	Name     string
	Value    int64
}