transaction.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package newrelic
  2. import (
  3. "net/http"
  4. "net/url"
  5. )
  6. // Transaction represents a request or a background task.
  7. // Each Transaction should only be used in a single goroutine.
  8. type Transaction interface {
  9. // If StartTransaction is called with a non-nil http.ResponseWriter then
  10. // the Transaction may be used in its place. This allows
  11. // instrumentation of the response code and response headers.
  12. http.ResponseWriter
  13. // End finishes the current transaction, stopping all further
  14. // instrumentation. Subsequent calls to End will have no effect.
  15. End() error
  16. // Ignore ensures that this transaction's data will not be recorded.
  17. Ignore() error
  18. // SetName names the transaction. Transactions will not be grouped
  19. // usefully if too many unique names are used.
  20. SetName(name string) error
  21. // NoticeError records an error. The first five errors per transaction
  22. // are recorded (this behavior is subject to potential change in the
  23. // future).
  24. NoticeError(err error) error
  25. // AddAttribute adds a key value pair to the current transaction. This
  26. // information is attached to errors, transaction events, and error
  27. // events. The key must contain fewer than than 255 bytes. The value
  28. // must be a number, string, or boolean. Attribute configuration is
  29. // applied (see config.go).
  30. //
  31. // For more information, see:
  32. // https://docs.newrelic.com/docs/agents/manage-apm-agents/agent-metrics/collect-custom-attributes
  33. AddAttribute(key string, value interface{}) error
  34. // SetWebRequest marks the transaction as a web transaction. If
  35. // WebRequest is non-nil, SetWebRequest will additionally collect
  36. // details on request attributes, url, and method. If headers are
  37. // present, the agent will look for a distributed tracing header. Use
  38. // NewWebRequest to transform a *http.Request into a WebRequest.
  39. SetWebRequest(WebRequest) error
  40. // StartSegmentNow allows the timing of functions, external calls, and
  41. // datastore calls. The segments of each transaction MUST be used in a
  42. // single goroutine. Consumers are encouraged to use the
  43. // `StartSegmentNow` functions which checks if the Transaction is nil.
  44. // See segments.go
  45. StartSegmentNow() SegmentStartTime
  46. // CreateDistributedTracePayload creates a payload to link the calls
  47. // between transactions. This method never returns nil. Instead, it may
  48. // return a shim implementation whose methods return empty strings.
  49. // CreateDistributedTracePayload should be called every time an outbound
  50. // call is made since the payload contains a timestamp.
  51. //
  52. // StartExternalSegment calls CreateDistributedTracePayload, so you
  53. // should not need to use this method for typical outbound HTTP calls.
  54. // Just use StartExternalSegment!
  55. CreateDistributedTracePayload() DistributedTracePayload
  56. // AcceptDistributedTracePayload is used at the beginning of a
  57. // transaction to identify the caller.
  58. //
  59. // Application.StartTransaction calls this method automatically if a
  60. // payload is present in the request headers (under the key
  61. // DistributedTracePayloadHeader). Therefore, this method does not need
  62. // to be used for typical HTTP transactions.
  63. //
  64. // AcceptDistributedTracePayload should be used as early in the
  65. // transaction as possible. It may not be called after a call to
  66. // CreateDistributedTracePayload.
  67. //
  68. // The payload parameter may be a DistributedTracePayload or a string.
  69. AcceptDistributedTracePayload(t TransportType, payload interface{}) error
  70. }
  71. // DistributedTracePayload is used to instrument connections between
  72. // transactions and applications.
  73. type DistributedTracePayload interface {
  74. // HTTPSafe serializes the payload into a string containing http safe
  75. // characters.
  76. HTTPSafe() string
  77. // Text serializes the payload into a string. The format is slightly
  78. // more compact than HTTPSafe.
  79. Text() string
  80. }
  81. const (
  82. // DistributedTracePayloadHeader is the header used by New Relic agents
  83. // for automatic trace payload instrumentation.
  84. DistributedTracePayloadHeader = "Newrelic"
  85. )
  86. // TransportType represents the type of connection that the trace payload was
  87. // transported over.
  88. type TransportType struct{ name string }
  89. // TransportType names used across New Relic agents:
  90. var (
  91. TransportUnknown = TransportType{name: "Unknown"}
  92. TransportHTTP = TransportType{name: "HTTP"}
  93. TransportHTTPS = TransportType{name: "HTTPS"}
  94. TransportKafka = TransportType{name: "Kafka"}
  95. TransportJMS = TransportType{name: "JMS"}
  96. TransportIronMQ = TransportType{name: "IronMQ"}
  97. TransportAMQP = TransportType{name: "AMQP"}
  98. TransportQueue = TransportType{name: "Queue"}
  99. TransportOther = TransportType{name: "Other"}
  100. )
  101. // WebRequest may be implemented to provide request information to
  102. // Transaction.SetWebRequest.
  103. type WebRequest interface {
  104. // Header may return nil if you don't have any headers or don't want to
  105. // transform them to http.Header format.
  106. Header() http.Header
  107. // URL may return nil if you don't have a URL or don't want to transform
  108. // it to *url.URL.
  109. URL() *url.URL
  110. Method() string
  111. // If a distributed tracing header is found in the headers returned by
  112. // Header(), this TransportType will be used in the distributed tracing
  113. // metrics.
  114. Transport() TransportType
  115. }
  116. // NewWebRequest turns a *http.Request into a WebRequest for input into
  117. // Transaction.SetWebRequest.
  118. func NewWebRequest(request *http.Request) WebRequest {
  119. if nil == request {
  120. return nil
  121. }
  122. return requestWrap{request: request}
  123. }