Skip to content

Commit

Permalink
Add container logs scraping from file by default (#88)
Browse files Browse the repository at this point in the history
Align with the Helm chart. We are mounting pod logs host path and container logs host path.
  • Loading branch information
vboulineau committed May 12, 2020
1 parent 302f86c commit 8b1b1fe
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ SOURCES := $(shell find $(SOURCEDIR) ! -name "*_test.go" -name '*.go')
BUILDINFOPKG=github.com/DataDog/${PROJECT_NAME}/version
GIT_TAG?=$(shell git tag -l --contains HEAD | tail -1)
LAST_TAG=$(or $(shell git tag | tail -1),v0.0.0)
TAG?=${GIT_TAG}
TAG_HASH=${LAST_TAG}_$(shell git rev-parse --short HEAD)
VERSION?=$(if $(GIT_TAG),$(GIT_TAG),$(TAG_HASH))
TAG?=${VERSION}
GIT_COMMIT?=$(shell git rev-parse HEAD)
DATE=$(shell date +%Y-%m-%d/%H:%M:%S )
GOMOD?="-mod=vendor"
Expand Down
6 changes: 6 additions & 0 deletions deploy/crds/datadoghq.com_datadogagents_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,12 @@ spec:
log:
description: Log Agent configuration
properties:
containerCollectUsingFiles:
description: 'Collect logs from files in /var/log/pods instead
of using container runtime API. It''s usually the most efficient
way of collecting logs. ref: https://docs.datadoghq.com/agent/basic_agent_usage/kubernetes/#log-collection-setup
Default: true'
type: boolean
containerLogsPath:
description: 'This to allow log collection from container log
path. Set to a different path if not using docker runtime.
Expand Down
67 changes: 34 additions & 33 deletions pkg/apis/datadoghq/v1alpha1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,40 @@ const (

// Datadog env var names
const (
DDAPIKey = "DD_API_KEY"
DDClusterName = "DD_CLUSTER_NAME"
DDSite = "DD_SITE"
DDddURL = "DD_DD_URL"
DDHealthPort = "DD_HEALTH_PORT"
DDLogLevel = "DD_LOG_LEVEL"
DDPodLabelsAsTags = "DD_KUBERNETES_POD_LABELS_AS_TAGS"
DDPodAnnotationsAsTags = "DD_KUBERNETES_POD_ANNOTATIONS_AS_TAGS"
DDTags = "DD_TAGS"
DDCollectKubeEvents = "DD_COLLECT_KUBERNETES_EVENTS"
DDLeaderElection = "DD_LEADER_ELECTION"
DDLogsEnabled = "DD_LOGS_ENABLED"
DDLogsConfigContainerCollectAll = "DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL"
DDDogstatsdOriginDetection = "DD_DOGSTATSD_ORIGIN_DETECTION"
DDClusterAgentEnabled = "DD_CLUSTER_AGENT_ENABLED"
DDClusterAgentKubeServiceName = "DD_CLUSTER_AGENT_KUBERNETES_SERVICE_NAME"
DDClusterAgentAuthToken = "DD_CLUSTER_AGENT_AUTH_TOKEN"
DDMetricsProviderEnabled = "DD_EXTERNAL_METRICS_PROVIDER_ENABLED"
DDMetricsProviderPort = "DD_EXTERNAL_METRICS_PROVIDER_PORT"
DDAppKey = "DD_APP_KEY"
DDClusterChecksEnabled = "DD_CLUSTER_CHECKS_ENABLED"
DDClcRunnerEnabled = "DD_CLC_RUNNER_ENABLED"
DDClcRunnerHost = "DD_CLC_RUNNER_HOST"
DDExtraConfigProviders = "DD_EXTRA_CONFIG_PROVIDERS"
DDExtraListeners = "DD_EXTRA_LISTENERS"
DDHostname = "DD_HOSTNAME"
DDAPMEnabled = "DD_APM_ENABLED"
DDProcessAgentEnabled = "DD_PROCESS_AGENT_ENABLED"
DDSystemProbeAgentEnabled = "DD_SYSTEM_PROBE_ENABLED"
DDEnableMetadataCollection = "DD_ENABLE_METADATA_COLLECTION"
DDKubeletHost = "DD_KUBERNETES_KUBELET_HOST"
DDCriSocketPath = "DD_CRI_SOCKET_PATH"
DockerHost = "DOCKER_HOST"
DDAPIKey = "DD_API_KEY"
DDClusterName = "DD_CLUSTER_NAME"
DDSite = "DD_SITE"
DDddURL = "DD_DD_URL"
DDHealthPort = "DD_HEALTH_PORT"
DDLogLevel = "DD_LOG_LEVEL"
DDPodLabelsAsTags = "DD_KUBERNETES_POD_LABELS_AS_TAGS"
DDPodAnnotationsAsTags = "DD_KUBERNETES_POD_ANNOTATIONS_AS_TAGS"
DDTags = "DD_TAGS"
DDCollectKubeEvents = "DD_COLLECT_KUBERNETES_EVENTS"
DDLeaderElection = "DD_LEADER_ELECTION"
DDLogsEnabled = "DD_LOGS_ENABLED"
DDLogsConfigContainerCollectAll = "DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL"
DDLogsContainerCollectUsingFiles = "DD_LOGS_CONFIG_K8S_CONTAINER_USE_FILE"
DDDogstatsdOriginDetection = "DD_DOGSTATSD_ORIGIN_DETECTION"
DDClusterAgentEnabled = "DD_CLUSTER_AGENT_ENABLED"
DDClusterAgentKubeServiceName = "DD_CLUSTER_AGENT_KUBERNETES_SERVICE_NAME"
DDClusterAgentAuthToken = "DD_CLUSTER_AGENT_AUTH_TOKEN"
DDMetricsProviderEnabled = "DD_EXTERNAL_METRICS_PROVIDER_ENABLED"
DDMetricsProviderPort = "DD_EXTERNAL_METRICS_PROVIDER_PORT"
DDAppKey = "DD_APP_KEY"
DDClusterChecksEnabled = "DD_CLUSTER_CHECKS_ENABLED"
DDClcRunnerEnabled = "DD_CLC_RUNNER_ENABLED"
DDClcRunnerHost = "DD_CLC_RUNNER_HOST"
DDExtraConfigProviders = "DD_EXTRA_CONFIG_PROVIDERS"
DDExtraListeners = "DD_EXTRA_LISTENERS"
DDHostname = "DD_HOSTNAME"
DDAPMEnabled = "DD_APM_ENABLED"
DDProcessAgentEnabled = "DD_PROCESS_AGENT_ENABLED"
DDSystemProbeAgentEnabled = "DD_SYSTEM_PROBE_ENABLED"
DDEnableMetadataCollection = "DD_ENABLE_METADATA_COLLECTION"
DDKubeletHost = "DD_KUBERNETES_KUBELET_HOST"
DDCriSocketPath = "DD_CRI_SOCKET_PATH"
DockerHost = "DOCKER_HOST"

// KubernetesEnvvarName Env var used by the Datadog Agent container entrypoint
// to add kubelet config provider and listener
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/datadoghq/v1alpha1/datadogagent_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
defaultApmEnabled bool = false
defaultLogEnabled bool = false
defaultLogsConfigContainerCollectAll bool = false
defaultLogsContainerCollectUsingFiles bool = true
defaultContainerLogsPath string = "/var/lib/docker/containers"
defaultPodLogsPath string = "/var/log/pods"
defaultLogsTempStoragePath string = "/var/lib/datadog-agent/logs"
Expand Down Expand Up @@ -561,6 +562,10 @@ func DefaultDatadogAgentSpecAgentLog(log *LogSpec) *LogSpec {
log.LogsConfigContainerCollectAll = NewBoolPointer(defaultLogsConfigContainerCollectAll)
}

if log.ContainerCollectUsingFiles == nil {
log.ContainerCollectUsingFiles = NewBoolPointer(defaultLogsContainerCollectUsingFiles)
}

if log.ContainerLogsPath == nil {
log.ContainerLogsPath = NewStringPointer(defaultContainerLogsPath)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/datadoghq/v1alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ type LogSpec struct {
// +optional
LogsConfigContainerCollectAll *bool `json:"logsConfigContainerCollectAll,omitempty"`

// Collect logs from files in /var/log/pods instead of using container runtime API.
// It's usually the most efficient way of collecting logs.
// ref: https://docs.datadoghq.com/agent/basic_agent_usage/kubernetes/#log-collection-setup
// Default: true
//
// +optional
ContainerCollectUsingFiles *bool `json:"containerCollectUsingFiles,omitempty"`

// This to allow log collection from container log path. Set to a different path if not using docker runtime.
// ref: https://docs.datadoghq.com/agent/kubernetes/daemonset_setup/?tab=k8sfile#create-manifest
// Default to `/var/lib/docker/containers`
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/datadoghq/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/apis/datadoghq/v1alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/controller/datadogagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func defaultEnvVars() []corev1.EnvVar {
Name: "DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL",
Value: "false",
},
{
Name: "DD_LOGS_CONFIG_K8S_CONTAINER_USE_FILE",
Value: "true",
},
{
Name: "DD_DOGSTATSD_ORIGIN_DETECTION",
Value: "false",
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/datadogagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ func getEnvVarsForAgent(dda *datadoghqv1alpha1.DatadogAgent) ([]corev1.EnvVar, e
Name: datadoghqv1alpha1.DDLogsConfigContainerCollectAll,
Value: strconv.FormatBool(*spec.Agent.Log.LogsConfigContainerCollectAll),
},
{
Name: datadoghqv1alpha1.DDLogsContainerCollectUsingFiles,
Value: strconv.FormatBool(*spec.Agent.Log.ContainerCollectUsingFiles),
},
{
Name: datadoghqv1alpha1.DDDogstatsdOriginDetection,
Value: strconv.FormatBool(*spec.Agent.Config.Dogstatsd.DogstatsdOriginDetection),
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/datadogagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,13 @@ type logWriter struct {
}

func (l *logWriter) Write(b []byte) (int, error) {
l.t.Helper()
l.t.Logf("pod [%s/%s - %s]: %s", l.namespace, l.name, l.container, string(b))
return len(b), nil
}

func exportPodsLogs(t *testing.T, f *framework.Framework, namespace string, err error) {
t.Helper()
if err == nil {
return
}
Expand Down Expand Up @@ -755,6 +757,7 @@ func exportPodsLogs(t *testing.T, f *framework.Framework, namespace string, err
}

func printPods(t *testing.T, f *framework.Framework, namespace string) {
t.Helper()
podList := &corev1.PodList{}
namespaceOption := &dynclient.ListOptions{Namespace: namespace}
_ = f.Client.List(goctx.TODO(), podList, namespaceOption)
Expand All @@ -768,6 +771,7 @@ func printPods(t *testing.T, f *framework.Framework, namespace string) {
}

func printDaemonSet(t *testing.T, f *framework.Framework, namespace string) {
t.Helper()
dsList := &appsv1.DaemonSetList{}
namespaceOption := &dynclient.ListOptions{Namespace: namespace}
_ = f.Client.List(goctx.TODO(), dsList, namespaceOption)
Expand Down

0 comments on commit 8b1b1fe

Please sign in to comment.