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-04-17 07:32:56 -06:00
|
|
|
|
"golang.org/x/tools/internal/event/core"
|
|
|
|
|
"golang.org/x/tools/internal/event/export"
|
|
|
|
|
"golang.org/x/tools/internal/event/export/metric"
|
|
|
|
|
"golang.org/x/tools/internal/event/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-04-17 07:32:56 -06:00
|
|
|
|
func (e *Exporter) ProcessEvent(ctx context.Context, ev core.Event, tagMap core.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),
|
2020-04-17 07:32:56 -06:00
|
|
|
|
Attributes: convertAttributes(core.Filter(span.Start(), core.Name)),
|
2020-03-27 16:59:18 -06:00
|
|
|
|
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-04-17 07:32:56 -06:00
|
|
|
|
func skipToValidTag(l core.TagList) (int, core.Tag) {
|
2020-04-03 21:06:57 -06:00
|
|
|
|
// skip to the first valid tag
|
|
|
|
|
for index := 0; l.Valid(index); index++ {
|
|
|
|
|
if tag := l.Tag(index); tag.Valid() {
|
|
|
|
|
return index, tag
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
return -1, core.Tag{}
|
2020-04-03 21:06:57 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
|
func convertAttributes(l core.TagList) *wire.Attributes {
|
2020-04-03 21:06:57 -06:00
|
|
|
|
index, tag := skipToValidTag(l)
|
|
|
|
|
if !tag.Valid() {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
attributes := make(map[string]wire.Attribute)
|
2020-04-03 21:06:57 -06:00
|
|
|
|
for {
|
|
|
|
|
if tag.Valid() {
|
2020-04-14 09:32:24 -06:00
|
|
|
|
attributes[tag.Key().Name()] = convertAttribute(tag)
|
2020-04-03 21:06:57 -06:00
|
|
|
|
}
|
|
|
|
|
index++
|
|
|
|
|
if !l.Valid(index) {
|
|
|
|
|
return &wire.Attributes{AttributeMap: attributes}
|
|
|
|
|
}
|
|
|
|
|
tag = l.Tag(index)
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
|
func convertAttribute(tag core.Tag) wire.Attribute {
|
2020-04-14 09:32:24 -06:00
|
|
|
|
switch key := tag.Key().(type) {
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.IntKey:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.Int8Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.Int16Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.Int32Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.Int64Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.UIntKey:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.UInt8Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.UInt16Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.UInt32Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.UInt64Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.IntAttribute{IntValue: int64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.Float32Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.DoubleAttribute{DoubleValue: float64(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.Float64Key:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.DoubleAttribute{DoubleValue: key.From(tag)}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.BooleanKey:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.BoolAttribute{BoolValue: key.From(tag)}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.StringKey:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(key.From(tag))}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.ErrorKey:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(key.From(tag).Error())}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
case *core.ValueKey:
|
2020-03-24 21:03:59 -06:00
|
|
|
|
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-04-17 07:32:56 -06:00
|
|
|
|
func convertEvents(events []core.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-04-17 07:32:56 -06:00
|
|
|
|
func convertEvent(ev core.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-04-17 07:32:56 -06:00
|
|
|
|
func convertAnnotation(ev core.Event) *wire.Annotation {
|
2020-04-03 21:06:57 -06:00
|
|
|
|
if _, tag := skipToValidTag(ev); !tag.Valid() {
|
2019-08-14 10:51:42 -06:00
|
|
|
|
return nil
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
2020-04-17 07:32:56 -06:00
|
|
|
|
tagMap := core.TagMap(ev)
|
|
|
|
|
description := core.Msg.Get(tagMap)
|
|
|
|
|
tags := core.Filter(ev, core.Msg)
|
2020-03-20 10:00:56 -06:00
|
|
|
|
if description == "" {
|
2020-04-17 07:32:56 -06:00
|
|
|
|
err := core.Err.Get(tagMap)
|
|
|
|
|
tags = core.Filter(tags, core.Err)
|
2020-03-20 10:00:56 -06:00
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
}
|