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

Service specific source maps #5410

Merged
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
2d72bd9
wip
stuartnelson3 Jun 1, 2021
39344ed
fleet/es branching working
stuartnelson3 Jun 1, 2021
d2e4248
add apikey to fleetStore
stuartnelson3 Jun 3, 2021
ae01674
[sourcemap/store] comment about duplicated config
stuartnelson3 Jun 3, 2021
5b8d875
implement fleet store+test
stuartnelson3 Jun 3, 2021
5a6a1ba
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 3, 2021
b612d3f
[store/fleet] use same Transport as es client
stuartnelson3 Jun 3, 2021
c1a9d49
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 3, 2021
382e5dd
remove old sourcemap api key code
stuartnelson3 Jun 4, 2021
d100bc1
use `key` struct for fleet config cache
stuartnelson3 Jun 4, 2021
3ccda4c
flatten sourcemap config struct defn
stuartnelson3 Jun 4, 2021
679f2b5
public elasticsearch/fleet constructor methods
stuartnelson3 Jun 4, 2021
c3a8aa8
synchronize cache fetch on miss
stuartnelson3 Jun 4, 2021
c082b70
write TODO about killing piling up requests
stuartnelson3 Jun 4, 2021
c4d2b1f
Add waiters semaphore
stuartnelson3 Jun 7, 2021
22dd903
remove unnecessary else block
stuartnelson3 Jun 7, 2021
c42479e
add timeout for waiting
stuartnelson3 Jun 7, 2021
9bd9bfe
update docs
stuartnelson3 Jun 7, 2021
f112d11
update changelog
stuartnelson3 Jun 7, 2021
37e0d4a
stop configuring fleet-server http client
stuartnelson3 Jun 7, 2021
72a13fe
/s/sourcemaps/metadata/
stuartnelson3 Jun 8, 2021
543f37b
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 8, 2021
718735b
fix comment
stuartnelson3 Jun 9, 2021
dd6cff7
remove sourcemap integration test
stuartnelson3 Jun 9, 2021
be1cd15
release waiters semaphore if there's a timeout
stuartnelson3 Jun 9, 2021
041c97a
remove references to version
stuartnelson3 Jun 9, 2021
5bf422c
[store] don't make mutex publicly callable
stuartnelson3 Jun 9, 2021
6948b67
[store] check for ctx.Done() while waiting
stuartnelson3 Jun 9, 2021
345b38b
check for error when reading fleet response
stuartnelson3 Jun 9, 2021
7920d64
[store] verify rum elasticsearch config used
stuartnelson3 Jun 9, 2021
6d85b80
delete repeated types in Fetch() signature
stuartnelson3 Jun 9, 2021
4ae72bb
[store] remove waiter limit
stuartnelson3 Jun 9, 2021
3650b48
remove waiters struct member
stuartnelson3 Jun 9, 2021
7d2e73d
wrap comment to 80 char
stuartnelson3 Jun 9, 2021
6dbee91
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 10, 2021
2bbf00b
systemtest: add rate limiting test (#5430)
axw Jun 10, 2021
93f7b44
Hardcode troughput settings for cloud environment (#5402)
jalvz Jun 10, 2021
6859ecb
systemtest: fix apm-server binary injection (#5440)
axw Jun 14, 2021
71a8792
remove timeout on waiting for fetch
stuartnelson3 Jun 14, 2021
1a9676e
use provided fleetmode.Enabled()
stuartnelson3 Jun 14, 2021
47223cc
[fleet] add tls fields to config
stuartnelson3 Jun 14, 2021
fee6582
[fleet] remove kibana config references
stuartnelson3 Jun 14, 2021
503506f
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 14, 2021
90a7318
add conditional around rendering api-key
stuartnelson3 Jun 16, 2021
e864156
add tls + response decompression
stuartnelson3 Jun 16, 2021
5b3ba88
remove dead comments and move code
stuartnelson3 Jun 16, 2021
2efc10a
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 16, 2021
e339269
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 17, 2021
74b57d3
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 18, 2021
db22ff0
Update apmpackage/apm/agent/input/template.yml.hbs
stuartnelson3 Jun 22, 2021
778d0e9
Move rate limiting and service name restrictions out of processor/str…
axw Jun 19, 2021
295d1c1
docker-compose: fleet-server depends on kibana (#5496)
axw Jun 21, 2021
477822c
beater: even more refactoring (#5502)
axw Jun 21, 2021
c4dc133
Fix UBI source URL (#5506)
jmlrt Jun 22, 2021
67bb80d
systemtest: remove TestDataStream*, fix Fleet test (#5503)
axw Jun 22, 2021
1cc5077
remove sourcemap api key from manifest
stuartnelson3 Jun 22, 2021
bb2c6c9
use single fleetcfg nil check
stuartnelson3 Jun 22, 2021
65ad079
attach context to outgoing fleet request
stuartnelson3 Jun 22, 2021
7711ee4
test sourcemap url path
stuartnelson3 Jun 22, 2021
3799cd5
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 22, 2021
051baa8
remove duplicate rum enable field
stuartnelson3 Jun 22, 2021
85193f5
update fleet hosts TODO
stuartnelson3 Jun 23, 2021
77fc0a0
verify that fleet store correctly uses Protocol
stuartnelson3 Jun 23, 2021
35468ac
show fleet used if fleetcfg != nil
stuartnelson3 Jun 23, 2021
78d2f1f
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 23, 2021
d098115
Merge branch 'master' into service-specific-source-maps
stuartnelson3 Jun 23, 2021
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
5 changes: 4 additions & 1 deletion _meta/beat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ apm-server:
# Sourcemapping is enabled by default.
#enabled: true

# Source maps are always fetched from Elasticsearch, by default using the output.elasticsearch configuration.
# Source maps may be fetched from Elasticsearch by using the
# output.elasticsearch configuration.
# A different instance must be configured when using any other output.
# This setting only affects sourcemap reads - the output determines where sourcemaps are written.
# Note: Configuring elasticsearch is not supported if apm-server is being
# managed by Fleet.
#elasticsearch:
# Array of hosts to connect to.
# Scheme and port can be left out and will be set to the default (`http` and `9200`).
Expand Down
5 changes: 4 additions & 1 deletion apm-server.docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ apm-server:
# Sourcemapping is enabled by default.
#enabled: true

# Source maps are always fetched from Elasticsearch, by default using the output.elasticsearch configuration.
# Source maps may be fetched from Elasticsearch by using the
# output.elasticsearch configuration.
# A different instance must be configured when using any other output.
# This setting only affects sourcemap reads - the output determines where sourcemaps are written.
# Note: Configuring elasticsearch is not supported if apm-server is being
# managed by Fleet.
#elasticsearch:
# Array of hosts to connect to.
# Scheme and port can be left out and will be set to the default (`http` and `9200`).
Expand Down
5 changes: 4 additions & 1 deletion apm-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ apm-server:
# Sourcemapping is enabled by default.
#enabled: true

# Source maps are always fetched from Elasticsearch, by default using the output.elasticsearch configuration.
# Source maps may be fetched from Elasticsearch by using the
# output.elasticsearch configuration.
# A different instance must be configured when using any other output.
# This setting only affects sourcemap reads - the output determines where sourcemaps are written.
# Note: Configuring elasticsearch is not supported if apm-server is being
# managed by Fleet.
#elasticsearch:
# Array of hosts to connect to.
# Scheme and port can be left out and will be set to the default (`http` and `9200`).
Expand Down
6 changes: 0 additions & 6 deletions apmpackage/apm/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ policy_templates:
required: false
show_user: false
default: 10000
- name: sourcemap_api_key
type: text
title: RUM - API Key for Sourcemaps
required: false
description: API Key for sourcemap feature. Enter as <Id>:<API Key>
show_user: false
- name: api_key_limit
type: integer
title: Maximum number of API Keys for Agent authentication
Expand Down
68 changes: 49 additions & 19 deletions beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ package beater
import (
"context"
"net"
"net/http"
"regexp"
"runtime"
"strings"
"sync"
"time"

"github.com/elastic/beats/v7/libbeat/common/fleetmode"

"github.com/elastic/beats/v7/libbeat/kibana"
"github.com/elastic/beats/v7/libbeat/common/transport"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"

"github.com/pkg/errors"
"go.elastic.co/apm"
"go.elastic.co/apm/module/apmhttp"
"golang.org/x/sync/errgroup"

"github.com/elastic/beats/v7/libbeat/beat"
Expand Down Expand Up @@ -256,8 +258,8 @@ func (s *serverCreator) Create(p beat.PipelineConnector, rawConfig *common.Confi
sharedServerRunnerParams: s.args,
Namespace: namespace,
Pipeline: p,
KibanaConfig: &integrationConfig.Fleet.Kibana,
RawConfig: apmServerCommonConfig,
FleetConfig: &integrationConfig.Fleet,
})
}

Expand All @@ -279,6 +281,7 @@ type serverRunner struct {
acker *waitPublishedAcker
namespace string
config *config.Config
fleetConfig *config.Fleet
beat *beat.Beat
logger *logp.Logger
tracer *apm.Tracer
Expand All @@ -289,10 +292,10 @@ type serverRunner struct {
type serverRunnerParams struct {
sharedServerRunnerParams

Namespace string
Pipeline beat.PipelineConnector
KibanaConfig *kibana.ClientConfig
RawConfig *common.Config
Namespace string
Pipeline beat.PipelineConnector
RawConfig *common.Config
FleetConfig *config.Fleet
}

type sharedServerRunnerParams struct {
Expand All @@ -310,17 +313,14 @@ func newServerRunner(ctx context.Context, args serverRunnerParams) (*serverRunne
return nil, err
}

if cfg.DataStreams.Enabled && args.KibanaConfig != nil {
cfg.Kibana.ClientConfig = *args.KibanaConfig
}

runServerContext, cancel := context.WithCancel(ctx)
return &serverRunner{
backgroundContext: ctx,
runServerContext: runServerContext,
cancelRunServerContext: cancel,

config: cfg,
fleetConfig: args.FleetConfig,
acker: args.Acker,
pipeline: args.Pipeline,
namespace: args.Namespace,
Expand Down Expand Up @@ -359,8 +359,7 @@ func (s *serverRunner) Start() {
func (s *serverRunner) run() error {
// Send config to telemetry.
recordAPMServerConfig(s.config)

transformConfig, err := newTransformConfig(s.beat.Info, s.config)
transformConfig, err := newTransformConfig(s.beat.Info, s.config, s.fleetConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -602,7 +601,7 @@ func runServerWithTracerServer(runServer RunServerFunc, tracerServer *tracerServ
}
}

func newTransformConfig(beatInfo beat.Info, cfg *config.Config) (*transform.Config, error) {
func newTransformConfig(beatInfo beat.Info, cfg *config.Config, fleetCfg *config.Fleet) (*transform.Config, error) {
transformConfig := &transform.Config{
DataStreams: cfg.DataStreams.Enabled,
RUM: transform.RUMConfig{
Expand All @@ -611,8 +610,8 @@ func newTransformConfig(beatInfo beat.Info, cfg *config.Config) (*transform.Conf
},
}

if cfg.RumConfig.Enabled && cfg.RumConfig.SourceMapping.Enabled && cfg.RumConfig.SourceMapping.ESConfig != nil {
store, err := newSourcemapStore(beatInfo, cfg.RumConfig.SourceMapping)
if cfg.RumConfig.Enabled && cfg.RumConfig.SourceMapping.Enabled {
store, err := newSourcemapStore(beatInfo, cfg.RumConfig.SourceMapping, fleetCfg)
if err != nil {
return nil, err
}
Expand All @@ -622,13 +621,44 @@ func newTransformConfig(beatInfo beat.Info, cfg *config.Config) (*transform.Conf
return transformConfig, nil
}

func newSourcemapStore(beatInfo beat.Info, cfg config.SourceMapping) (*sourcemap.Store, error) {
esClient, err := elasticsearch.NewClient(cfg.ESConfig)
func newSourcemapStore(beatInfo beat.Info, cfg config.SourceMapping, fleetCfg *config.Fleet) (*sourcemap.Store, error) {
if fleetCfg != nil {
var (
c = *http.DefaultClient
rt = http.DefaultTransport
)
var tlsConfig *tlscommon.TLSConfig
var err error
if fleetCfg.TLS.IsEnabled() {
if tlsConfig, err = tlscommon.LoadTLSConfig(fleetCfg.TLS); err != nil {
return nil, err
}
}

// Default for es is 90s :shrug:
timeout := 30 * time.Second
dialer := transport.NetDialer(timeout)
tlsDialer, err := transport.TLSDialer(dialer, tlsConfig, timeout)
if err != nil {
return nil, err
}

rt = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
DialTLS: tlsDialer.Dial,
TLSClientConfig: tlsConfig.ToConfig(),
}

c.Transport = apmhttp.WrapRoundTripper(rt)
return sourcemap.NewFleetStore(&c, fleetCfg, cfg.Metadata, cfg.Cache.Expiration)
}
c, err := elasticsearch.NewClient(cfg.ESConfig)
if err != nil {
return nil, err
}
index := strings.ReplaceAll(cfg.IndexPattern, "%{[observer.version]}", beatInfo.Version)
return sourcemap.NewStore(esClient, index, cfg.Cache.Expiration)
return sourcemap.NewElasticsearchStore(c, index, cfg.Cache.Expiration)
}

// WrapRunServerWithProcessors wraps runServer such that it wraps args.Reporter
Expand Down
30 changes: 28 additions & 2 deletions beater/beater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import (
"go.uber.org/zap/zaptest/observer"

"github.com/elastic/apm-server/beater/config"
"github.com/elastic/apm-server/elasticsearch"
"github.com/elastic/apm-server/model"
"github.com/elastic/apm-server/sourcemap/test"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/instrumentation"
Expand Down Expand Up @@ -246,7 +248,7 @@ func TestTransformConfigIndex(t *testing.T) {
cfg.RumConfig.SourceMapping.IndexPattern = indexPattern
}

transformConfig, err := newTransformConfig(beat.Info{Version: "1.2.3"}, cfg)
transformConfig, err := newTransformConfig(beat.Info{Version: "1.2.3"}, cfg, nil)
require.NoError(t, err)
require.NotNil(t, transformConfig.RUM.SourcemapStore)
transformConfig.RUM.SourcemapStore.Added(context.Background(), "name", "version", "path")
Expand All @@ -266,7 +268,7 @@ func TestTransformConfig(t *testing.T) {
cfg := config.DefaultConfig()
cfg.RumConfig.Enabled = rumEnabled
cfg.RumConfig.SourceMapping.Enabled = sourcemapEnabled
transformConfig, err := newTransformConfig(beat.Info{Version: "1.2.3"}, cfg)
transformConfig, err := newTransformConfig(beat.Info{Version: "1.2.3"}, cfg, nil)
require.NoError(t, err)
if expectSourcemapStore {
assert.NotNil(t, transformConfig.RUM.SourcemapStore)
Expand All @@ -280,3 +282,27 @@ func TestTransformConfig(t *testing.T) {
test(true, false, false)
test(true, true, true)
}

func TestStoreUsesRUMElasticsearchConfig(t *testing.T) {
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
var called bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.Write([]byte(test.ValidSourcemap))
}))
defer ts.Close()

cfg := config.DefaultConfig()
cfg.RumConfig.Enabled = true
cfg.RumConfig.SourceMapping.Enabled = true
cfg.RumConfig.SourceMapping.ESConfig = elasticsearch.DefaultConfig()
cfg.RumConfig.SourceMapping.ESConfig.Hosts = []string{ts.URL}

transformConfig, err := newTransformConfig(beat.Info{Version: "1.2.3"}, cfg, nil)
require.NoError(t, err)
// Check that the provided rum elasticsearch config was used and
// Fetch() goes to the test server.
_, err = transformConfig.RUM.SourcemapStore.Fetch(context.Background(), "app", "1.0", "/bundle/path")
require.NoError(t, err)

assert.True(t, called)
}
17 changes: 17 additions & 0 deletions beater/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func TestUnpackConfig(t *testing.T) {
MaxRetries: 3,
Backoff: elasticsearch.DefaultBackoffConfig,
},
Metadata: []SourceMapMetadata{},
esConfigured: true,
},
LibraryPattern: "^custom",
Expand Down Expand Up @@ -295,6 +296,14 @@ func TestUnpackConfig(t *testing.T) {
"rum": map[string]interface{}{
"enabled": true,
"source_mapping": map[string]interface{}{
"metadata": []map[string]string{
{
"service.name": "opbeans-rum",
"service.version": "1.2.3",
"bundle.filepath": "/test/e2e/general-usecase/bundle.js.map",
"sourcemap.url": "http://somewhere.com/bundle.js.map",
},
},
"cache": map[string]interface{}{
"expiration": 7,
},
Expand Down Expand Up @@ -376,6 +385,14 @@ func TestUnpackConfig(t *testing.T) {
},
IndexPattern: "apm-*-sourcemap*",
ESConfig: elasticsearch.DefaultConfig(),
Metadata: []SourceMapMetadata{
{
ServiceName: "opbeans-rum",
ServiceVersion: "1.2.3",
BundleFilepath: "/test/e2e/general-usecase/bundle.js.map",
SourceMapURL: "http://somewhere.com/bundle.js.map",
},
},
},
LibraryPattern: "rum",
ExcludeFromGrouping: "^/webpack",
Expand Down
7 changes: 5 additions & 2 deletions beater/config/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/kibana"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
)

func NewIntegrationConfig(rootConfig *common.Config) (*IntegrationConfig, error) {
Expand Down Expand Up @@ -66,5 +66,8 @@ type Package struct {
}

type Fleet struct {
Kibana kibana.ClientConfig `config:"kibana"`
Hosts []string `config:"hosts"`
Protocol string `config:"protocol"`
AccessAPIKey string `config:"access_api_key"`
TLS *tlscommon.Config `config:"ssl"`
}
22 changes: 10 additions & 12 deletions beater/config/rum.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ type EventRate struct {
LruSize int `config:"lru_size"`
}

// SourceMapping holds sourecemap config information
// SourceMapping holds sourcemap config information
type SourceMapping struct {
Cache Cache `config:"cache"`
Enabled bool `config:"enabled"`
IndexPattern string `config:"index_pattern"`
ESConfig *elasticsearch.Config `config:"elasticsearch"`
Metadata []SourceMapMetadata `config:"metadata"`
esConfigured bool
}

Expand All @@ -79,14 +80,13 @@ func (c *RumConfig) setup(log *logp.Logger, dataStreamsEnabled bool, outputESCfg
return errors.Wrapf(err, "Invalid regex for `exclude_from_grouping`: ")
}

var apiKey string
if c.SourceMapping.esConfigured {
if dataStreamsEnabled {
// when running under Fleet, the only setting configured is the api key
apiKey = c.SourceMapping.ESConfig.APIKey
} else {
return nil
}
if c.SourceMapping.esConfigured && len(c.SourceMapping.Metadata) > 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work once a new package with the changes in apmpackage/apm/agent/input/template.yml.hbs is published

return errors.New("configuring both source_mapping.elasticsearch and sourcemapping.source_maps not allowed")
}

// No need to unpack the ESConfig if SourceMapMetadata exist
if len(c.SourceMapping.Metadata) > 0 {
return nil
}

// fall back to elasticsearch output configuration for sourcemap storage if possible
Expand All @@ -98,9 +98,6 @@ func (c *RumConfig) setup(log *logp.Logger, dataStreamsEnabled bool, outputESCfg
if err := outputESCfg.Unpack(c.SourceMapping.ESConfig); err != nil {
return errors.Wrap(err, "unpacking Elasticsearch config into Sourcemap config")
}
if c.SourceMapping.ESConfig.APIKey == "" {
c.SourceMapping.ESConfig.APIKey = apiKey
}
return nil
}

Expand All @@ -119,6 +116,7 @@ func defaultSourcemapping() SourceMapping {
Cache: Cache{Expiration: defaultSourcemapCacheExpiration},
IndexPattern: defaultSourcemapIndexPattern,
ESConfig: elasticsearch.DefaultConfig(),
Metadata: []SourceMapMetadata{},
}
}

Expand Down
Loading