Skip to content

Commit

Permalink
Merge branch 'main' into rarguelloF/APPSEC-55047/refactor-trace-http
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF authored Oct 15, 2024
2 parents 5a0c9bb + 93311db commit cbcce81
Show file tree
Hide file tree
Showing 20 changed files with 1,986 additions and 574 deletions.
5 changes: 4 additions & 1 deletion ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ type config struct {

// ciVisibilityEnabled controls if the tracer is loaded with CI Visibility mode. default false
ciVisibilityEnabled bool

// logDirectory is directory for tracer logs specified by user-setting DD_TRACE_LOG_DIRECTORY. default empty/unused
logDirectory string
}

// orchestrionConfig contains Orchestrion configuration.
Expand Down Expand Up @@ -379,6 +382,7 @@ func newConfig(opts ...StartOption) *config {
c.logStartup = internal.BoolEnv("DD_TRACE_STARTUP_LOGS", true)
c.runtimeMetrics = internal.BoolVal(getDDorOtelConfig("metrics"), false)
c.debug = internal.BoolVal(getDDorOtelConfig("debugMode"), false)
c.logDirectory = os.Getenv("DD_TRACE_LOG_DIRECTORY")
c.enabled = newDynamicConfig("tracing_enabled", internal.BoolVal(getDDorOtelConfig("enabled"), true), func(b bool) bool { return true }, equal[bool])
if _, ok := os.LookupEnv("DD_TRACE_ENABLED"); ok {
c.enabled.cfgOrigin = telemetry.OriginEnvVar
Expand Down Expand Up @@ -505,7 +509,6 @@ func newConfig(opts ...StartOption) *config {
if c.debug {
log.SetLevel(log.LevelDebug)
}

// if using stdout or traces are disabled, agent is disabled
agentDisabled := c.logToStdout || !c.enabled.current
c.agent = loadAgentFeatures(agentDisabled, c.agentURL, c.httpClient)
Expand Down
1 change: 1 addition & 0 deletions ddtrace/tracer/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func startTelemetry(c *config) {
{Name: "trace_peer_service_defaults_enabled", Value: c.peerServiceDefaultsEnabled},
{Name: "orchestrion_enabled", Value: c.orchestrionCfg.Enabled},
{Name: "trace_enabled", Value: c.enabled.current, Origin: c.enabled.cfgOrigin},
{Name: "trace_log_directory", Value: c.logDirectory},
c.traceSampleRate.toTelemetry(),
c.headerAsTags.toTelemetry(),
c.globalTags.toTelemetry(),
Expand Down
18 changes: 18 additions & 0 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ type tracer struct {
// abandonedSpansDebugger specifies where and how potentially abandoned spans are stored
// when abandoned spans debugging is enabled.
abandonedSpansDebugger *abandonedSpansDebugger

// logFile contains a pointer to the file for writing tracer logs along with helper functionality for closing the file
// logFile is closed when tracer stops
// by default, tracer logs to stderr and this setting is unused
logFile *log.ManagedFile
}

const (
Expand Down Expand Up @@ -272,6 +277,14 @@ func newUnstartedTracer(opts ...StartOption) *tracer {
if c.dataStreamsMonitoringEnabled {
dataStreamsProcessor = datastreams.NewProcessor(statsd, c.env, c.serviceName, c.version, c.agentURL, c.httpClient)
}
var logFile *log.ManagedFile
if v := c.logDirectory; v != "" {
logFile, err = log.OpenFileAtPath(v)
if err != nil {
log.Warn("%v", err)
c.logDirectory = ""
}
}
t := &tracer{
config: c,
traceWriter: writer,
Expand All @@ -294,6 +307,7 @@ func newUnstartedTracer(opts ...StartOption) *tracer {
}),
statsd: statsd,
dataStreams: dataStreamsProcessor,
logFile: logFile,
}
return t
}
Expand Down Expand Up @@ -679,6 +693,10 @@ func (t *tracer) Stop() {
}
appsec.Stop()
remoteconfig.Stop()
// Close log file last to account for any logs from the above calls
if t.logFile != nil {
t.logFile.Close()
}
}

// Inject uses the configured or default TextMap Propagator.
Expand Down
21 changes: 21 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ func TestTracerStart(t *testing.T) {
})
}

func TestTracerLogFile(t *testing.T) {
t.Run("valid", func(t *testing.T) {
dir, err := os.MkdirTemp("", "example")
if err != nil {
t.Fatalf("Failure to make temp dir: %v", err)
}
t.Setenv("DD_TRACE_LOG_DIRECTORY", dir)
tracer := newTracer()
assert.Equal(t, dir, tracer.config.logDirectory)
assert.NotNil(t, tracer.logFile)
assert.Equal(t, dir+"/"+log.LoggerFile, tracer.logFile.Name())
})
t.Run("invalid", func(t *testing.T) {
t.Setenv("DD_TRACE_LOG_DIRECTORY", "some/nonexistent/path")
tracer := newTracer()
defer Stop()
assert.Empty(t, tracer.config.logDirectory)
assert.Nil(t, tracer.logFile)
})
}

func TestTracerStartSpan(t *testing.T) {
t.Run("generic", func(t *testing.T) {
tracer := newTracer()
Expand Down
9 changes: 9 additions & 0 deletions internal/civisibility/constants/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ const (
// GitTag indicates the current git tag.
// This constant is used to tag traces with the tag name associated with the current commit.
GitTag = "git.tag"

// GitHeadCommit indicates the GIT head commit hash.
GitHeadCommit = "git.commit.head_sha"

// GitPrBaseCommit indicates the GIT PR base commit hash.
GitPrBaseCommit = "git.pull_request.base_branch_sha"

// GitPrBaseBranch indicates the GIT PR base branch name.
GitPrBaseBranch = "git.pull_request.base_branch"
)
7 changes: 7 additions & 0 deletions internal/civisibility/constants/test_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ const (
// This constant is used to tag traces with the test session name
TestSessionName = "test_session.name"

// TestIsNew indicates a new test
// This constant is used to tag test events that are detected as new by early flake detection
TestIsNew = "test.is_new"

// TestIsRetry indicates a retry execution
// This constant is used to tag test events that are part of a retry execution
TestIsRetry = "test.is_retry"

// TestEarlyFlakeDetectionRetryAborted indicates a retry abort reason by the early flake detection feature
TestEarlyFlakeDetectionRetryAborted = "test.early_flake.abort_reason"
)

// Define valid test status types.
Expand Down
Loading

0 comments on commit cbcce81

Please sign in to comment.