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

Deprecate WavefrontProxyClient [WIP] #50

Merged
merged 13 commits into from
Jul 26, 2020
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ When you use a Sender SDK, you won’t see span-level RED metrics by default unl
// Wavefront Tracing Span Data format
// <tracingSpanName> source=<source> [pointTags] <start_millis> <duration_milliseconds>
// Example:
// "getAllUsers source=localhost traceId=7b3bf470-9456-11e8-9eb6-529269fb1459
// spanId=0313bafe-9457-11e8-9eb6-529269fb1459 parent=2f64e538-9457-11e8-9eb6-529269fb1459
// "getAllUsers source=localhost traceID=7b3bf470-9456-11e8-9eb6-529269fb1459
// spanID=0313bafe-9457-11e8-9eb6-529269fb1459 parent=2f64e538-9457-11e8-9eb6-529269fb1459
laullon marked this conversation as resolved.
Show resolved Hide resolved
// application=Wavefront http.method=GET 1552949776000 343"

sender.SendSpan("getAllUsers", 1552949776000, 343, "localhost",
Expand Down
10 changes: 5 additions & 5 deletions application/heartbeater.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/wavefronthq/wavefront-sdk-go/senders"
"github.com/wavefronthq/wavefront-sdk-go/wavefront"
)

// HeartbeatService sends a heartbeat metric every 5 minutes
Expand All @@ -16,7 +16,7 @@ type HeartbeatService interface {
}

