log.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2016 Datadog, Inc.
  5. package tracer
  6. import (
  7. "bytes"
  8. "encoding/json"
  9. "fmt"
  10. "math"
  11. "net/http"
  12. "runtime"
  13. "time"
  14. "gopkg.in/DataDog/dd-trace-go.v1/internal/appsec"
  15. "gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
  16. "gopkg.in/DataDog/dd-trace-go.v1/internal/log"
  17. "gopkg.in/DataDog/dd-trace-go.v1/internal/osinfo"
  18. "gopkg.in/DataDog/dd-trace-go.v1/internal/version"
  19. )
  20. // startupInfo contains various information about the status of the tracer on startup.
  21. type startupInfo struct {
  22. Date string `json:"date"` // ISO 8601 date and time of start
  23. OSName string `json:"os_name"` // Windows, Darwin, Debian, etc.
  24. OSVersion string `json:"os_version"` // Version of the OS
  25. Version string `json:"version"` // Tracer version
  26. Lang string `json:"lang"` // "Go"
  27. LangVersion string `json:"lang_version"` // Go version, e.g. go1.13
  28. Env string `json:"env"` // Tracer env
  29. Service string `json:"service"` // Tracer Service
  30. AgentURL string `json:"agent_url"` // The address of the agent
  31. AgentError string `json:"agent_error"` // Any error that occurred trying to connect to agent
  32. Debug bool `json:"debug"` // Whether debug mode is enabled
  33. AnalyticsEnabled bool `json:"analytics_enabled"` // True if there is a global analytics rate set
  34. SampleRate string `json:"sample_rate"` // The default sampling rate for the rules sampler
  35. SampleRateLimit string `json:"sample_rate_limit"` // The rate limit configured with the rules sampler
  36. SamplingRules []SamplingRule `json:"sampling_rules"` // Rules used by the rules sampler
  37. SamplingRulesError string `json:"sampling_rules_error"` // Any errors that occurred while parsing sampling rules
  38. ServiceMappings map[string]string `json:"service_mappings"` // Service Mappings
  39. Tags map[string]string `json:"tags"` // Global tags
  40. RuntimeMetricsEnabled bool `json:"runtime_metrics_enabled"` // Whether or not runtime metrics are enabled
  41. HealthMetricsEnabled bool `json:"health_metrics_enabled"` // Whether or not health metrics are enabled
  42. ProfilerCodeHotspotsEnabled bool `json:"profiler_code_hotspots_enabled"` // Whether or not profiler code hotspots are enabled
  43. ProfilerEndpointsEnabled bool `json:"profiler_endpoints_enabled"` // Whether or not profiler endpoints are enabled
  44. ApplicationVersion string `json:"dd_version"` // Version of the user's application
  45. Architecture string `json:"architecture"` // Architecture of host machine
  46. GlobalService string `json:"global_service"` // Global service string. If not-nil should be same as Service. (#614)
  47. LambdaMode string `json:"lambda_mode"` // Whether or not the client has enabled lambda mode
  48. AppSec bool `json:"appsec"` // AppSec status: true when started, false otherwise.
  49. AgentFeatures agentFeatures `json:"agent_features"` // Lists the capabilities of the agent.
  50. }
  51. // checkEndpoint tries to connect to the URL specified by endpoint.
  52. // If the endpoint is not reachable, checkEndpoint returns an error
  53. // explaining why.
  54. func checkEndpoint(endpoint string) error {
  55. req, err := http.NewRequest("POST", endpoint, bytes.NewReader([]byte{0x90}))
  56. if err != nil {
  57. return fmt.Errorf("cannot create http request: %v", err)
  58. }
  59. req.Header.Set(traceCountHeader, "0")
  60. req.Header.Set("Content-Type", "application/msgpack")
  61. _, err = defaultClient.Do(req)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. // logStartup generates a startupInfo for a tracer and writes it to the log in
  68. // JSON format.
  69. func logStartup(t *tracer) {
  70. tags := make(map[string]string)
  71. for k, v := range t.config.globalTags {
  72. tags[k] = fmt.Sprintf("%v", v)
  73. }
  74. info := startupInfo{
  75. Date: time.Now().Format(time.RFC3339),
  76. OSName: osinfo.OSName(),
  77. OSVersion: osinfo.OSVersion(),
  78. Version: version.Tag,
  79. Lang: "Go",
  80. LangVersion: runtime.Version(),
  81. Env: t.config.env,
  82. Service: t.config.serviceName,
  83. AgentURL: t.config.transport.endpoint(),
  84. Debug: t.config.debug,
  85. AnalyticsEnabled: !math.IsNaN(globalconfig.AnalyticsRate()),
  86. SampleRate: fmt.Sprintf("%f", t.rulesSampling.traces.globalRate),
  87. SampleRateLimit: "disabled",
  88. SamplingRules: append(t.config.traceRules, t.config.spanRules...),
  89. ServiceMappings: t.config.serviceMappings,
  90. Tags: tags,
  91. RuntimeMetricsEnabled: t.config.runtimeMetrics,
  92. HealthMetricsEnabled: t.config.runtimeMetrics,
  93. ApplicationVersion: t.config.version,
  94. ProfilerCodeHotspotsEnabled: t.config.profilerHotspots,
  95. ProfilerEndpointsEnabled: t.config.profilerEndpoints,
  96. Architecture: runtime.GOARCH,
  97. GlobalService: globalconfig.ServiceName(),
  98. LambdaMode: fmt.Sprintf("%t", t.config.logToStdout),
  99. AgentFeatures: t.config.agent,
  100. AppSec: appsec.Enabled(),
  101. }
  102. if _, _, err := samplingRulesFromEnv(); err != nil {
  103. info.SamplingRulesError = fmt.Sprintf("%s", err)
  104. }
  105. if limit, ok := t.rulesSampling.TraceRateLimit(); ok {
  106. info.SampleRateLimit = fmt.Sprintf("%v", limit)
  107. }
  108. if !t.config.logToStdout {
  109. if err := checkEndpoint(t.config.transport.endpoint()); err != nil {
  110. info.AgentError = fmt.Sprintf("%s", err)
  111. log.Warn("DIAGNOSTICS Unable to reach agent intake: %s", err)
  112. }
  113. }
  114. bs, err := json.Marshal(info)
  115. if err != nil {
  116. log.Warn("DIAGNOSTICS Failed to serialize json for startup log (%v) %#v\n", err, info)
  117. return
  118. }
  119. log.Info("DATADOG TRACER CONFIGURATION %s\n", string(bs))
  120. }