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 8 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 @@ -244,9 +244,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 allowed if apm-server is being
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
# 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 @@ -244,9 +244,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 allowed 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 @@ -244,9 +244,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 allowed 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
29 changes: 25 additions & 4 deletions beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package beater
import (
"context"
"net"
"net/http"
"regexp"
"runtime"
"strings"
Expand All @@ -30,6 +31,7 @@ import (

"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 @@ -606,7 +608,7 @@ func newTransformConfig(beatInfo beat.Info, cfg *config.Config) (*transform.Conf
},
}

if cfg.RumConfig.Enabled && cfg.RumConfig.SourceMapping.Enabled && cfg.RumConfig.SourceMapping.ESConfig != nil {
if cfg.RumConfig.Enabled && cfg.RumConfig.SourceMapping.Enabled {
store, err := newSourcemapStore(beatInfo, cfg.RumConfig.SourceMapping)
if err != nil {
return nil, err
Expand All @@ -618,12 +620,31 @@ func newTransformConfig(beatInfo beat.Info, cfg *config.Config) (*transform.Conf
}

func newSourcemapStore(beatInfo beat.Info, cfg config.SourceMapping) (*sourcemap.Store, error) {
esClient, err := elasticsearch.NewClient(cfg.ESConfig)
logger := logp.NewLogger(logs.Sourcemap)
if len(cfg.SourceMapConfigs) == 0 {
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
esClient, err := elasticsearch.NewClient(cfg.ESConfig)
if err != nil {
return nil, err
}
index := strings.ReplaceAll(cfg.IndexPattern, "%{[observer.version]}", beatInfo.Version)
b := sourcemap.NewESStore(esClient, index, logger)

return sourcemap.NewStore(b, logger, cfg.Cache.Expiration)
}

// make a copy of the default http client, configure a Transport to be
// the same as the esClient, instrument it, and set it on the http
// client.
c := *http.DefaultClient
tr, err := elasticsearch.NewHTTPTransport(cfg.ESConfig)
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
index := strings.ReplaceAll(cfg.IndexPattern, "%{[observer.version]}", beatInfo.Version)
return sourcemap.NewStore(esClient, index, cfg.Cache.Expiration)

c.Transport = apmhttp.WrapRoundTripper(tr)
b := sourcemap.NewFleetStore(cfg.ESConfig.APIKey, cfg.SourceMapConfigs, &c)

return sourcemap.NewStore(b, logger, cfg.Cache.Expiration)
}

// WrapRunServerWithProcessors wraps runServer such that it wraps args.Reporter
Expand Down
8 changes: 6 additions & 2 deletions beater/config/agentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ func (s *AgentConfig) setup() error {
type Service struct {
Name string `config:"name"`
Environment string `config:"environment"`
Version string `config:"version"`
}

// String implements the Stringer interface.
func (s *Service) String() string {
var name, env string
var name, env, version string
if s.Name != "" {
name = "service.name=" + s.Name
}
if s.Environment != "" {
env = "service.environment=" + s.Environment
}
return strings.Join([]string{name, env}, " ")
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
if s.Version != "" {
version = "service.version=" + s.Version
}
return strings.Join([]string{name, env, version}, " ")
}
8 changes: 5 additions & 3 deletions beater/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func TestUnpackConfig(t *testing.T) {
MaxRetries: 3,
Backoff: elasticsearch.DefaultBackoffConfig,
},
esConfigured: true,
SourceMapConfigs: []SourceMapConfig{},
esConfigured: true,
},
LibraryPattern: "^custom",
ExcludeFromGrouping: "^grouping",
Expand Down Expand Up @@ -363,8 +364,9 @@ func TestUnpackConfig(t *testing.T) {
Cache: Cache{
Expiration: 7 * time.Second,
},
IndexPattern: "apm-*-sourcemap*",
ESConfig: elasticsearch.DefaultConfig(),
IndexPattern: "apm-*-sourcemap*",
ESConfig: elasticsearch.DefaultConfig(),
SourceMapConfigs: []SourceMapConfig{},
},
LibraryPattern: "rum",
ExcludeFromGrouping: "^/webpack",
Expand Down
29 changes: 20 additions & 9 deletions beater/config/rum.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ type EventRate struct {

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

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

if c.SourceMapping.esConfigured && len(c.SourceMapping.SourceMapConfigs) > 0 {
return errors.New("configuring both source_mapping.elasticsearch and sourcemapping.source_maps not allowed")
}

// No need to unpack the ESConfig if we've set SourceMapConfigs
if len(c.SourceMapping.SourceMapConfigs) > 0 {
return nil
}

var apiKey string
if c.SourceMapping.esConfigured {
if dataStreamsEnabled {
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -115,10 +125,11 @@ func (s *SourceMapping) Unpack(inp *common.Config) error {

func defaultSourcemapping() SourceMapping {
return SourceMapping{
Enabled: true,
Cache: Cache{Expiration: defaultSourcemapCacheExpiration},
IndexPattern: defaultSourcemapIndexPattern,
ESConfig: elasticsearch.DefaultConfig(),
Enabled: true,
Cache: Cache{Expiration: defaultSourcemapCacheExpiration},
IndexPattern: defaultSourcemapIndexPattern,
ESConfig: elasticsearch.DefaultConfig(),
SourceMapConfigs: []SourceMapConfig{},
}
}

Expand Down
32 changes: 32 additions & 0 deletions beater/config/sourcemapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package config

type SourceMapConfig struct {
Service Service `config:"service"`
stuartnelson3 marked this conversation as resolved.
Show resolved Hide resolved
Bundle Bundle `config:"bundle"`
SourceMap SourceMap `config:"sourcemap"`
}

type Bundle struct {
Filepath string `config:"filepath"`
}

type SourceMap struct {
URL string `config:"url"`
}
5 changes: 3 additions & 2 deletions elasticsearch/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func connectionConfig(config *Config) (http.RoundTripper, []string, http.Header,
if err != nil {
return nil, nil, nil, err
}
transp, err := httpTransport(config)
transp, err := NewHTTPTransport(config)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -134,7 +134,8 @@ func addresses(cfg *Config) ([]string, error) {
return addresses, nil
}

func httpTransport(cfg *Config) (*http.Transport, error) {
// NewHttpTransport configures an *http.Transport according to a *Config
func NewHTTPTransport(cfg *Config) (*http.Transport, error) {
proxy, err := httpProxyURL(cfg)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion model/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

logs "github.com/elastic/apm-server/log"
"github.com/elastic/apm-server/sourcemap"
"github.com/elastic/apm-server/sourcemap/test"
"github.com/elastic/apm-server/tests"
"github.com/elastic/apm-server/transform"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
)

func baseException() *Exception {
Expand Down Expand Up @@ -805,7 +807,8 @@ func TestSourcemapping(t *testing.T) {
assert.Equal(t, 1, *event.Exception.Stacktrace[0].Lineno)

// transform with sourcemap store
store, err := sourcemap.NewStore(test.ESClientWithValidSourcemap(t), "apm-*sourcemap*", time.Minute)
b := sourcemap.NewESStore(test.ESClientWithValidSourcemap(t), "apm-*sourcemap*", logp.NewLogger(logs.Sourcemap))
store, err := sourcemap.NewStore(b, logp.NewLogger(logs.Sourcemap), time.Minute)
require.NoError(t, err)
transformedWithSourcemap := event.fields(context.Background(), &transform.Config{
RUM: transform.RUMConfig{SourcemapStore: store},
Expand Down
3 changes: 2 additions & 1 deletion model/sourcemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func TestInvalidateCache(t *testing.T) {
// create sourcemap store
client, err := estest.NewElasticsearchClient(estest.NewTransport(t, http.StatusOK, nil))
require.NoError(t, err)
store, err := sourcemap.NewStore(client, "foo", time.Minute)
b := sourcemap.NewESStore(client, "foo", logp.NewLogger(logs.Sourcemap))
store, err := sourcemap.NewStore(b, logp.NewLogger(logs.Sourcemap), time.Minute)
require.NoError(t, err)

// transform with sourcemap store
Expand Down
6 changes: 5 additions & 1 deletion model/stacktrace_frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (
"github.com/stretchr/testify/require"

"github.com/elastic/apm-server/elasticsearch"
logs "github.com/elastic/apm-server/log"

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

"github.com/elastic/apm-server/sourcemap"
"github.com/elastic/apm-server/sourcemap/test"
Expand Down Expand Up @@ -422,7 +424,9 @@ func TestLibraryFrame(t *testing.T) {
}

func testSourcemapStore(t *testing.T, client elasticsearch.Client) *sourcemap.Store {
store, err := sourcemap.NewStore(client, "apm-*sourcemap*", time.Minute)
logger := logp.NewLogger(logs.Sourcemap)
b := sourcemap.NewESStore(client, "apm-*sourcemap*", logger)
store, err := sourcemap.NewStore(b, logger, time.Minute)
require.NoError(t, err)
return store
}
13 changes: 10 additions & 3 deletions sourcemap/es_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ var (
errSourcemapWrongFormat = errors.New("Sourcemapping ES Result not in expected format")
)

type esStore struct {
// ESStore handles making sourcemap requests to an ElasticSearch backend.
type ESStore struct {
client elasticsearch.Client
index string
logger *logp.Logger
Expand All @@ -65,7 +66,13 @@ type esSourcemapResponse struct {
} `json:"hits"`
}

func (s *esStore) fetch(ctx context.Context, name, version, path string) (string, error) {
// NewESStore returns an instance of ESStore for interacting with sourcemaps
// stored in ElasticSearch.
func NewESStore(c elasticsearch.Client, index string, logger *logp.Logger) *ESStore {
return &ESStore{c, index, logger}
}

func (s *ESStore) fetch(ctx context.Context, name, version, path string) (string, error) {
statusCode, body, err := s.runSearchQuery(ctx, name, version, path)
if err != nil {
return "", errors.Wrap(err, errMsgESFailure)
Expand All @@ -87,7 +94,7 @@ func (s *esStore) fetch(ctx context.Context, name, version, path string) (string
return parse(body, name, version, path, s.logger)
}

func (s *esStore) runSearchQuery(ctx context.Context, name, version, path string) (int, io.ReadCloser, error) {
func (s *ESStore) runSearchQuery(ctx context.Context, name, version, path string) (int, io.ReadCloser, error) {
// build and encode the query
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(query(name, version, path)); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions sourcemap/es_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/elastic/apm-server/sourcemap/test"
)

func Test_esFetcher_fetchError(t *testing.T) {
func Test_ESFetcher_fetchError(t *testing.T) {
for name, tc := range map[string]struct {
statusCode int
esBody map[string]interface{}
Expand Down Expand Up @@ -77,7 +77,7 @@ func Test_esFetcher_fetchError(t *testing.T) {
}
}

func Test_esFetcher_fetch(t *testing.T) {
func Test_ESFetcher_fetch(t *testing.T) {
for name, tc := range map[string]struct {
client elasticsearch.Client
filePath string
Expand All @@ -101,6 +101,6 @@ func Test_esFetcher_fetch(t *testing.T) {
}
}

func testESStore(client elasticsearch.Client) *esStore {
return &esStore{client: client, index: "apm-sourcemap", logger: logp.NewLogger(logs.Sourcemap)}
func testESStore(client elasticsearch.Client) *ESStore {
return NewESStore(client, "apm-sourcemap", logp.NewLogger(logs.Sourcemap))
}
Loading