Skip to content

Commit

Permalink
apm auto-inject launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
zwj authored and 谭彪 committed Oct 18, 2024
1 parent 2e5e5b5 commit 31389b0
Show file tree
Hide file tree
Showing 24 changed files with 1,582 additions and 50 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,3 @@ internal/plugins/externals/ebpf/demo/
internal/plugins/externals/ebpf/internal/testuitls/mysqlins/mysqlins
internal/export/doc/zh/inputs/imgs/tracing.png
/git
/build
7 changes: 7 additions & 0 deletions Dockerfile_dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:20.04

ARG TARGETARCH

RUN export DEBIAN_FRONTEND=noninteractive \
&& sed -i 's/\(archive\|security\|ports\).ubuntu.com/mirrors.aliyun.com/' /etc/apt/sources.list \
&& apt-get update && apt-get install -y make curl tree tzdata gcc
4 changes: 4 additions & 0 deletions Dockerfile_dev_musl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM alpine:3.14

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add make build-base alpine-sdk
2 changes: 2 additions & 0 deletions cmd/installer/installer/dkconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ var (
HostName,
IPDBType string

InstrumentationEnabled string

ConfdBackend,
ConfdBasicAuth,
ConfdClientCaKeys,
Expand Down
32 changes: 28 additions & 4 deletions cmd/installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import (

"gitlab.jiagouyun.com/cloudcare-tools/datakit/cmd/installer/installer"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/cmd/upgrader/upgrader"
apminj "gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/apminject/utils"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/cmds"
cp "gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/colorprint"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/config"

"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/datakit"
dl "gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/downloader"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/git"
Expand Down Expand Up @@ -70,6 +72,12 @@ var (
runtime.GOARCH,
DataKitVersion))

datakitAPMInjectURL = "https://" + path.Join(DataKitBaseURL,
fmt.Sprintf("datakit-apm-inject-%s-%s-%s.tar.gz",
runtime.GOOS,
runtime.GOARCH,
DataKitVersion))

InstallerBaseURL = ""

l = logger.DefaultSLogger("installer")
Expand Down Expand Up @@ -119,6 +127,10 @@ func init() {
flag.StringVar(&flagLite, "lite", "", "install datakit lite")
flag.StringVar(&flagELinker, "elinker", "", "install datakit eBPF span linker")

flag.StringVar(&installer.InstrumentationEnabled, "apm-instrumentation-enabled", "", "enable APM instrumentation")
// flag.StringVar(&flagAPMInstrumentationLibraries, "apm-instrumentation-libraries", "datadog|java,python",
// "install and use the APM library of the specified provider")

flag.StringVar(&installer.Dataway, "dataway", "", "DataWay host(https://guance.openway.com?token=xxx)")
flag.StringVar(&installer.Proxy, "proxy", "", "http proxy http://ip:port for datakit")
flag.StringVar(&installer.DatakitName, "name", "", "specify DataKit name, example: prod-env-datakit")
Expand Down Expand Up @@ -260,7 +272,6 @@ func downloadFiles(to string) error {
if err := dl.Download(cli, dkURL, to, true, flagDownloadOnly); err != nil {
return err
}

fmt.Printf("\n")

dl.CurDownloading = "data"
Expand All @@ -272,16 +283,29 @@ func downloadFiles(to string) error {

// We will not upgrade dk-upgrader default when upgrading Datakit except for setting flagUpgradeManagerService flag
if !flagDKUpgrade || (flagDKUpgrade && flagUpgraderEnabled == 1) || flagDownloadOnly {
toUpgrader := to
if !flagDownloadOnly {
to = upgrader.InstallDir
toUpgrader = upgrader.InstallDir
}
dl.CurDownloading = upgrader.BuildBinName
cp.Infof("Downloading %s => %s\n", dkUpgraderURL, to)
if err := dl.Download(cli, dkUpgraderURL, to, true, flagDownloadOnly); err != nil {
cp.Infof("Downloading %s => %s\n", dkUpgraderURL, toUpgrader)
if err := dl.Download(cli, dkUpgraderURL, toUpgrader, true, flagDownloadOnly); err != nil {
l.Warnf("unable to download %s from [%s]: %s", upgrader.BuildBinName, dkUpgraderURL, err)
}
}

if installer.InstrumentationEnabled != "" && runtime.GOOS == "linux" &&
(runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64") {
if err := apminj.Download(l, apminj.WithOnline(cli, datakitAPMInjectURL),
apminj.WithInstallDir(to),
apminj.WithInstrumentationEnabled(installer.InstrumentationEnabled),
); err != nil {
l.Warnf("download apm inject failed: %s", err.Error())
} else {
config.Cfg.APMInject.InstrumentationEnabled = installer.InstrumentationEnabled
}
}

if installer.IPDBType != "" {
fmt.Printf("\n")
baseURL := "https://" + DataKitBaseURL
Expand Down
40 changes: 40 additions & 0 deletions cmd/make/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func Compile() error {
return err
}

if err := compileAPMInject(goos, goarch, BuildDir); err != nil {
return err
}

upgraderDir := fmt.Sprintf("%s/%s-%s-%s", BuildDir, upgrader.BuildBinName, goos, goarch)
l.Debugf("upgraderDir = %s", upgraderDir)
if err := compileArch(upgrader.BuildBinName,
Expand Down Expand Up @@ -340,6 +344,42 @@ func compileArch(bin, goos, goarch, dir, mainEntranceFile, tags string) error {
return nil
}

func compileAPMInject(goos, goarch, dir string) error {
if goos != "linux" {
l.Warnf("skip building apm auto-inject launcher: unsupported os %s", goos)
return nil
}

if goarch != "amd64" && goarch != "arm64" {
l.Warnf("skip building apm auto-inject launcher: unsupported arch %s", goarch)
return nil
}

_, err := exec.LookPath("docker")
if err != nil {
l.Warnf("skip building apm auto-inject launcher: %s",
err.Error())
return nil
}

cmdArgs := []string{
"sh", "internal/apminject/build_lib.sh",
goarch, fmt.Sprintf("%s/datakit-apm-inject-linux-%s", dir, goarch),
}

l.Debugf("building %v with %v", fmt.Sprintf("%s-%s/%s",
goos, goarch, "apm-auto-inject-launcher"), cmdArgs)

var envs []string
msg, err := runEnv(cmdArgs, envs)
if err != nil {
return fmt.Errorf("failed to run %v, envs: %v: %w, msg: %s",
cmdArgs, envs, err, string(msg))
}

return nil
}

// is_extra_lite check whether to build lite datakit.
func isExtraLite() bool {
extraLite := true
Expand Down
7 changes: 7 additions & 0 deletions cmd/make/build/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ func PubDatakit() error {
}
}

// apm-auto-inject-launcher
if goos == Linux && (goarch == AMD64 || goarch == ARM64) {
gzName, gzPath := tarFiles(
PubDir, BuildDir, "datakit-apm-inject", goos, goarch, TarWithRlsVer)
basics[gzName] = gzPath
}

upgraderGZFile, upgraderGZPath := tarFiles(PubDir, BuildDir, upgrader.BuildBinName, parts[0], parts[1], TarNoRlsVer)

installerExe := fmt.Sprintf("installer-%s-%s", goos, goarch)
Expand Down
8 changes: 8 additions & 0 deletions install.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ if [ -n "$DK_ELINKER" ]; then
printf "* Set elinker => $DK_ELINKER\n"
fi

apmInstrumentation=
if [ -n "$DK_APM_INSTRUMENTATION_ENABLED" ]; then
apmInstrumentation=$DK_APM_INSTRUMENTATION_ENABLED
printf "* Set apm-instrumentation-enabled => $DK_APM_INSTRUMENTATION_ENABLED\n"
fi

global_customer_keys=
if [ -n "$DK_SINKER_GLOBAL_CUSTOMER_KEYS" ]; then
global_customer_keys=$DK_SINKER_GLOBAL_CUSTOMER_KEYS
Expand Down Expand Up @@ -550,6 +556,7 @@ if [ "$upgrade" ]; then
--install-log="$install_log" \
--upgrade --lite="${lite}" \
--elinker="${elinker}" \
--apm-instrumentation-enabled="${apmInstrumentation}" \
--upgrade-manager="${upgrade_manager}" \
--upgrade-ip-whitelist="${upgrade_ip_whitelist}" \
--upgrade-listen="${upgrade_listen}" \
Expand All @@ -575,6 +582,7 @@ $sudo_cmd $installer \
--proxy="${proxy}" \
--lite="${lite}" \
--elinker="${elinker}" \
--apm-instrumentation-enabled="${apmInstrumentation}" \
--env_hostname="${env_hostname}" \
--dca-enable="${dca_enable}" \
--dca-listen="${dca_listen}" \
Expand Down
52 changes: 52 additions & 0 deletions internal/apminject/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.PHONY: all debug rewriter test

ARCH ?= $(shell uname -m | sed -e s/x86_64/x86_64/ \
-e s/aarch64.\*/aarch64/)

ifeq ($(ARCH), x86_64)
ARCH = amd64
endif
ifeq ($(ARCH), aarch64)
ARCH = arm64
endif

REPO_PATH ?= ${HOME}/go/src/gitlab.jiagouyun.com/cloudcare-tools/datakit
SOURCE_PATH := $(REPO_PATH)/internal/apminject
DIST_DIR ?= $(REPO_PATH)/internal/apminject/dist/datakit-apm-inject-linux-$(ARCH)

DATAKIT_INJ_REWRITE_PROC ?= \"/usr/local/datakit/apm_inject/inject/rewriter\"

all: rewriter launcher

rewriter:
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o $(DIST_DIR)/rewriter \
$(SOURCE_PATH)/rewriter/rewriter.go

launcher:
mkdir -p $(DIST_DIR)
gcc -DDATAKIT_INJ_REWRITE_PROC=${DATAKIT_INJ_REWRITE_PROC} \
$(SOURCE_PATH)/apm_launcher.c -fPIC -shared \
-o $(DIST_DIR)/apm_launcher.so

launcher_musl:
mkdir -p $(DIST_DIR)
gcc -DDATAKIT_INJ_REWRITE_PROC=${DATAKIT_INJ_REWRITE_PROC} \
$(SOURCE_PATH)/apm_launcher.c -fPIC -shared \
-o $(DIST_DIR)/apm_launcher_musl.so

launcher_debug:
mkdir -p ${DIST_DIR}
gcc -DDATAKIT_DEBUG -DDATAKIT_INJ_REWRITE_PROC=${DATAKIT_INJ_REWRITE_PROC} \
$(SOURCE_PATH)/apm_launcher.c -fPIC -shared \
-o $(DIST_DIR)/apm_launcher_debug.so

launcher_musl_debug:
mkdir -p ${DIST_DIR}
gcc -DDATAKIT_DEBUG -DDATAKIT_INJ_REWRITE_PROC=${DATAKIT_INJ_REWRITE_PROC} \
$(SOURCE_PATH)/apm_launcher.c -fPIC -shared \
-o $(DIST_DIR)/apm_launcher_musl_debug.so

test:
mkdir -p ${DIST_DIR}
gcc $(SOURCE_PATH)/launcher_test.c -o $(DIST_DIR)/launcher_test \
&& $(DIST_DIR)/launcher_test && rm $(DIST_DIR)/launcher_test
Loading

0 comments on commit 31389b0

Please sign in to comment.