The Datadog Agent is not meant to be imported as a Go library. However, certain parts of it can be exposed as a library by making use of nested Go modules. This exposes parts of the codebase without extracting the code to a different repository, and it avoids replace
directive clashes.
At present the Go modules offer no public stability guarantees and are intended for internal consumption by Datadog only.
Before you create a Go nested module, determine the packages that you want to expose and their dependencies. You might need to refactor the code to have an exportable package, because the replace
directives that we use might be incompatible with your project.
After you have refactored, if needed, and listed the packages that you want to expose and their dependencies, follow these steps for each module you want to create:
- Create
go.mod
andgo.sum
files in the module root folder. You can usego mod init && go mod tidy
within the module folder as a starting point. Ensure thego version
line matches the version in the maingo.mod
file. - On each module that depends on the current one, add a
require
directive with the module path with versionv0.0.0
.require ( // ... github.com/DataDog/datadog-agent/path/to/module v.0.0.0 // ... )
- On each module that depends on the current one, add a
replace
directive to replace the module with the repository where it lives.// main go.mod file replace ( github.com/DataDog/datadog-agent/path/to/module => ./path/to/module )
- Update the
DEFAULT_MODULES
dictionary on thetasks/modules.py
file. You need to:- create a new module, specifying the path, targets, dependencies and a condition to run tests (if any) and
- update the dependencies of other modules if they depend on this one.
For example, if
pkg/A
depends onpkg/B
andpkg/B
is Windows only, we would specify:
DEFAULT_MODULES = { "pkg/A": GoModule("pkg/A", dependencies=["pkg/B"]), "pkg/B": GoModule("pkg/B", condition=lambda: sys.platform == "win32") }
Go nested modules interdependencies are automatically updated when creating a release candidate or a final version, with the same tasks that update the release.json
. For Agent version 7.X.Y
the module will have version v0.X.Y
.
Go nested modules are tagged automatically by the release.tag-version
invoke task, on the same commit as the main module, with a tag of the form path/to/module/v0.X.Y
.