type heartbeater struct {
sender senders.Sender
client wavefront.Client
application Tags
source string
components []string
Expand All @@ -28,9 +28,9 @@ type heartbeater struct {
}

// StartHeartbeatService will create and start a new HeartbeatService
func StartHeartbeatService(sender senders.Sender, application Tags, source string, components ...string) HeartbeatService {
func StartHeartbeatService(client wavefront.Client, application Tags, source string, components ...string) HeartbeatService {
hb := &heartbeater{
sender: sender,
client: client,
application: application,
source: source,
components: components,
Expand Down Expand Up @@ -78,7 +78,7 @@ func (hb *heartbeater) beat() {
}

func (hb *heartbeater) send(tags map[string]string) {
err := hb.sender.SendMetric("~component.heartbeat", 1, 0, hb.source, tags)
err := hb.client.SendMetric("~component.heartbeat", 1, 0, hb.source, tags)
if err != nil {
log.Printf("heartbeater SendMetric error: %v\n", err)
}
Expand Down
95 changes: 95 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"fmt"
"log"
"os"
"strings"
"time"

"github.com/wavefronthq/wavefront-sdk-go/application"
"github.com/wavefronthq/wavefront-sdk-go/event"
"github.com/wavefronthq/wavefront-sdk-go/wavefront"

"github.com/wavefronthq/wavefront-sdk-go/senders"
)

func main() {
wf := wavefront.NewMultiClient()

urls := strings.Split(os.Getenv("WF_URL"), "|")
for idx, url := range urls {
client, err := wavefront.NewClient(url)
if err != nil {
panic(err)
}
client.SetSource(fmt.Sprintf("client_%d", idx))
wf.Add(client)
}

// OLD PROXY way
proxyCfg := &senders.ProxyConfiguration{
Host: "localhost",
MetricsPort: 2878,
DistributionPort: 2878,
TracingPort: 2878,
EventsPort: 2878,
FlushIntervalSeconds: 10,
}

sender, err := senders.NewProxySender(proxyCfg)
if err != nil {
panic(err)
}
sender.SetSource("client_proxy")
wf.Add(sender)

// OLD DIRECT way
directCfg := &senders.DirectConfiguration{
Server: "https://-----.wavefront.com",
Token: "--------------",
BatchSize: 10000,
MaxBufferSize: 50000,
FlushIntervalSeconds: 1,
}

sender, err = senders.NewDirectSender(directCfg)
if err != nil {
panic(err)
}
sender.SetSource("client_direct")
wf.Add(sender)

log.Print("senders ready")

source := "" //"go_sdk_example"

app := application.New("sample app", "main.go")
application.StartHeartbeatService(wf, app, source)

tags := make(map[string]string)
tags["namespace"] = "default"
tags["Kind"] = "Deployment"

options := []event.Option{event.Details("Details"), event.Type("type"), event.Severity("severity")}

for i := 0; i < 10; i++ {
wf.SendMetric("sample.metric", float64(i), time.Now().UnixNano(), source, map[string]string{"env": "test"})

txt := fmt.Sprintf("test event %d", i)
sendEvent(wf, txt, time.Now().Unix(), 0, source, tags, options...)

time.Sleep(10 * time.Second)
}

wf.Flush()
wf.Close()
}

func sendEvent(sender wavefront.Client, name string, startMillis, endMillis int64, source string, tags map[string]string, setters ...event.Option) {
err := sender.SendEvent(name, startMillis, endMillis, source, tags, setters...)
if err != nil {
log.Fatal(err)
os.Exit(-1)
}
}
18 changes: 1 addition & 17 deletions internal/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ var (
errReport = errors.New("error: invalid Format or points")
)

const (
contentType = "Content-Type"
contentEncoding = "Content-Encoding"
authzHeader = "Authorization"
bearer = "Bearer "
gzipFormat = "gzip"

octetStream = "application/octet-stream"
applicationJSON = "application/json"

reportEndpoint = "/report"
eventEndpoint = "/api/v2/event"

formatKey = "f"
)

// The implementation of a Reporter that reports points directly to a Wavefront server.
type directReporter struct {
serverURL string
Expand All @@ -45,7 +29,7 @@ func NewDirectReporter(server string, token string) Reporter {

func (reporter directReporter) Report(format string, pointLines string) (*http.Response, error) {
if format == "" || pointLines == "" {
return nil, errReport
return nil, formatError
}

// compress
Expand Down
38 changes: 17 additions & 21 deletions senders/formatter.go → internal/formatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package senders
package internal
laullon marked this conversation as resolved.
Show resolved Hide resolved

import (
"bytes"
Expand All @@ -11,11 +11,7 @@ import (

"github.com/wavefronthq/wavefront-sdk-go/event"
"github.com/wavefronthq/wavefront-sdk-go/histogram"
)

const (
deltaPrefix = "\u2206"
altDeltaPrefix = "\u0394"
"github.com/wavefronthq/wavefront-sdk-go/types"
)

var /* const */ quotation = regexp.MustCompile("\"")
Expand Down Expand Up @@ -125,9 +121,9 @@ func HistoLine(name string, centroids []histogram.Centroid, hgs map[histogram.Gr
// Gets a span line in the Wavefront span data format:
// <tracingSpanName> source=<source> [pointTags] <start_millis> <duration_milli_seconds>
// Example:
// "getAllUsers source=localhost traceId=7b3bf470-9456-11e8-9eb6-529269fb1459 spanId=0313bafe-9457-11e8-9eb6-529269fb1459
// "getAllUsers source=localhost traceID=7b3bf470-9456-11e8-9eb6-529269fb1459 spanID=0313bafe-9457-11e8-9eb6-529269fb1459
// parent=2f64e538-9457-11e8-9eb6-529269fb1459 application=Wavefront http.method=GET 1533531013 343500"
func SpanLine(name string, startMillis, durationMillis int64, source, traceId, spanId string, parents, followsFrom []string, tags []SpanTag, spanLogs []SpanLog, defaultSource string) (string, error) {
func SpanLine(name string, startMillis, durationMillis int64, source, traceID, spanID string, parents, followsFrom []string, tags []types.SpanTag, spanLogs []types.SpanLog, defaultSource string) (string, error) {
if name == "" {
return "", errors.New("empty span name")
}
Expand All @@ -136,23 +132,23 @@ func SpanLine(name string, startMillis, durationMillis int64, source, traceId, s
source = defaultSource
}

if !isUUIDFormat(traceId) {
return "", errors.New("traceId is not in UUID format")
if !isUUIDFormat(traceID) {
return "", errors.New("traceID is not in UUID format")
}
if !isUUIDFormat(spanId) {
return "", errors.New("spanId is not in UUID format")
if !isUUIDFormat(spanID) {
return "", errors.New("spanID is not in UUID format")
}

sb := GetBuffer()
defer PutBuffer(sb)

sb.WriteString(sanitizeValue(name))
sb.WriteString(" source=")
sb.WriteString(sanitizeValue(source))
sb.WriteString(" traceId=")
sb.WriteString(traceId)
sb.WriteString(" spanId=")
sb.WriteString(spanId)
sb.WriteString(strconv.Quote(sanitizeInternal(source)))
sb.WriteString(" traceID=")
laullon marked this conversation as resolved.
Show resolved Hide resolved
sb.WriteString(traceID)
sb.WriteString(" spanID=")
sb.WriteString(spanID)

for _, parent := range parents {
sb.WriteString(" parent=")
Expand Down Expand Up @@ -189,10 +185,10 @@ func SpanLine(name string, startMillis, durationMillis int64, source, traceId, s
return sb.String(), nil
}

func SpanLogJSON(traceId, spanId string, spanLogs []SpanLog) (string, error) {
l := SpanLogs{
TraceId: traceId,
SpanId: spanId,
func SpanLogJSON(traceID, spanID string, spanLogs []types.SpanLog) (string, error) {
l := types.SpanLogs{
TraceID: traceID,
SpanID: spanID,
Logs: spanLogs,
}
out, err := json.Marshal(l)
Expand Down
23 changes: 12 additions & 11 deletions senders/formatter_test.go → internal/formatter_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package senders
package internal

import (
"strconv"
"testing"

"github.com/wavefronthq/wavefront-sdk-go/histogram"
"github.com/wavefronthq/wavefront-sdk-go/types"
)

var line string
Expand Down Expand Up @@ -117,11 +118,11 @@ func BenchmarkSpanLine(b *testing.B) {
start := int64(1533531013)
dur := int64(343500)
src := "test_source"
traceId := "7b3bf470-9456-11e8-9eb6-529269fb1459"
traceID := "7b3bf470-9456-11e8-9eb6-529269fb1459"

var r string
for n := 0; n < b.N; n++ {
r, _ = SpanLine(name, start, dur, src, traceId, traceId, []string{traceId}, nil, nil, nil, "")
r, _ = SpanLine(name, start, dur, src, traceID, traceID, []string{traceID}, nil, nil, nil, "")
}
line = r
}
Expand All @@ -130,23 +131,23 @@ func TestSpanLine(t *testing.T) {
line, err := SpanLine("order.shirts", 1533531013, 343500, "test_source",
"7b3bf470-9456-11e8-9eb6-529269fb1459", "7b3bf470-9456-11e8-9eb6-529269fb1459",
[]string{"7b3bf470-9456-11e8-9eb6-529269fb1458"}, nil, nil, nil, "")
expected := "\"order.shirts\" source=\"test_source\" traceId=7b3bf470-9456-11e8-9eb6-529269fb1459" +
" spanId=7b3bf470-9456-11e8-9eb6-529269fb1459 parent=7b3bf470-9456-11e8-9eb6-529269fb1458 1533531013 343500\n"
expected := "\"order.shirts\" source=\"test_source\" traceID=7b3bf470-9456-11e8-9eb6-529269fb1459" +
" spanID=7b3bf470-9456-11e8-9eb6-529269fb1459 parent=7b3bf470-9456-11e8-9eb6-529269fb1458 1533531013 343500\n"
assertEquals(expected, line, err, t)

line, err = SpanLine("order.shirts", 1533531013, 343500, "test_source",
"7b3bf470-9456-11e8-9eb6-529269fb1459", "7b3bf470-9456-11e8-9eb6-529269fb1459", nil,
[]string{"7b3bf470-9456-11e8-9eb6-529269fb1458"}, []SpanTag{{Key: "env", Value: "test"}}, nil, "")
expected = "\"order.shirts\" source=\"test_source\" traceId=7b3bf470-9456-11e8-9eb6-529269fb1459" +
" spanId=7b3bf470-9456-11e8-9eb6-529269fb1459 followsFrom=7b3bf470-9456-11e8-9eb6-529269fb1458 \"env\"=\"test\" 1533531013 343500\n"
[]string{"7b3bf470-9456-11e8-9eb6-529269fb1458"}, []types.SpanTag{{Key: "env", Value: "test"}}, nil, "")
expected = "\"order.shirts\" source=\"test_source\" traceID=7b3bf470-9456-11e8-9eb6-529269fb1459" +
" spanID=7b3bf470-9456-11e8-9eb6-529269fb1459 followsFrom=7b3bf470-9456-11e8-9eb6-529269fb1458 \"env\"=\"test\" 1533531013 343500\n"
assertEquals(expected, line, err, t)

line, err = SpanLine("order.shirts", 1533531013, 343500, "test_source",
"7b3bf470-9456-11e8-9eb6-529269fb1459", "7b3bf470-9456-11e8-9eb6-529269fb1459", nil,
[]string{"7b3bf470-9456-11e8-9eb6-529269fb1458"},
[]SpanTag{{Key: "env", Value: "test"}, {Key: "env", Value: "dev"}}, nil, "")
expected = "\"order.shirts\" source=\"test_source\" traceId=7b3bf470-9456-11e8-9eb6-529269fb1459" +
" spanId=7b3bf470-9456-11e8-9eb6-529269fb1459 followsFrom=7b3bf470-9456-11e8-9eb6-529269fb1458 \"env\"=\"test\" \"env\"=\"dev\" 1533531013 343500\n"
[]types.SpanTag{{Key: "env", Value: "test"}, {Key: "env", Value: "dev"}}, nil, "")
expected = "\"order.shirts\" source=\"test_source\" traceID=7b3bf470-9456-11e8-9eb6-529269fb1459" +
" spanID=7b3bf470-9456-11e8-9eb6-529269fb1459 followsFrom=7b3bf470-9456-11e8-9eb6-529269fb1458 \"env\"=\"test\" \"env\"=\"dev\" 1533531013 343500\n"
assertEquals(expected, line, err, t)
}

Expand Down
24 changes: 24 additions & 0 deletions internal/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,27 @@ type ConnectionHandler interface {

Flusher
}

const (
contentType = "Content-Type"
contentEncoding = "Content-Encoding"
authzHeader = "Authorization"
bearer = "Bearer "
gzipFormat = "gzip"

octetStream = "application/octet-stream"
applicationJSON = "application/json"

reportEndpoint = "/report"
eventEndpoint = "/api/v2/event"

formatKey = "f"
)

const formatError stringError = "error: invalid Format or points"

type stringError string

func (e stringError) Error() string {
return string(e)
}
2 changes: 1 addition & 1 deletion senders/pool.go → internal/pool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package senders
package internal

import (
"bytes"
Expand Down
Loading