Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] util/log,diagnostics: new TELEMETRY channel, send reports to logging #64218

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,8 @@ EVENTLOG_PROTOS = \
pkg/util/log/eventpb/session_events.proto \
pkg/util/log/eventpb/sql_audit_events.proto \
pkg/util/log/eventpb/cluster_events.proto \
pkg/util/log/eventpb/job_events.proto
pkg/util/log/eventpb/job_events.proto \
pkg/util/log/eventpb/telemetry.proto

LOGSINKDOC_DEP = pkg/util/log/logconfig/config.go

Expand Down
25 changes: 25 additions & 0 deletions docs/generated/eventlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,31 @@ An event of type `drop_role` is recorded when a role is dropped.
| `ApplicationName` | The application name for the session where the event was emitted. This is included in the event to ease filtering of logging output by application. | yes |
| `PlaceholderValues` | The mapping of SQL placeholders to their values, for prepared statements. | yes |

## Telemetry events



Events in this category are logged to channel TELEMETRY.


### `diagnostic_report`

An event of type `diagnostic_report` is recorded periodically, at the interval
configurable via the cluster setting `diagnostics.reporting.interval`.


| Field | Description | Sensitive |
|--|--|--|
| `Report` | the telemetry payload, encoded as JSON. | yes |


#### Common fields

| Field | Description | Sensitive |
|--|--|--|
| `Timestamp` | The timestamp of the event. Expressed as nanoseconds since the Unix epoch. | no |
| `EventType` | The type of the event. | no |

## Zone config events

Events in this category pertain to zone config changes on
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ helping developers of CockroachDB itself. It exists as a separate
channel so as to not pollute the SQL perf logging output with
internal troubleshooting details.

## TELEMETRY

The TELEMETRY channel reports telemetry events. Telemetry events describe
feature usage within CockroachDB and anonymizes any application-
specific data.

13 changes: 12 additions & 1 deletion pkg/cli/testdata/logflags
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,18 @@ init
----
config: {<fileDefaultsNoMaxSize(/mypath)>,
<fluentDefaults>,
sinks: {file-groups: {default: {channels: all,
sinks: {file-groups: {default: {channels: [ DEV,
OPS,
HEALTH,
STORAGE,
SESSIONS,
SQL_SCHEMA,
USER_ADMIN,
PRIVILEGES,
SENSITIVE_ACCESS,
SQL_EXEC,
SQL_PERF,
SQL_INTERNAL_PERF],
dir: /mypath,
buffered-writes: true,
filter: INFO,
Expand Down
15 changes: 15 additions & 0 deletions pkg/roachpb/app_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package roachpb

import (
"encoding/json"
"fmt"
"math"

"github.com/cockroachdb/cockroach/pkg/util"
Expand Down Expand Up @@ -71,6 +73,19 @@ func (l *NumericStat) AlmostEqual(b NumericStat, eps float64) bool {
math.Abs(l.SquaredDiffs-b.SquaredDiffs) <= eps
}

// MarshalJSON implements the json.Marshaler interface.
func (l *NumericStat) MarshalJSON() ([]byte, error) {
// We need this code because of https://github.com/golang/go/issues/3480.
v := struct {
Mean string
SquaredDiffs string
}{
Mean: fmt.Sprintf("%f", l.Mean),
SquaredDiffs: fmt.Sprintf("%f", l.SquaredDiffs),
}
return json.Marshal(v)
}

// AddNumericStats combines derived statistics.
// Adapted from https://www.johndcook.com/blog/skewness_kurtosis/
func AddNumericStats(a, b NumericStat, countA, countB int64) NumericStat {
Expand Down
1 change: 1 addition & 0 deletions pkg/server/diagnostics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_library(
"//pkg/util/envutil",
"//pkg/util/httputil",
"//pkg/util/log",
"//pkg/util/log/eventpb",
"//pkg/util/log/logcrash",
"//pkg/util/protoutil",
"//pkg/util/stop",
Expand Down
16 changes: 16 additions & 0 deletions pkg/server/diagnostics/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package diagnostics
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -37,12 +39,14 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/cockroach/pkg/util/log/logcrash"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/kr/pretty"
"github.com/mitchellh/reflectwalk"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -121,6 +125,18 @@ func (r *Reporter) ReportDiagnostics(ctx context.Context) {

report := r.CreateReport(ctx, telemetry.ResetCounts)

// The eventpb.DiagnosticReport type is handled specially by
// logging: it presupposes that the report has been JSON-encoded
// already. Do it here.
log.Infof(ctx, "report: %s", fmt.Sprintf("%# v", pretty.Formatter(report)))
j, err := json.Marshal(report)
if err != nil {
logcrash.ReportOrPanic(ctx, &r.Settings.SV, "error encoding report as JSON: %v", err)
} else {
ev := eventpb.DiagnosticReport{Report: j}
log.StructuredEvent(ctx, &ev)
}

url := r.buildReportingURL(report)
if url == nil {
return
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/log/channel/channel_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/util/log/eventpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ proto_library(
"role_events.proto",
"session_events.proto",
"sql_audit_events.proto",
"telemetry.proto",
"zone_events.proto",
],
strip_import_prefix = "/pkg",
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/log/eventpb/eventlog_channels_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 23 additions & 11 deletions pkg/util/log/eventpb/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ type eventInfo struct {
}

type fieldInfo struct {
Comment string
FieldType string
FieldName string
ReportingSafe bool
Inherited bool
IsEnum bool
Comment string
FieldType string
FieldName string
ReportingSafe bool
Inherited bool
IsEnum bool
IsDiagnosticReport bool
}

func run() error {
Expand Down Expand Up @@ -362,11 +363,12 @@ func readInput(
safe = true
}
fi := fieldInfo{
Comment: comment,
FieldType: typ,
FieldName: name,
ReportingSafe: safe,
IsEnum: isEnum,
Comment: comment,
FieldType: typ,
FieldName: name,
ReportingSafe: safe,
IsEnum: isEnum,
IsDiagnosticReport: name == "Report" && curMsg.GoType == "DiagnosticReport",
}
curMsg.Fields = append(curMsg.Fields, fi)
curMsg.AllFields = append(curMsg.AllFields, fi)
Expand Down Expand Up @@ -441,6 +443,16 @@ func (m *{{.GoType}}) AppendJSONFields(printComma bool, b redact.RedactableBytes
{{range .AllFields }}
{{if .Inherited -}}
printComma, b = m.{{.FieldName}}.AppendJSONFields(printComma, b)
{{- else if .IsDiagnosticReport -}}
if m.{{.FieldName}} != nil {
if printComma { b = append(b, ',')}; printComma = true
b = append(b, "\"{{.FieldName}}\":"...)
// The DiagnosticReport is already pre-encoded as JSON, devoid
// of sensitive information.
// We can simply strip any markers contained and embed the result as-is.
payload := redact.RedactableBytes(redact.EscapeMarkers(m.{{.FieldName}}))
b = append(b, payload...)
}
{{- else if eq .FieldType "string" -}}
if m.{{.FieldName}} != "" {
if printComma { b = append(b, ',')}; printComma = true
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/log/eventpb/json_encode_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading