Path: blob/main/pkg/integrations/v2/app_agent_receiver/payload.go
5363 views
package app_agent_receiver12import (3"fmt"4"sort"5"strings"6"time"78"go.opentelemetry.io/collector/pdata/pcommon"9"go.opentelemetry.io/collector/pdata/ptrace"10)1112// Payload is the body of the receiver request13type Payload struct {14Exceptions []Exception `json:"exceptions,omitempty"`15Logs []Log `json:"logs,omitempty"`16Measurements []Measurement `json:"measurements,omitempty"`17Events []Event `json:"events,omitempty"`18Meta Meta `json:"meta,omitempty"`19Traces *Traces `json:"traces,omitempty"`20}2122// Frame struct represents a single stacktrace frame23type Frame struct {24Function string `json:"function,omitempty"`25Module string `json:"module,omitempty"`26Filename string `json:"filename,omitempty"`27Lineno int `json:"lineno,omitempty"`28Colno int `json:"colno,omitempty"`29}3031// String function converts a Frame into a human readable string32func (frame Frame) String() string {33module := ""34if len(frame.Module) > 0 {35module = frame.Module + "|"36}37return fmt.Sprintf("\n at %s (%s%s:%v:%v)", frame.Function, module, frame.Filename, frame.Lineno, frame.Colno)38}3940// Stacktrace is a collection of Frames41type Stacktrace struct {42Frames []Frame `json:"frames,omitempty"`43}4445// Exception struct controls all the data regarding an exception46type Exception struct {47Type string `json:"type,omitempty"`48Value string `json:"value,omitempty"`49Stacktrace *Stacktrace `json:"stacktrace,omitempty"`50Timestamp time.Time `json:"timestamp"`51Trace TraceContext `json:"trace,omitempty"`52}5354// Message string is concatenating of the Exception.Type and Exception.Value55func (e Exception) Message() string {56return fmt.Sprintf("%s: %s", e.Type, e.Value)57}5859// String is the string representation of an Exception60func (e Exception) String() string {61var stacktrace = e.Message()62if e.Stacktrace != nil {63for _, frame := range e.Stacktrace.Frames {64stacktrace += frame.String()65}66}67return stacktrace68}6970// KeyVal representation of the exception object71func (e Exception) KeyVal() *KeyVal {72kv := NewKeyVal()73KeyValAdd(kv, "timestamp", e.Timestamp.String())74KeyValAdd(kv, "kind", "exception")75KeyValAdd(kv, "type", e.Type)76KeyValAdd(kv, "value", e.Value)77KeyValAdd(kv, "stacktrace", e.String())78MergeKeyVal(kv, e.Trace.KeyVal())79return kv80}8182// TraceContext holds trace id and span id associated to an entity (log, exception, measurement...).83type TraceContext struct {84TraceID string `json:"trace_id"`85SpanID string `json:"span_id"`86}8788// KeyVal representation of the trace context object.89func (tc TraceContext) KeyVal() *KeyVal {90retv := NewKeyVal()91KeyValAdd(retv, "traceID", tc.TraceID)92KeyValAdd(retv, "spanID", tc.SpanID)93return retv94}9596// Traces wraps the otel traces model.97type Traces struct {98ptrace.Traces99}100101// UnmarshalJSON unmarshals Traces model.102func (t *Traces) UnmarshalJSON(b []byte) error {103unmarshaler := &ptrace.JSONUnmarshaler{}104td, err := unmarshaler.UnmarshalTraces(b)105if err != nil {106return err107}108*t = Traces{td}109return nil110}111112// MarshalJSON marshals Traces model to json.113func (t Traces) MarshalJSON() ([]byte, error) {114marshaler := &ptrace.JSONMarshaler{}115return marshaler.MarshalTraces(t.Traces)116}117118// SpanSlice unpacks Traces entity into a slice of Spans.119func (t Traces) SpanSlice() []ptrace.Span {120spans := make([]ptrace.Span, 0)121rss := t.ResourceSpans()122for i := 0; i < rss.Len(); i++ {123rs := rss.At(i)124ilss := rs.ScopeSpans()125for j := 0; j < ilss.Len(); j++ {126s := ilss.At(j).Spans()127for si := 0; si < s.Len(); si++ {128spans = append(spans, s.At(si))129}130}131}132return spans133}134135// SpanToKeyVal returns KeyVal representation of a Span.136func SpanToKeyVal(s ptrace.Span) *KeyVal {137kv := NewKeyVal()138if s.StartTimestamp() > 0 {139KeyValAdd(kv, "timestamp", s.StartTimestamp().AsTime().String())140}141if s.EndTimestamp() > 0 {142KeyValAdd(kv, "end_timestamp", s.StartTimestamp().AsTime().String())143}144KeyValAdd(kv, "kind", "span")145KeyValAdd(kv, "traceID", s.TraceID().HexString())146KeyValAdd(kv, "spanID", s.SpanID().HexString())147KeyValAdd(kv, "span_kind", s.Kind().String())148KeyValAdd(kv, "name", s.Name())149KeyValAdd(kv, "parent_spanID", s.ParentSpanID().HexString())150s.Attributes().Range(func(k string, v pcommon.Value) bool {151KeyValAdd(kv, "attr_"+k, fmt.Sprintf("%v", v))152return true153})154155return kv156}157158// LogLevel is log level enum for incoming app logs159type LogLevel string160161const (162// LogLevelTrace is "trace"163LogLevelTrace LogLevel = "trace"164// LogLevelDebug is "debug"165LogLevelDebug LogLevel = "debug"166// LogLevelInfo is "info"167LogLevelInfo LogLevel = "info"168// LogLevelWarning is "warning"169LogLevelWarning LogLevel = "warning"170// LogLevelError is "error"171LogLevelError LogLevel = "error"172)173174// LogContext is a string to string map structure that175// represents the context of a log message176type LogContext map[string]string177178// Log struct controls the data that come into a Log message179type Log struct {180Message string `json:"message,omitempty"`181LogLevel LogLevel `json:"level,omitempty"`182Context LogContext `json:"context,omitempty"`183Timestamp time.Time `json:"timestamp"`184Trace TraceContext `json:"trace,omitempty"`185}186187// KeyVal representation of a Log object188func (l Log) KeyVal() *KeyVal {189kv := NewKeyVal()190KeyValAdd(kv, "timestamp", l.Timestamp.String())191KeyValAdd(kv, "kind", "log")192KeyValAdd(kv, "message", l.Message)193KeyValAdd(kv, "level", string(l.LogLevel))194MergeKeyValWithPrefix(kv, KeyValFromMap(l.Context), "context_")195MergeKeyVal(kv, l.Trace.KeyVal())196return kv197}198199// Measurement holds the data for user provided measurements200type Measurement struct {201Values map[string]float64 `json:"values,omitempty"`202Timestamp time.Time `json:"timestamp,omitempty"`203Trace TraceContext `json:"trace,omitempty"`204}205206// KeyVal representation of the exception object207func (m Measurement) KeyVal() *KeyVal {208kv := NewKeyVal()209210KeyValAdd(kv, "timestamp", m.Timestamp.String())211KeyValAdd(kv, "kind", "measurement")212213keys := make([]string, 0, len(m.Values))214for k := range m.Values {215keys = append(keys, k)216}217sort.Strings(keys)218for _, k := range keys {219KeyValAdd(kv, k, fmt.Sprintf("%f", m.Values[k]))220}221MergeKeyVal(kv, m.Trace.KeyVal())222return kv223}224225// SDK holds metadata about the app agent that produced the event226type SDK struct {227Name string `json:"name,omitempty"`228Version string `json:"version,omitempty"`229Integrations []SDKIntegration `json:"integrations,omitempty"`230}231232// KeyVal produces key->value representation of Sdk metadata233func (sdk SDK) KeyVal() *KeyVal {234kv := NewKeyVal()235KeyValAdd(kv, "name", sdk.Name)236KeyValAdd(kv, "version", sdk.Version)237238if len(sdk.Integrations) > 0 {239integrations := make([]string, len(sdk.Integrations))240241for i, integration := range sdk.Integrations {242integrations[i] = integration.String()243}244245KeyValAdd(kv, "integrations", strings.Join(integrations, ","))246}247248return kv249}250251// SDKIntegration holds metadata about a plugin/integration on the app agent that collected and sent the event252type SDKIntegration struct {253Name string `json:"name,omitempty"`254Version string `json:"version,omitempty"`255}256257func (i SDKIntegration) String() string {258return fmt.Sprintf("%s:%s", i.Name, i.Version)259}260261// User holds metadata about the user related to an app event262type User struct {263Email string `json:"email,omitempty"`264ID string `json:"id,omitempty"`265Username string `json:"username,omitempty"`266Attributes map[string]string `json:"attributes,omitempty"`267}268269// KeyVal produces a key->value representation User metadata270func (u User) KeyVal() *KeyVal {271kv := NewKeyVal()272KeyValAdd(kv, "email", u.Email)273KeyValAdd(kv, "id", u.ID)274KeyValAdd(kv, "username", u.Username)275MergeKeyValWithPrefix(kv, KeyValFromMap(u.Attributes), "attr_")276return kv277}278279// Meta holds metadata about an app event280type Meta struct {281SDK SDK `json:"sdk,omitempty"`282App App `json:"app,omitempty"`283User User `json:"user,omitempty"`284Session Session `json:"session,omitempty"`285Page Page `json:"page,omitempty"`286Browser Browser `json:"browser,omitempty"`287View View `json:"view,omitempty"`288}289290// KeyVal produces key->value representation of the app event metadata291func (m Meta) KeyVal() *KeyVal {292kv := NewKeyVal()293MergeKeyValWithPrefix(kv, m.SDK.KeyVal(), "sdk_")294MergeKeyValWithPrefix(kv, m.App.KeyVal(), "app_")295MergeKeyValWithPrefix(kv, m.User.KeyVal(), "user_")296MergeKeyValWithPrefix(kv, m.Session.KeyVal(), "session_")297MergeKeyValWithPrefix(kv, m.Page.KeyVal(), "page_")298MergeKeyValWithPrefix(kv, m.Browser.KeyVal(), "browser_")299MergeKeyValWithPrefix(kv, m.View.KeyVal(), "view_")300return kv301}302303// Session holds metadata about the browser session the event originates from304type Session struct {305ID string `json:"id,omitempty"`306Attributes map[string]string `json:"attributes,omitempty"`307}308309// KeyVal produces key->value representation of the Session metadata310func (s Session) KeyVal() *KeyVal {311kv := NewKeyVal()312KeyValAdd(kv, "id", s.ID)313MergeKeyValWithPrefix(kv, KeyValFromMap(s.Attributes), "attr_")314return kv315}316317// Page holds metadata about the web page event originates from318type Page struct {319ID string `json:"id,omitempty"`320URL string `json:"url,omitempty"`321Attributes map[string]string `json:"attributes,omitempty"`322}323324// KeyVal produces key->val representation of Page metadata325func (p Page) KeyVal() *KeyVal {326kv := NewKeyVal()327KeyValAdd(kv, "id", p.ID)328KeyValAdd(kv, "url", p.URL)329MergeKeyValWithPrefix(kv, KeyValFromMap(p.Attributes), "attr_")330return kv331}332333// App holds metadata about the application event originates from334type App struct {335Name string `json:"name,omitempty"`336Release string `json:"release,omitempty"`337Version string `json:"version,omitempty"`338Environment string `json:"environment,omitempty"`339}340341// Event holds RUM event data342type Event struct {343Name string `json:"name"`344Domain string `json:"domain,omitempty"`345Attributes map[string]string `json:"attributes,omitempty"`346Timestamp time.Time `json:"timestamp,omitempty"`347Trace TraceContext `json:"trace,omitempty"`348}349350// KeyVal produces key -> value representation of Event metadata351func (e Event) KeyVal() *KeyVal {352kv := NewKeyVal()353KeyValAdd(kv, "timestamp", e.Timestamp.String())354KeyValAdd(kv, "kind", "event")355KeyValAdd(kv, "event_name", e.Name)356KeyValAdd(kv, "event_domain", e.Domain)357if e.Attributes != nil {358MergeKeyValWithPrefix(kv, KeyValFromMap(e.Attributes), "event_data_")359}360MergeKeyVal(kv, e.Trace.KeyVal())361return kv362}363364// KeyVal produces key-> value representation of App metadata365func (a App) KeyVal() *KeyVal {366kv := NewKeyVal()367KeyValAdd(kv, "name", a.Name)368KeyValAdd(kv, "release", a.Release)369KeyValAdd(kv, "version", a.Version)370KeyValAdd(kv, "environment", a.Environment)371return kv372}373374// Browser holds metadata about a client's browser375type Browser struct {376Name string `json:"name,omitempty"`377Version string `json:"version,omitempty"`378OS string `json:"os,omitempty"`379Mobile bool `json:"mobile,omitempty"`380}381382// KeyVal produces key->value representation of the Browser metadata383func (b Browser) KeyVal() *KeyVal {384kv := NewKeyVal()385KeyValAdd(kv, "name", b.Name)386KeyValAdd(kv, "version", b.Version)387KeyValAdd(kv, "os", b.OS)388KeyValAdd(kv, "mobile", fmt.Sprintf("%v", b.Mobile))389return kv390}391392// View holds metadata about a view393type View struct {394Name string `json:"name,omitempty"`395}396397func (v View) KeyVal() *KeyVal {398kv := NewKeyVal()399KeyValAdd(kv, "name", v.Name)400return kv401}402403404