Skip to content

Commit

Permalink
feat: Add ability to set adapter scoped metadata
Browse files Browse the repository at this point in the history
Partially addresses #40
  • Loading branch information
bhelx committed Jul 20, 2023
1 parent c21807b commit defc166
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions go/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Adapter interface {
type TraceEvent struct {
Events []Event
TelemetryId TelemetryId
AdapterMeta interface{}
}

// Shared implementation for all Adapters
Expand Down
25 changes: 22 additions & 3 deletions go/adapter/datadog/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datadog
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -75,9 +76,20 @@ func (d *DatadogAdapter) HandleTraceEvent(te observe.TraceEvent) {

go func() {
output := datadog_formatter.New()
// TODO: for the moment, these are hard-coded, but will transition to a programmer-
// controlled API to customer these values.
allSpans[0].Resource = "request"

if meta, ok := te.AdapterMeta.(DatadogMetadata); ok {
topSpan := allSpans[0]
if meta.ResourceName != nil {
topSpan.Resource = *meta.ResourceName
}
if meta.HttpUrl != nil {
topSpan.Meta["http.url"] = *meta.HttpUrl
}
if meta.HttpStatusCode != nil {
topSpan.Meta["http.status_code"] = fmt.Sprintf("%d", *meta.HttpStatusCode)
}
}

tt := d.Config.TraceType.String()
allSpans[0].Type = &tt
output.AddTrace(allSpans)
Expand Down Expand Up @@ -126,6 +138,13 @@ func (d *DatadogAdapter) makeCallSpans(event observe.CallEvent, parentId *uint64
return spans
}

type DatadogMetadata struct {
HttpUrl *string
HttpMethod *string
HttpStatusCode *int
ResourceName *string
}

type DatadogSpanKind int

const (
Expand Down
1 change: 1 addition & 0 deletions go/adapter/datadog_formatter/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewSpan(service string, traceId uint64, parentId *uint64, name string, star
Duration: uint64(end.Sub(start).Nanoseconds()),
Service: service,
Resource: name,
Meta: make(map[string]string),
}
return &span
}
Expand Down
14 changes: 14 additions & 0 deletions go/bin/datadog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func main() {
}
defer m.Close(ctx)

// normally this metadata would be in your web-server framework
// or derived when you need them. we're just gonna initialize
// some example values here
resourceName := "my-resource"
httpUrl := "https://example.com/my-endpoint"
httpStatusCode := 200

meta := datadog.DatadogMetadata{
ResourceName: &resourceName,
HttpUrl: &httpUrl,
HttpStatusCode: &httpStatusCode,
}
traceCtx.Metadata(meta)

traceCtx.Finish()

time.Sleep(time.Second * 2)
Expand Down
6 changes: 6 additions & 0 deletions go/trace_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type TraceCtx struct {
Config *Config
names map[uint32]string
telemetryId TelemetryId
adapterMeta interface{}
}

// Creates a new TraceCtx. Used internally by the Adapter. The user should create the trace context from the Adapter.
Expand All @@ -59,12 +60,17 @@ func newTraceCtx(ctx context.Context, adapter *AdapterBase, r wazero.Runtime, da
return traceCtx, nil
}

func (t *TraceCtx) Metadata(metadata interface{}) {
t.adapterMeta = metadata
}

// Finish() will stop the trace and send the
// TraceEvent payload to the adapter
func (t *TraceCtx) Finish() {
traceEvent := TraceEvent{
Events: t.events,
TelemetryId: t.telemetryId,
AdapterMeta: t.adapterMeta,
}
t.adapter <- traceEvent
// clear the trace context
Expand Down

0 comments on commit defc166

Please sign in to comment.