Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitoring: enable tracing self instrumentation in APM Server #14231

Merged
merged 13 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ https://github.com/elastic/apm-server/compare/8.15\...main[View commits]
==== Added

- APM Server will no longer retry an HTTP request that returned 502s, 503s, 504s. It will only retry 429s. {pull}13523[13523]
- APM Server now supports emitting distributed tracing for its own operation when running under Elastic Agent, and adds support for configuring a sampling rate {pull}14231[14231]
2 changes: 1 addition & 1 deletion internal/beatcmd/beat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestRunManager(t *testing.T) {
},
},
"instrumentation": map[string]interface{}{
"enabled": false,
"enabled": true,
"environment": "testenv",
},
}, m)
Expand Down
3 changes: 1 addition & 2 deletions internal/beatcmd/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ func (r *Reloader) reload(inputConfig, outputConfig, apmTracingConfig *config.C)
return fmt.Errorf("APM tracing config for elastic not found")
}
// set enabled manually as APMConfig doesn't contain it.
// TODO set "enable" to true after the issue https://github.com/elastic/elastic-agent/issues/5211 gets resolved.
c.SetBool("enabled", -1, false)
c.SetBool("enabled", -1, true)
wrappedApmTracingConfig = config.MustNewConfigFrom(map[string]interface{}{
"instrumentation": c,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/beatcmd/reloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestReloaderNewRunnerParams(t *testing.T) {
args := <-calls
assert.NotNil(t, args.Logger)
assert.Equal(t, info, args.Info)
assert.Equal(t, config.MustNewConfigFrom(`{"revision": 1, "input": 123, "output.console.enabled": true, "instrumentation.enabled":false, "instrumentation.environment":"test"}`), args.Config)
assert.Equal(t, config.MustNewConfigFrom(`{"revision": 1, "input": 123, "output.console.enabled": true, "instrumentation.enabled":true, "instrumentation.environment":"test"}`), args.Config)
}

func expectNoEvent(t testing.TB, ch <-chan struct{}, message string) {
Expand Down
8 changes: 8 additions & 0 deletions internal/beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http"
"os"
"runtime"
"strconv"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -525,6 +526,7 @@ func newInstrumentation(rawConfig *agentconfig.C) (instrumentation.Instrumentati
ServerCertificate string `config:"servercert"`
ServerCA string `config:"serverca"`
} `config:"tls"`
SamplingRate *float32 `config:"samplingrate"`
}
cfg, err := rawConfig.Child("instrumentation", -1)
if err != nil || !cfg.Enabled() {
Expand All @@ -541,6 +543,7 @@ func newInstrumentation(rawConfig *agentconfig.C) (instrumentation.Instrumentati
envServerCert = "ELASTIC_APM_SERVER_CERT"
envCACert = "ELASTIC_APM_SERVER_CA_CERT_FILE"
envGlobalLabels = "ELASTIC_APM_GLOBAL_LABELS"
envSamplingRate = "ELASTIC_APM_TRANSACTION_SAMPLE_RATE"
)
if apmCfg.APIKey != "" {
os.Setenv(envAPIKey, apmCfg.APIKey)
Expand All @@ -566,6 +569,11 @@ func newInstrumentation(rawConfig *agentconfig.C) (instrumentation.Instrumentati
os.Setenv(envGlobalLabels, apmCfg.GlobalLabels)
defer os.Unsetenv(envGlobalLabels)
}
if apmCfg.SamplingRate != nil {
r := max(min(*apmCfg.SamplingRate, 1.0), 0.0)
os.Setenv(envSamplingRate, strconv.FormatFloat(float64(r), 'f', -1, 32))
defer os.Unsetenv(envSamplingRate)
}
return instrumentation.New(rawConfig, "apm-server", version.Version)
}

Expand Down
42 changes: 42 additions & 0 deletions internal/beater/beater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (
"encoding/pem"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -285,6 +287,46 @@ func TestNewInstrumentation(t *testing.T) {
assert.Equal(t, "Bearer secret", auth)
}

func TestNewInstrumentationWithSampling(t *testing.T) {
runSampled := func(rate float32) {
var events int
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/intake/v2/events" {
zr, _ := zlib.NewReader(r.Body)
b, _ := io.ReadAll(zr)
// Skip metadata and transaction keys, only count span.
events = strings.Count(string(b), "\n") - 2
}
w.WriteHeader(http.StatusOK)
}))
defer s.Close()
cfg := agentconfig.MustNewConfigFrom(map[string]interface{}{
"instrumentation": map[string]interface{}{
"enabled": true,
"hosts": []string{s.URL},
"tls": map[string]interface{}{
"skipverify": true,
},
"samplingrate": fmt.Sprintf("%f", rate),
},
})
i, err := newInstrumentation(cfg)
require.NoError(t, err)
tracer := i.Tracer()
tr := tracer.StartTransaction("name", "type")
tr.StartSpan("span", "type", nil).End()
tr.End()
tracer.Flush(nil)
assert.Equal(t, int(rate), events)
}
t.Run("100% sampling", func(t *testing.T) {
runSampled(1.0)
})
t.Run("0% sampling", func(t *testing.T) {
runSampled(0.0)
})
}

func TestProcessMemoryLimit(t *testing.T) {
l := logp.NewLogger("test")
const gb = 1 << 30
Expand Down
Loading