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.
|
|
|
|
|
// This keeps the complie time dependencies to zero and allows the agent to
|
|
|
|
|
// 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"
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
"golang.org/x/tools/internal/telemetry"
|
|
|
|
|
"golang.org/x/tools/internal/telemetry/export"
|
|
|
|
|
"golang.org/x/tools/internal/telemetry/export/ocagent/wire"
|
2019-08-13 13:07:39 -06:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/tag"
|
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
|
|
|
|
|
|
|
|
|
type exporter struct {
|
2019-08-17 21:26:28 -06:00
|
|
|
|
mu sync.Mutex
|
2019-08-15 20:43:07 -06:00
|
|
|
|
config Config
|
2019-07-17 17:17:43 -06:00
|
|
|
|
node *wire.Node
|
|
|
|
|
spans []*wire.Span
|
|
|
|
|
metrics []*wire.Metric
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2019-08-15 20:43:07 -06:00
|
|
|
|
func Connect(config *Config) export.Exporter {
|
|
|
|
|
if config == nil || config.Address == "off" {
|
2019-08-14 10:51:42 -06:00
|
|
|
|
return nil
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
2019-08-15 20:43:07 -06:00
|
|
|
|
exporter := &exporter{config: *config}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
exporter.node = &wire.Node{
|
|
|
|
|
Identifier: &wire.ProcessIdentifier{
|
|
|
|
|
HostName: exporter.config.Host,
|
|
|
|
|
Pid: exporter.config.Process,
|
|
|
|
|
StartTimestamp: convertTimestamp(exporter.config.Start),
|
|
|
|
|
},
|
|
|
|
|
LibraryInfo: &wire.LibraryInfo{
|
|
|
|
|
Language: wire.LanguageGo,
|
|
|
|
|
ExporterVersion: "0.0.1",
|
|
|
|
|
CoreLibraryVersion: "x/tools",
|
|
|
|
|
},
|
|
|
|
|
ServiceInfo: &wire.ServiceInfo{
|
|
|
|
|
Name: exporter.config.Service,
|
|
|
|
|
},
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
go func() {
|
2019-08-15 20:43:07 -06:00
|
|
|
|
for _ = range time.Tick(exporter.config.Rate) {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func (e *exporter) StartSpan(ctx context.Context, span *telemetry.Span) {}
|
|
|
|
|
|
|
|
|
|
func (e *exporter) FinishSpan(ctx context.Context, span *telemetry.Span) {
|
2019-08-17 21:26:28 -06:00
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
2019-07-17 17:17:43 -06:00
|
|
|
|
e.spans = append(e.spans, convertSpan(span))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func (e *exporter) Log(context.Context, telemetry.Event) {}
|
|
|
|
|
|
|
|
|
|
func (e *exporter) Metric(ctx context.Context, data telemetry.MetricData) {
|
2019-08-17 21:26:28 -06:00
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
2019-07-17 17:17:43 -06:00
|
|
|
|
e.metrics = append(e.metrics, convertMetric(data))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 20:43:07 -06:00
|
|
|
|
func (e *exporter) Flush() {
|
2019-08-17 21:26:28 -06:00
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
2019-07-17 17:17:43 -06:00
|
|
|
|
spans := e.spans
|
|
|
|
|
e.spans = nil
|
|
|
|
|
metrics := e.metrics
|
|
|
|
|
e.metrics = nil
|
|
|
|
|
|
|
|
|
|
if len(spans) > 0 {
|
|
|
|
|
e.send("/v1/trace", &wire.ExportTraceServiceRequest{
|
|
|
|
|
Node: e.node,
|
|
|
|
|
Spans: spans,
|
|
|
|
|
//TODO: Resource?
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if len(metrics) > 0 {
|
|
|
|
|
e.send("/v1/metrics", &wire.ExportMetricsServiceRequest{
|
|
|
|
|
Node: e.node,
|
|
|
|
|
Metrics: metrics,
|
|
|
|
|
//TODO: Resource?
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *exporter) send(endpoint string, message interface{}) {
|
|
|
|
|
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
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func convertSpan(span *telemetry.Span) *wire.Span {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
result := &wire.Span{
|
2019-08-14 10:51:42 -06:00
|
|
|
|
TraceId: span.ID.TraceID[:],
|
|
|
|
|
SpanId: span.ID.SpanID[:],
|
2019-07-17 17:17:43 -06:00
|
|
|
|
TraceState: nil, //TODO?
|
|
|
|
|
ParentSpanId: span.ParentID[:],
|
|
|
|
|
Name: toTruncatableString(span.Name),
|
|
|
|
|
Kind: wire.UnspecifiedSpanKind,
|
|
|
|
|
StartTime: convertTimestamp(span.Start),
|
|
|
|
|
EndTime: convertTimestamp(span.Finish),
|
|
|
|
|
Attributes: convertAttributes(span.Tags),
|
|
|
|
|
TimeEvents: convertEvents(span.Events),
|
|
|
|
|
SameProcessAsParentSpan: true,
|
|
|
|
|
//TODO: StackTrace?
|
|
|
|
|
//TODO: Links?
|
|
|
|
|
//TODO: Status?
|
|
|
|
|
//TODO: Resource?
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func convertMetric(data telemetry.MetricData) *wire.Metric {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return nil //TODO:
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func convertAttributes(tags telemetry.TagList) *wire.Attributes {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
if len(tags) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
attributes := make(map[string]wire.Attribute)
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
|
attributes[fmt.Sprint(tag.Key)] = convertAttribute(tag.Value)
|
|
|
|
|
}
|
|
|
|
|
return &wire.Attributes{AttributeMap: attributes}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertAttribute(v interface{}) wire.Attribute {
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
|
case int8:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case int16:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case int32:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case int64:
|
|
|
|
|
return wire.IntAttribute{IntValue: v}
|
2019-08-03 17:13:38 -06:00
|
|
|
|
case int:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
2019-07-17 17:17:43 -06:00
|
|
|
|
case uint8:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case uint16:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case uint32:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case uint64:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case uint:
|
|
|
|
|
return wire.IntAttribute{IntValue: int64(v)}
|
|
|
|
|
case float32:
|
|
|
|
|
return wire.DoubleAttribute{DoubleValue: float64(v)}
|
|
|
|
|
case float64:
|
|
|
|
|
return wire.DoubleAttribute{DoubleValue: v}
|
|
|
|
|
case bool:
|
|
|
|
|
return wire.BoolAttribute{BoolValue: v}
|
|
|
|
|
case string:
|
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(v)}
|
|
|
|
|
default:
|
|
|
|
|
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprint(v))}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func convertEvents(events []telemetry.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}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func convertEvent(event telemetry.Event) wire.TimeEvent {
|
2019-07-17 17:17:43 -06:00
|
|
|
|
return wire.TimeEvent{
|
2019-08-14 10:51:42 -06:00
|
|
|
|
Time: convertTimestamp(event.At),
|
|
|
|
|
Annotation: convertAnnotation(event),
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
|
func convertAnnotation(event telemetry.Event) *wire.Annotation {
|
|
|
|
|
description := event.Message
|
|
|
|
|
if description == "" && event.Error != nil {
|
|
|
|
|
description = event.Error.Error()
|
|
|
|
|
event.Error = nil
|
2019-08-03 17:13:38 -06:00
|
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
|
tags := event.Tags
|
|
|
|
|
if event.Error != nil {
|
|
|
|
|
tags = append(tags, tag.Of("Error", event.Error))
|
2019-07-17 17:17:43 -06:00
|
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
|
if description == "" && len(tags) == 0 {
|
|
|
|
|
return nil
|
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),
|
|
|
|
|
}
|
|
|
|
|
}
|