Skip to content

Commit

Permalink
[exporter/elasticsearch] Add telemetry settings
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonip committed Jul 2, 2024
1 parent fc9fee4 commit 4502692
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
10 changes: 10 additions & 0 deletions exporter/elasticsearchexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ Settings related to node discovery are:

Node discovery can be disabled by setting `discover.interval` to 0.

### Telemetry settings

The Elasticsearch Exporter's own telemetry settings for testing and debugging purposes.

⚠️ This is experimental and may change at any time.

- `telemetry`:
- `log_request_body` (default=false): Logs Elasticsearch client request body.
- `log_response_body` (default=false): Logs Elasticsearch client response body.

## Exporting metrics

Metrics support is currently in development.
Expand Down
9 changes: 9 additions & 0 deletions exporter/elasticsearchexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ type Config struct {
Flush FlushSettings `mapstructure:"flush"`
Mapping MappingsSettings `mapstructure:"mapping"`
LogstashFormat LogstashFormatSettings `mapstructure:"logstash_format"`

// TelemetrySettings contains settings useful for testing/debugging purposes
// This is experimental and may change at any time.
TelemetrySettings `mapstructure:"telemetry"`
}

type TelemetrySettings struct {
LogRequestBody bool `mapstructure:"log_request_body"`
LogResponseBody bool `mapstructure:"log_response_body"`
}

type LogstashFormatSettings struct {
Expand Down
37 changes: 28 additions & 9 deletions exporter/elasticsearchexporter/elasticsearch_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ type esBulkIndexerItem = docappender.BulkIndexerItem

// clientLogger implements the estransport.Logger interface
// that is required by the Elasticsearch client for logging.
type clientLogger zap.Logger
type clientLogger struct {
*zap.Logger
logRequestBody bool
logResponseBody bool
}

// LogRoundTrip should not modify the request or response, except for consuming and closing the body.
// Implementations have to check for nil values in request and response.
func (cl *clientLogger) LogRoundTrip(requ *http.Request, resp *http.Response, err error, _ time.Time, dur time.Duration) error {
zl := (*zap.Logger)(cl)
zl := cl.Logger
switch {
case err == nil && resp != nil:
zl.Debug("Request roundtrip completed.",
Expand All @@ -50,19 +54,28 @@ func (cl *clientLogger) LogRoundTrip(requ *http.Request, resp *http.Response, er
zl.Error("Request failed.", zap.NamedError("reason", err))
}

if cl.logRequestBody && requ != nil {
if b, err := io.ReadAll(requ.Body); err == nil {
zl.Debug("Request body", zap.ByteString("body", b))
}
}
if cl.logResponseBody && resp != nil {
if b, err := io.ReadAll(resp.Body); err == nil {
zl.Debug("Response body", zap.ByteString("body", b))
}
}

return nil
}

// RequestBodyEnabled makes the client pass a copy of request body to the logger.
func (*clientLogger) RequestBodyEnabled() bool {
// TODO: introduce setting log the bodies for more detailed debug logs
return false
func (cl *clientLogger) RequestBodyEnabled() bool {
return cl.logRequestBody
}

// ResponseBodyEnabled makes the client pass a copy of response body to the logger.
func (*clientLogger) ResponseBodyEnabled() bool {
// TODO: introduce setting log the bodies for more detailed debug logs
return false
func (cl *clientLogger) ResponseBodyEnabled() bool {
return cl.logResponseBody
}

func newElasticsearchClient(
Expand Down Expand Up @@ -97,6 +110,12 @@ func newElasticsearchClient(
return nil, err
}

esLogger := clientLogger{
Logger: telemetry.Logger,
logRequestBody: config.LogRequestBody,
logResponseBody: config.LogResponseBody,
}

return elasticsearch7.NewClient(esConfigCurrent{
Transport: httpClient.Transport,

Expand All @@ -122,7 +141,7 @@ func newElasticsearchClient(
// configure internal metrics reporting and logging
EnableMetrics: false, // TODO
EnableDebugLogger: false, // TODO
Logger: (*clientLogger)(telemetry.Logger),
Logger: &esLogger,
})
}

Expand Down

0 comments on commit 4502692

Please sign in to comment.