Skip to content

Commit

Permalink
[NH-79350] Refactors to remove most global state (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
swi-jared authored May 13, 2024
1 parent 37e9560 commit 1ba4531
Show file tree
Hide file tree
Showing 41 changed files with 2,037 additions and 2,263 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ removecert:
test: certgen runtest removecert
testfast: certgen runtestfast removecert

examples:
@cd examples && go test -race -timeout 1m -short ./... && echo "All examples passed."

vet:
@go vet -composites=false ./... && echo "Go vet analysis passed."

clean:
@go clean -testcache

sure: clean test examples vet
lint:
golangci-lint run ./...

license-check:
./license_check.sh

sure: clean test vet lint license-check

.PHONY: certgen test removecert examples vet clean
.PHONY: certgen test removecert vet clean lint license-check
13 changes: 4 additions & 9 deletions instrumentation/net/http/swohttp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package swohttp

import (
"github.com/solarwinds/apm-go/internal/reporter"
"github.com/solarwinds/apm-go/swo"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand All @@ -32,12 +31,10 @@ const (
XTraceOptionsResponse = "X-Trace-Options-Response"
)

var xtraceRegexp = regexp.MustCompile(`\A00-[[:xdigit:]]{32}-[[:xdigit:]]{16}-01\z`)
// TODO future: we should figure out a way to mock oboe so we can test for sampled == 01
var xtraceRegexp = regexp.MustCompile(`\A00-[[:xdigit:]]{32}-[[:xdigit:]]{16}-00\z`)

func TestHandlerNoXOptsResponse(t *testing.T) {
r := reporter.SetTestReporter(reporter.TestReporterSettingType(reporter.DefaultST))
defer r.Close(0)

cb, err := swo.Start()
require.NoError(t, err)
defer cb()
Expand All @@ -49,9 +46,6 @@ func TestHandlerNoXOptsResponse(t *testing.T) {
}

func TestHandlerWithXOptsResponse(t *testing.T) {
r := reporter.SetTestReporter(reporter.TestReporterSettingType(reporter.DefaultST))
defer r.Close(0)

cb, err := swo.Start()
require.NoError(t, err)
defer cb()
Expand All @@ -61,7 +55,8 @@ func TestHandlerWithXOptsResponse(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, XTrace+","+XTraceOptionsResponse, resp.Header.Get(ACEHdr))
require.Regexp(t, xtraceRegexp, resp.Header.Get(XTrace))
require.Regexp(t, "trigger-trace=ok", resp.Header.Get(XTraceOptionsResponse))
// TODO: it'd be nice to have this actually receive settings from oboe and test for `trigger-trace=ok`
require.Regexp(t, "trigger-trace=settings-not-available", resp.Header.Get(XTraceOptionsResponse))
}

func doRequest(t *testing.T, xtOpts string) *http.Response {
Expand Down
14 changes: 14 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ const (
InfoLabel = "info"
UnknownLabel = "UNKNOWN"
)

const (
KvSignatureKey = "SignatureKey"
KvBucketCapacity = "BucketCapacity"
KvBucketRate = "BucketRate"
KvTriggerTraceRelaxedBucketCapacity = "TriggerRelaxedBucketCapacity"
KvTriggerTraceRelaxedBucketRate = "TriggerRelaxedBucketRate"
KvTriggerTraceStrictBucketCapacity = "TriggerStrictBucketCapacity"
KvTriggerTraceStrictBucketRate = "TriggerStrictBucketRate"
KvMetricsFlushInterval = "MetricsFlushInterval"
KvEventsFlushInterval = "EventsFlushInterval"
KvMaxTransactions = "MaxTransactions"
KvMaxCustomMetrics = "MaxCustomMetrics"
)
21 changes: 12 additions & 9 deletions internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
)

type exporter struct {
r reporter.Reporter
}

func exportSpan(_ context.Context, s sdktrace.ReadOnlySpan) {
func (e *exporter) exportSpan(_ context.Context, s sdktrace.ReadOnlySpan) {
evt := reporter.CreateEntryEvent(s.SpanContext(), s.StartTime(), s.Parent())
layer := fmt.Sprintf("%s:%s", strings.ToUpper(s.SpanKind().String()), s.Name())
evt.SetLayer(layer)
Expand Down Expand Up @@ -68,7 +69,7 @@ func exportSpan(_ context.Context, s sdktrace.ReadOnlySpan) {

evt.AddKVs(s.Attributes())

if err := reporter.ReportEvent(evt); err != nil {
if err := e.r.ReportEvent(evt); err != nil {
log.Warning("cannot send entry event", err)
return
}
Expand All @@ -88,33 +89,35 @@ func exportSpan(_ context.Context, s sdktrace.ReadOnlySpan) {
}
}
evt.AddKVs(otEvt.Attributes)
if err := reporter.ReportEvent(evt); err != nil {
if err := e.r.ReportEvent(evt); err != nil {
log.Warningf("could not send %s event: %s", s.Name(), err)
continue
}
}

evt = reporter.CreateExitEvent(s.SpanContext(), s.EndTime())
evt.AddKV(attribute.String(constants.Layer, layer))
if err := reporter.ReportEvent(evt); err != nil {
if err := e.r.ReportEvent(evt); err != nil {
log.Warning("cannot send exit event", err)
return
}

}

func (e *exporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
reporter.WaitForReady(ctx)
e.r.WaitForReady(ctx)
for _, s := range spans {
exportSpan(ctx, s)
e.exportSpan(ctx, s)
}
return nil
}

func (e *exporter) Shutdown(ctx context.Context) error {
return reporter.Shutdown(ctx)
return e.r.Shutdown(ctx)
}

func NewExporter() sdktrace.SpanExporter {
return &exporter{}
func NewExporter(r reporter.Reporter) sdktrace.SpanExporter {
return &exporter{
r: r,
}
}
9 changes: 3 additions & 6 deletions internal/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import (

func TestExportSpan(t *testing.T) {
r := &capturingReporter{}
defer reporter.SetGlobalReporter(r)()
tr, cb := testutils.TracerWithExporter(NewExporter())
tr, cb := testutils.TracerWithExporter(NewExporter(r))
defer cb()

ctx := context.Background()
Expand Down Expand Up @@ -156,8 +155,7 @@ func TestExportSpan(t *testing.T) {

func TestExportSpanBacktrace(t *testing.T) {
r := &capturingReporter{}
defer reporter.SetGlobalReporter(r)()
tr, cb := testutils.TracerWithExporter(NewExporter())
tr, cb := testutils.TracerWithExporter(NewExporter(r))
defer cb()

ctx := context.Background()
Expand Down Expand Up @@ -199,8 +197,7 @@ func getBsonFromEvent(t *testing.T, event reporter.Event) map[string]interface{}

func TestExportSpanStatusCodes(t *testing.T) {
r := &capturingReporter{}
defer reporter.SetGlobalReporter(r)()
tr, cb := testutils.TracerWithExporter(NewExporter())
tr, cb := testutils.TracerWithExporter(NewExporter(r))
defer cb()

permutations := []struct {
Expand Down
Loading

0 comments on commit 1ba4531

Please sign in to comment.