gtrace.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gtrace provides convenience wrapping functionality for tracing feature using OpenTelemetry.
  7. package gtrace
  8. import (
  9. "context"
  10. "github.com/gogf/gf/container/gmap"
  11. "github.com/gogf/gf/container/gvar"
  12. "github.com/gogf/gf/net/gipv4"
  13. "github.com/gogf/gf/os/gcmd"
  14. "go.opentelemetry.io/otel"
  15. "go.opentelemetry.io/otel/attribute"
  16. "go.opentelemetry.io/otel/propagation"
  17. "go.opentelemetry.io/otel/trace"
  18. "os"
  19. "strings"
  20. )
  21. const (
  22. tracingCommonKeyIpIntranet = `ip.intranet`
  23. tracingCommonKeyIpHostname = `hostname`
  24. commandEnvKeyForMaxContentLogSize = "gf.gtrace.maxcontentlogsize"
  25. commandEnvKeyForTracingInternal = "gf.gtrace.tracinginternal"
  26. )
  27. var (
  28. intranetIps, _ = gipv4.GetIntranetIpArray()
  29. intranetIpStr = strings.Join(intranetIps, ",")
  30. hostname, _ = os.Hostname()
  31. tracingInternal = true // tracingInternal enables tracing for internal type spans.
  32. tracingMaxContentLogSize = 256 * 1024 // Max log size for request and response body, especially for HTTP/RPC request.
  33. // defaultTextMapPropagator is the default propagator for context propagation between peers.
  34. defaultTextMapPropagator = propagation.NewCompositeTextMapPropagator(
  35. propagation.TraceContext{},
  36. propagation.Baggage{},
  37. )
  38. )
  39. func init() {
  40. tracingInternal = gcmd.GetOptWithEnv(commandEnvKeyForTracingInternal, true).Bool()
  41. if maxContentLogSize := gcmd.GetOptWithEnv(commandEnvKeyForMaxContentLogSize).Int(); maxContentLogSize > 0 {
  42. tracingMaxContentLogSize = maxContentLogSize
  43. }
  44. CheckSetDefaultTextMapPropagator()
  45. }
  46. // IsTracingInternal returns whether tracing spans of internal components.
  47. func IsTracingInternal() bool {
  48. return tracingInternal
  49. }
  50. // MaxContentLogSize returns the max log size for request and response body, especially for HTTP/RPC request.
  51. func MaxContentLogSize() int {
  52. return tracingMaxContentLogSize
  53. }
  54. // CommonLabels returns common used attribute labels:
  55. // ip.intranet, hostname.
  56. func CommonLabels() []attribute.KeyValue {
  57. return []attribute.KeyValue{
  58. attribute.String(tracingCommonKeyIpHostname, hostname),
  59. attribute.String(tracingCommonKeyIpIntranet, intranetIpStr),
  60. }
  61. }
  62. // IsActivated checks and returns if tracing feature is activated.
  63. func IsActivated(ctx context.Context) bool {
  64. return GetTraceId(ctx) != ""
  65. }
  66. // CheckSetDefaultTextMapPropagator sets the default TextMapPropagator if it is not set previously.
  67. func CheckSetDefaultTextMapPropagator() {
  68. p := otel.GetTextMapPropagator()
  69. if len(p.Fields()) == 0 {
  70. otel.SetTextMapPropagator(GetDefaultTextMapPropagator())
  71. }
  72. }
  73. // GetDefaultTextMapPropagator returns the default propagator for context propagation between peers.
  74. func GetDefaultTextMapPropagator() propagation.TextMapPropagator {
  75. return defaultTextMapPropagator
  76. }
  77. // GetTraceId retrieves and returns TraceId from context.
  78. // It returns an empty string is tracing feature is not activated.
  79. func GetTraceId(ctx context.Context) string {
  80. if ctx == nil {
  81. return ""
  82. }
  83. traceId := trace.SpanContextFromContext(ctx).TraceID()
  84. if traceId.IsValid() {
  85. return traceId.String()
  86. }
  87. return ""
  88. }
  89. // GetSpanId retrieves and returns SpanId from context.
  90. // It returns an empty string is tracing feature is not activated.
  91. func GetSpanId(ctx context.Context) string {
  92. if ctx == nil {
  93. return ""
  94. }
  95. spanId := trace.SpanContextFromContext(ctx).SpanID()
  96. if spanId.IsValid() {
  97. return spanId.String()
  98. }
  99. return ""
  100. }
  101. // SetBaggageValue is a convenient function for adding one key-value pair to baggage.
  102. // Note that it uses attribute.Any to set the key-value pair.
  103. func SetBaggageValue(ctx context.Context, key string, value interface{}) context.Context {
  104. return NewBaggage(ctx).SetValue(key, value)
  105. }
  106. // SetBaggageMap is a convenient function for adding map key-value pairs to baggage.
  107. // Note that it uses attribute.Any to set the key-value pair.
  108. func SetBaggageMap(ctx context.Context, data map[string]interface{}) context.Context {
  109. return NewBaggage(ctx).SetMap(data)
  110. }
  111. // GetBaggageMap retrieves and returns the baggage values as map.
  112. func GetBaggageMap(ctx context.Context) *gmap.StrAnyMap {
  113. return NewBaggage(ctx).GetMap()
  114. }
  115. // GetBaggageVar retrieves value and returns a *gvar.Var for specified key from baggage.
  116. func GetBaggageVar(ctx context.Context, key string) *gvar.Var {
  117. return NewBaggage(ctx).GetVar(key)
  118. }