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

feat: Add ability to set adapter scoped metadata #46

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
56 changes: 53 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,45 @@ 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 te.AdapterMeta != nil {
if meta, ok := te.AdapterMeta.(DatadogMetadata); ok {
topSpan := allSpans[0]
if topSpan.Meta == nil {
topSpan.Meta = make(map[string]string)
}
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)
}
if meta.HttpClientIp != nil {
topSpan.Meta["http.client_ip"] = *meta.HttpClientIp
}
if meta.HttpRequestContentLength != nil {
topSpan.Meta["http.request.content_length"] = fmt.Sprintf("%d", *meta.HttpRequestContentLength)
}
if meta.HttpRequestContentLengthUncompressed != nil {
topSpan.Meta["http.request.content_length_uncompressed"] = fmt.Sprintf("%d", *meta.HttpRequestContentLengthUncompressed)
}
if meta.HttpResponseContentLength != nil {
topSpan.Meta["http.response.content_length"] = fmt.Sprintf("%d", *meta.HttpResponseContentLength)
}
if meta.HttpResponseContentLengthUncompressed != nil {
topSpan.Meta["http.response.content_length_uncompressed"] = fmt.Sprintf("%d", *meta.HttpResponseContentLengthUncompressed)
}
if meta.SpanKind != nil {
topSpan.Meta["span.kind"] = meta.SpanKind.String()
}
} else {
log.Println("The Datadog adapter was expecting a DatadogMetadata object on the trace")
}
}

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

type DatadogMetadata struct {
HttpUrl *string
HttpMethod *string
HttpStatusCode *int
ResourceName *string
HttpClientIp *string
HttpRequestContentLength *int
HttpRequestContentLengthUncompressed *int
HttpResponseContentLength *int
HttpResponseContentLengthUncompressed *int
SpanKind *DatadogSpanKind
}

type DatadogSpanKind int

const (
Expand Down
18 changes: 18 additions & 0 deletions go/bin/datadog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ 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
spanKind := datadog.Server
clientIp := "66.210.227.34"

meta := datadog.DatadogMetadata{
ResourceName: &resourceName,
HttpUrl: &httpUrl,
HttpStatusCode: &httpStatusCode,
SpanKind: &spanKind,
HttpClientIp: &clientIp,
}
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
Loading