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

Add sourcemap periodic refresh tracing #11116

Merged
merged 7 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -17,3 +17,4 @@ https://github.com/elastic/apm-server/compare/8.9\...main[View commits]

[float]
==== Added
- Add a self-instrumentation transaction to the source map periodic refresh action. {pull}11116[11116]
4 changes: 3 additions & 1 deletion internal/beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (s *Runner) Run(ctx context.Context) error {
fetcher, cancel, err := newSourcemapFetcher(
s.config.RumConfig.SourceMapping,
kibanaClient, newElasticsearchClient,
tracer,
)
if err != nil {
return err
Expand Down Expand Up @@ -839,6 +840,7 @@ func newSourcemapFetcher(
cfg config.SourceMapping,
kibanaClient *kibana.Client,
newElasticsearchClient func(*elasticsearch.Config) (*elasticsearch.Client, error),
tracer *apm.Tracer,
) (sourcemap.Fetcher, context.CancelFunc, error) {
esClient, err := newElasticsearchClient(cfg.ESConfig)
if err != nil {
Expand All @@ -849,7 +851,7 @@ func newSourcemapFetcher(

// start background sync job
ctx, cancel := context.WithCancel(context.Background())
metadataFetcher, invalidationChan := sourcemap.NewMetadataFetcher(ctx, esClient, sourcemapIndex)
metadataFetcher, invalidationChan := sourcemap.NewMetadataFetcher(ctx, esClient, sourcemapIndex, tracer)

esFetcher := sourcemap.NewElasticsearchFetcher(esClient, sourcemapIndex)
size := 128
Expand Down
2 changes: 2 additions & 0 deletions internal/beater/beater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.elastic.co/apm/v2/apmtest"

"github.com/elastic/apm-server/internal/beater/config"
"github.com/elastic/apm-server/internal/elasticsearch"
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestStoreUsesRUMElasticsearchConfig(t *testing.T) {
_, cancel, err := newSourcemapFetcher(
cfg.RumConfig.SourceMapping,
nil, elasticsearch.NewClient,
apmtest.NewRecordingTracer().Tracer,
)
require.NoError(t, err)
defer cancel()
Expand Down
34 changes: 33 additions & 1 deletion internal/sourcemap/metadata_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"sync"
"time"

"go.elastic.co/apm/v2"

"github.com/elastic/apm-server/internal/elasticsearch"
"github.com/elastic/apm-server/internal/logs"
"github.com/elastic/elastic-agent-libs/logp"
Expand All @@ -43,9 +45,15 @@ type MetadataESFetcher struct {
init chan struct{}
initErr error
invalidationChan chan<- []identifier
tracer *apm.Tracer
}

func NewMetadataFetcher(ctx context.Context, esClient *elasticsearch.Client, index string) (MetadataFetcher, <-chan []identifier) {
func NewMetadataFetcher(
ctx context.Context,
esClient *elasticsearch.Client,
index string,
tracer *apm.Tracer,
) (MetadataFetcher, <-chan []identifier) {
invalidationCh := make(chan []identifier)

s := &MetadataESFetcher{
Expand All @@ -56,6 +64,7 @@ func NewMetadataFetcher(ctx context.Context, esClient *elasticsearch.Client, ind
logger: logp.NewLogger(logs.Sourcemap),
init: make(chan struct{}),
invalidationChan: invalidationCh,
tracer: tracer,
}

s.startBackgroundSync(ctx)
Expand Down Expand Up @@ -125,10 +134,18 @@ func (s *MetadataESFetcher) startBackgroundSync(ctx context.Context) {
}

func (s *MetadataESFetcher) sync(ctx context.Context) error {
tx := s.tracer.StartTransaction("MetadataESFetcher.sync", "")
defer tx.End()
ctx = apm.ContextWithTransaction(ctx, tx)

sourcemaps := make(map[identifier]string)

result, err := s.initialSearch(ctx, sourcemaps)
if err != nil {
if e := apm.CaptureError(ctx, err); e != nil {
e.Send()
}

return err
}

Expand All @@ -142,6 +159,9 @@ func (s *MetadataESFetcher) sync(ctx context.Context) error {
for {
result, err = s.scrollsearch(ctx, scrollID, sourcemaps)
if err != nil {
if e := apm.CaptureError(ctx, err); e != nil {
e.Send()
}
return fmt.Errorf("failed scroll search: %w", err)
}

Expand All @@ -164,6 +184,9 @@ func (s *MetadataESFetcher) sync(ctx context.Context) error {
}

func (s *MetadataESFetcher) update(ctx context.Context, sourcemaps map[identifier]string) {
span := apm.TransactionFromContext(ctx).StartSpan("MetadataESFetcher.update", "", nil)
defer span.End()

s.mu.Lock()
defer s.mu.Unlock()

Expand Down Expand Up @@ -223,8 +246,14 @@ func (s *MetadataESFetcher) update(ctx context.Context, sourcemaps map[identifie
}

func (s *MetadataESFetcher) initialSearch(ctx context.Context, updates map[identifier]string) (*esSearchSourcemapResponse, error) {
span := apm.TransactionFromContext(ctx).StartSpan("MetadataESFetcher.initialSearch", "", nil)
defer span.End()

resp, err := s.runSearchQuery(ctx)
if err != nil {
if e := apm.CaptureError(ctx, err); e != nil {
e.Send()
}
return nil, fmt.Errorf("failed to run initial search query: %w: %v", errFetcherUnvailable, err)
}
defer resp.Body.Close()
Expand Down Expand Up @@ -315,6 +344,9 @@ func parseResponse(body io.ReadCloser, logger *logp.Logger) (*esSearchSourcemapR
}

func (s *MetadataESFetcher) scrollsearch(ctx context.Context, scrollID string, updates map[identifier]string) (*esSearchSourcemapResponse, error) {
span := apm.TransactionFromContext(ctx).StartSpan("MetadataESFetcher.scrollSearch", "", nil)
defer span.End()

resp, err := s.runScrollSearchQuery(ctx, scrollID)
if err != nil {
return nil, fmt.Errorf("failed to run scroll search query: %w", err)
Expand Down
12 changes: 10 additions & 2 deletions internal/sourcemap/metadata_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.elastic.co/apm/v2/apmtest"
"go.elastic.co/apm/v2/transport/transporttest"

"github.com/elastic/apm-server/internal/elasticsearch"
"github.com/elastic/elastic-agent-libs/logp"
Expand Down Expand Up @@ -117,7 +119,8 @@ func TestMetadataFetcher(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

fetcher, _ := NewMetadataFetcher(ctx, esClient, ".apm-source-map")
tracer, recorder := transporttest.NewRecorderTracer()
fetcher, _ := NewMetadataFetcher(ctx, esClient, ".apm-source-map", tracer)

<-fetcher.ready()
if tc.expectErr {
Expand All @@ -130,6 +133,10 @@ func TestMetadataFetcher(t *testing.T) {
assert.Equal(t, tc.expectID, ok)

close(waitCh)
tracer.Flush(nil)

assert.Len(t, recorder.Payloads().Transactions, 1)
assert.Greater(t, len(recorder.Payloads().Spans), 1)
})
}
}
Expand Down Expand Up @@ -243,7 +250,8 @@ func TestInvalidation(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

fetcher, invalidationChan := NewMetadataFetcher(ctx, esClient, ".apm-source-map")
rt := apmtest.NewRecordingTracer()
fetcher, invalidationChan := NewMetadataFetcher(ctx, esClient, ".apm-source-map", rt.Tracer)

invCh := make(chan struct{})
go func() {
Expand Down