-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into shevchenko/sampling-by-metric-tag
- Loading branch information
Showing
11 changed files
with
1,350 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
//go:generate msgp -o event_msgp.go -tests=false | ||
|
||
package stacktrace | ||
|
||
import ( | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal" | ||
|
||
"github.com/google/uuid" | ||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
var _ msgp.Marshaler = (*Event)(nil) | ||
|
||
type EventCategory string | ||
|
||
const ( | ||
// ExceptionEvent is the event type for exception events | ||
ExceptionEvent EventCategory = "exception" | ||
// VulnerabilityEvent is the event type for vulnerability events | ||
VulnerabilityEvent EventCategory = "vulnerability" | ||
// ExploitEvent is the event type for exploit events | ||
ExploitEvent EventCategory = "exploit" | ||
) | ||
|
||
// Event is the toplevel structure to contain a stacktrace and the additional information needed to correlate it with other data | ||
type Event struct { | ||
// Category is a well-known type of the event, not optional | ||
Category EventCategory `msg:"-"` | ||
// Type is a value event category specific, optional | ||
Type string `msg:"type,omitempty"` | ||
// Language is the language of the code that generated the event (set to "go" anyway here) | ||
Language string `msg:"language,omitempty"` | ||
// ID is the id of the event, optional for exceptions but mandatory for vulnerabilities and exploits to correlate with more data | ||
ID string `msg:"id,omitempty"` | ||
// Message is a generic message for the event | ||
Message string `msg:"message,omitempty"` | ||
// Frames is the stack trace of the event | ||
Frames StackTrace `msg:"frames"` | ||
} | ||
|
||
// NewEvent creates a new stacktrace event with the given category, type and message | ||
func NewEvent(eventCat EventCategory, eventType, message string) *Event { | ||
return &Event{ | ||
Category: eventCat, | ||
Type: eventType, | ||
Language: "go", | ||
Message: message, | ||
Frames: SkipAndCapture(defaultCallerSkip), | ||
} | ||
} | ||
|
||
// IDLink returns a UUID to link the stacktrace event with other data. NOT thread-safe | ||
func (e *Event) IDLink() string { | ||
if e.ID != "" { | ||
newUUID, err := uuid.NewUUID() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
e.ID = newUUID.String() | ||
} | ||
|
||
return e.ID | ||
} | ||
|
||
// AddToSpan adds the event to the given span's root span as a tag if stacktrace collection is enabled | ||
func AddToSpan(span ddtrace.Span, events ...*Event) { | ||
if !Enabled() { | ||
return | ||
} | ||
|
||
groupByCategory := map[EventCategory][]*Event{ | ||
ExceptionEvent: {}, | ||
VulnerabilityEvent: {}, | ||
ExploitEvent: {}, | ||
} | ||
|
||
for _, event := range events { | ||
groupByCategory[event.Category] = append(groupByCategory[event.Category], event) | ||
} | ||
|
||
type rooter interface { | ||
Root() ddtrace.Span | ||
} | ||
if lrs, ok := span.(rooter); ok { | ||
span = lrs.Root() | ||
} | ||
|
||
span.SetTag("_dd.stack", internal.MetaStructValue{Value: groupByCategory}) | ||
} |
Oops, something went wrong.