2019-07-17 17:17:43 -06:00
|
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Package ocagent adds the ability to export all telemetry to an ocagent.
|
2019-09-06 11:19:11 -06:00
|
|
|
|
// This keeps the compile time dependencies to zero and allows the agent to
|
2019-07-17 17:17:43 -06:00
|
|
|
|
// have the exporters needed for telemetry aggregation and viewing systems.
|
|
|
|
|
package ocagent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2019-08-14 10:51:42 -06:00
|
|
|
|
"context"
|
2019-07-17 17:17:43 -06:00
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2019-08-15 20:43:07 -06:00
|
|
|
|
"path/filepath"
|
2019-08-17 21:26:28 -06:00
|
|
|
|
"sync"
|
2019-07-17 17:17:43 -06:00
|
|
|
|
"time"
|
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2020-03-01 16:35:55 -07:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/export"
|
2020-03-17 14:00:16 -06:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/export/metric"
|
2019-08-14 10:51:42 -06:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/export/ocagent/wire"
|
2019-07-17 17:17:43 -06:00
|
|
|
|
)
|
|
|
|
|
|
2019-08-15 20:43:07 -06:00
|
|
|
|
type Config struct {
|
|
|
|
|
Start time.Time
|
|
|
|
|
Host string
|
|
|
|
|
Process uint32
|
|
|
|
|
Client *http.Client
|
|
|
|
|
Service string
|
|
|
|
|
Address string
|
|
|
|
|
Rate time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Discover finds the local agent to export to, it will return nil if there
|
|
|
|
|
// is not one running.
|
|
|
|
|
// TODO: Actually implement a discovery protocol rather than a hard coded address
|
|
|
|
|
func Discover() *Config {
|
|
|
|
|
return &Config{
|
|
|
|
|
Address: "http://localhost:55678",
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
|
2020-02-28 12:08:49 -07:00
|
|
|
|
type Exporter struct {
|
2019-08-17 21:26:28 -06:00
|
|
|
|
mu sync.Mutex
|
2019-08-15 20:43:07 -06:00
|
|
|
|
config Config
|
2020-03-01 16:35:55 -07:00
|
|
|
|
spans []*export.Span
|
2020-03-17 14:00:16 -06:00
|
|
|
|
metrics []metric.Data
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
// Connect creates a process specific exporter with the specified
|
|
|
|
|
// serviceName and the address of the ocagent to which it will upload
|
|
|
|
|
// its telemetry.
|
2020-02-28 12:08:49 -07:00
|
|
|
|
func Connect(config *Config) *Exporter {
|
2019-08-15 20:43:07 -06:00
|
|
|
|
if config == nil || config.Address == "off" {
|
2019-08-14 10:51:42 -06:00
|
|
|
|
return nil
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
2020-02-28 12:08:49 -07:00
|
|
|
|
exporter := &Exporter{config: *config}
|
2019-08-15 20:43:07 -06:00
|
|
|
|
if exporter.config.Start.IsZero() {
|
|
|
|
|
exporter.config.Start = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if exporter.config.Host == "" {
|
|
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
|
exporter.config.Host = hostname
|
|
|
|
|
}
|
|
|
|
|
if exporter.config.Process == 0 {
|
|
|
|
|
exporter.config.Process = uint32(os.Getpid())
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
2019-08-15 20:43:07 -06:00
|
|
|
|
if exporter.config.Client == nil {
|
|
|
|
|
exporter.config.Client = http.DefaultClient
|
|
|
|
|
}
|
|
|
|
|
if exporter.config.Service == "" {
|
|
|
|
|
exporter.config.Service = filepath.Base(os.Args[0])
|
|
|
|
|
}
|
|
|
|
|
if exporter.config.Rate == 0 {
|
|
|
|
|
exporter.config.Rate = 2 * time.Second
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
go func() {
|
2019-11-20 20:43:00 -07:00
|
|
|
|
for range time.Tick(exporter.config.Rate) {
|
2019-08-15 20:43:07 -06:00
|
|
|
|
exporter.Flush()
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
}()
|
2019-08-14 10:51:42 -06:00
|
|
|
|
return exporter
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 06:29:48 -06:00
|
|
|
|
func (e *Exporter) ProcessEvent(ctx context.Context, ev event.Event, tagMap event.TagMap) context.Context {
|
2020-03-17 14:00:16 -06:00
|
|
|
|
switch {
|
|
|
|
|
case ev.IsEndSpan():
|
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
span := export.GetSpan(ctx)
|
|
|
|
|
if span != nil {
|
|
|
|
|
e.spans = append(e.spans, span)
|
|
|
|
|
}
|
|
|
|
|
case ev.IsRecord():
|
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
2020-03-20 06:29:48 -06:00
|
|
|
|
data := metric.Entries.Get(tagMap).([]metric.Data)
|
2020-03-17 14:00:16 -06:00
|
|
|
|
e.metrics = append(e.metrics, data...)
|
2020-03-01 16:35:55 -07:00
|
|
|
|
}
|
2020-03-20 06:29:48 -06:00
|
|
|
|
return ctx
|
2020-03-01 10:16:26 -07:00
|
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
|
|
2020-02-28 12:08:49 -07:00
|
|
|
|
func (e *Exporter) Flush() {
|
2019-08-17 21:26:28 -06:00
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
2019-11-18 07:51:25 -07:00
|
|
|
|
spans := make([]*wire.Span, len(e.spans))
|
|
|
|
|
for i, s := range e.spans {
|
|
|
|
|
spans[i] = convertSpan(s)
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
e.spans = nil
|
2019-11-25 08:15:35 -07:00
|
|
|
|
metrics := make([]*wire.Metric, len(e.metrics))
|
|
|
|
|
for i, m := range e.metrics {
|
|
|
|
|
metrics[i] = convertMetric(m, e.config.Start)
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
e.metrics = nil
|
|
|
|
|
|
|
|
|
|
if len(spans) > 0 {
|
|
|
|
|
e.send("/v1/trace", &wire.ExportTraceServiceRequest{
|
2019-11-26 22:19:20 -07:00
|
|
|
|
Node: e.config.buildNode(),
|
2019-07-17 17:17:43 -06:00
|
|
|
|
Spans: spans,
|
|
|
|
|
//TODO: Resource?
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if len(metrics) > 0 {
|
|
|
|
|
e.send("/v1/metrics", &wire.ExportMetricsServiceRequest{
|
2019-11-26 22:19:20 -07:00
|
|
|
|
Node: e.config.buildNode(),
|
2019-07-17 17:17:43 -06:00
|
|
|
|
Metrics: metrics,
|
|
|
|
|
//TODO: Resource?
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 22:19:20 -07:00
|
|
|
|
func (cfg *Config) buildNode() *wire.Node {
|
2019-11-26 11:47:17 -07:00
|
|
|
|
return &wire.Node{
|
|
|
|
|
Identifier: &wire.ProcessIdentifier{
|
2019-11-26 22:19:20 -07:00
|
|
|
|
HostName: cfg.Host,
|
|
|
|
|
Pid: cfg.Process,
|
|
|
|
|
StartTimestamp: convertTimestamp(cfg.Start),
|
2019-11-26 11:47:17 -07:00
|
|
|
|
},
|
|
|
|
|
LibraryInfo: &wire.LibraryInfo{
|
|
|
|
|
Language: wire.LanguageGo,
|
|
|
|
|
ExporterVersion: "0.0.1",
|
|
|
|
|
CoreLibraryVersion: "x/tools",
|
|
|
|
|
},
|
|
|
|
|
ServiceInfo: &wire.ServiceInfo{
|
2019-11-26 22:19:20 -07:00
|
|
|
|
Name: cfg.Service,
|
2019-11-26 11:47:17 -07:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 12:08:49 -07:00
|
|
|
|
func (e *Exporter) send(endpoint string, message interface{}) {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
blob, err := json.Marshal(message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errorInExport("ocagent failed to marshal message for %v: %v", endpoint, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-08-15 20:43:07 -06:00
|
|
|
|
uri := e.config.Address + endpoint
|
2019-07-17 17:17:43 -06:00
|
|
|
|
req, err := http.NewRequest("POST", uri, bytes.NewReader(blob))
|
|
|
|
|
if err != nil {
|
|
|
|
|
errorInExport("ocagent failed to build request for %v: %v", uri, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2019-08-15 20:43:07 -06:00
|
|
|
|
res, err := e.config.Client.Do(req)
|
2019-07-17 17:17:43 -06:00
|
|
|
|
if err != nil {
|
|
|
|
|
errorInExport("ocagent failed to send message: %v \n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-08-15 20:43:07 -06:00
|
|
|
|
if res.Body != nil {
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func errorInExport(message string, args ...interface{}) {
|
|
|
|
|
// This function is useful when debugging the exporter, but in general we
|
|
|
|
|
// want to just drop any export
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertTimestamp(t time.Time) wire.Timestamp {
|
|
|
|
|
return t.Format(time.RFC3339Nano)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toTruncatableString(s string) *wire.TruncatableString {
|
2019-08-03 17:13:38 -06:00
|
|
|
|
if s == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return &wire.TruncatableString{Value: s}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 16:35:55 -07:00
|
|
|
|
func convertSpan(span *export.Span) *wire.Span {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
result := &wire.Span{
|
2019-11-20 20:43:00 -07:00
|
|
|
|
TraceID: span.ID.TraceID[:],
|
|
|
|
|
SpanID: span.ID.SpanID[:],
|
2019-07-17 17:17:43 -06:00
|
|
|
|
TraceState: nil, //TODO?
|
2019-11-20 20:43:00 -07:00
|
|
|
|
ParentSpanID: span.ParentID[:],
|
2019-07-17 17:17:43 -06:00
|
|
|
|
Name: toTruncatableString(span.Name),
|
|
|
|
|
Kind: wire.UnspecifiedSpanKind,
|
2020-03-27 16:59:18 -06:00
|
|
|
|
StartTime: convertTimestamp(span.Start().At),
|
|
|
|
|
EndTime: convertTimestamp(span.Finish().At),
|
|
|
|
|
Attributes: convertAttributes(event.Filter(span.Start().Tags(), event.Name)),
|
|
|
|
|
TimeEvents: convertEvents(span.Events()),
|
2019-07-17 17:17:43 -06:00
|
|
|
|
SameProcessAsParentSpan: true,
|
|
|
|
|
//TODO: StackTrace?
|
|
|
|
|
//TODO: Links?
|
|
|
|
|
//TODO: Status?
|
|
|
|
|
//TODO: Resource?
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:00:16 -06:00
|
|
|
|
func convertMetric(data metric.Data, start time.Time) *wire.Metric {
|
2019-10-10 01:40:19 -06:00
|
|
|
|
descriptor := dataToMetricDescriptor(data)
|
2019-10-23 18:34:23 -06:00
|
|
|
|
timeseries := dataToTimeseries(data, start)
|
2019-10-10 01:40:19 -06:00
|
|
|
|
|
|
|
|
|
if descriptor == nil && timeseries == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: handle Histogram metrics
|
|
|
|
|
return &wire.Metric{
|
|
|
|
|
MetricDescriptor: descriptor,
|
|
|
|
|
Timeseries: timeseries,
|
|
|
|
|
// TODO: attach Resource?
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 06:29:48 -06:00
|
|
|
|
func convertAttributes(it event.TagIterator) *wire.Attributes {
|
|
|
|
|
if !it.Valid() {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
attributes := make(map[string]wire.Attribute)
|
2020-03-20 06:29:48 -06:00
|
|
|
|
for ; it.Valid(); it.Advance() {
|
|
|
|
|
tag := it.Tag()
|
2020-03-24 21:03:59 -06:00
|
|
|
|
attributes[tag.Key.Name()] = convertAttribute(tag)
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
return &wire.Attributes{AttributeMap: attributes}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:03:59 -06:00
|
|
|
|
func convertAttribute(tag event.Tag) wire.Attribute {
|
|
|
|
|
switch key := tag.Key.(type) {
|
|
|
|
|
case *event.IntKey:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.Int8Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.Int16Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.Int32Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.Int64Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.UIntKey:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.UInt8Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.UInt16Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.UInt32Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.UInt64Key:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
|
|
|
|
case *event.Float32Key:
|
|
|
|
|
return wire.DoubleAttribute{DoubleValue: float64(key.From(tag))}
|
|
|
|
|
case *event.Float64Key:
|
|
|
|
|
return wire.DoubleAttribute{DoubleValue: key.From(tag)}
|
|
|
|
|
case *event.BooleanKey:
|
|
|
|
|
return wire.BoolAttribute{BoolValue: key.From(tag)}
|
|
|
|
|
case *event.StringKey:
|
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(key.From(tag))}
|
|
|
|
|
case *event.ErrorKey:
|
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(key.From(tag).Error())}
|
|
|
|
|
case *event.ValueKey:
|
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprint(key.From(tag)))}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
default:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprintf("%T", key))}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
|
func convertEvents(events []event.Event) *wire.TimeEvents {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
//TODO: MessageEvents?
|
|
|
|
|
result := make([]wire.TimeEvent, len(events))
|
|
|
|
|
for i, event := range events {
|
|
|
|
|
result[i] = convertEvent(event)
|
|
|
|
|
}
|
|
|
|
|
return &wire.TimeEvents{TimeEvent: result}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
|
func convertEvent(ev event.Event) wire.TimeEvent {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return wire.TimeEvent{
|
2020-03-07 16:02:27 -07:00
|
|
|
|
Time: convertTimestamp(ev.At),
|
|
|
|
|
Annotation: convertAnnotation(ev),
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
|
func convertAnnotation(ev event.Event) *wire.Annotation {
|
2020-03-20 06:29:48 -06:00
|
|
|
|
tags := ev.Tags()
|
2020-03-20 10:00:56 -06:00
|
|
|
|
if !tags.Valid() {
|
2019-08-14 10:51:42 -06:00
|
|
|
|
return nil
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
2020-04-03 20:59:59 -06:00
|
|
|
|
tagMap := event.TagMap(ev)
|
2020-03-20 10:00:56 -06:00
|
|
|
|
description := event.Msg.Get(tagMap)
|
|
|
|
|
tags = event.Filter(tags, event.Msg)
|
|
|
|
|
if description == "" {
|
|
|
|
|
err := event.Err.Get(tagMap)
|
|
|
|
|
tags = event.Filter(tags, event.Err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
description = err.Error()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return &wire.Annotation{
|
2019-07-30 15:37:57 -06:00
|
|
|
|
Description: toTruncatableString(description),
|
2019-07-17 17:17:43 -06:00
|
|
|
|
Attributes: convertAttributes(tags),
|
|
|
|
|
}
|
|
|
|
|
}
|