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

Make it easy to log elastic requests #8183

Closed
erik-kallen opened this issue May 8, 2024 · 3 comments
Closed

Make it easy to log elastic requests #8183

erik-kallen opened this issue May 8, 2024 · 3 comments
Labels
8.x Relates to 8.x client version Category: Question

Comments

@erik-kallen
Copy link

Is your feature request related to a problem? Please describe.
I want to log requests to Elastic, primarily for development/debug purposes

Describe the solution you'd like
It could either happen automatically (for example with a Logger property on ElasticSearchClientSettings), or through some callbacks.

Describe alternatives you've considered
I'm currently using this rather horrible hack:

private class LoggingHttpTransport(IElasticsearchClientSettings settings, ILogger logger) : DefaultHttpTransport<IElasticsearchClientSettings>(settings)
{
    public override TResponse Request<TResponse>(HttpMethod method, string path, PostData? data, RequestParameters? requestParameters, in OpenTelemetryData openTelemetryData)
    {
        LogRequest(method, path, data);
        return base.Request<TResponse>(method, path, data, requestParameters, in openTelemetryData);
    }

    public override Task<TResponse> RequestAsync<TResponse>(HttpMethod method, string path, PostData? data, RequestParameters? requestParameters, in OpenTelemetryData openTelemetryData, CancellationToken cancellationToken = default)
    {
        LogRequest(method, path, data);
        return base.RequestAsync<TResponse>(method, path, data, requestParameters, in openTelemetryData, cancellationToken);
    }

    private void LogRequest(HttpMethod method, string path, PostData? data)
    {
        if (data != null)
        {
            using var ms = new MemoryStream();
            data.Write(ms, Settings);
            ms.Position = 0;
            using var reader = new StreamReader(ms);
            var serializedBody = reader.ReadToEnd();
            logger.LogDebug("Calling elastic {Method} {Url}: {Body}", method, path, serializedBody);
        }
        else
        {
            logger.LogDebug("Calling elastic {Method} {Url}", method, path);
        }
    }
}

combined with this:

private static ElasticsearchClient CreateElasticSearchClient(ElasticsearchClientSettings settings, ILogger logger)
{
    var constructor = typeof(ElasticsearchClient).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, [typeof(HttpTransport<IElasticsearchClientSettings>)]);
    return (ElasticsearchClient)constructor!.Invoke([new LoggingHttpTransport(settings, logger)]);
}
@flobernd flobernd added 8.x Relates to 8.x client version Category: Question and removed Category: Feature labels May 8, 2024
@flobernd
Copy link
Member

flobernd commented May 8, 2024

Hi @erik-kallen,

there are multiple ways of logging ES queries.

  1. Elastic.Clients.Elasticsearch supports OpenTelemetry
  2. A debug callback can be registered:
var settings = new ElasticsearchClientSettings(new Uri("..."))
    .EnableDebugMode(cd =>
    {
        Console.WriteLine(cd.DebugInformation);
    });

Does that answer your question?

@erik-kallen
Copy link
Author

I don't think EnableDebugMode gives me enough customizability, but I'll look into Telemetry, thank you!

@flobernd
Copy link
Member

flobernd commented May 9, 2024

Happy to help! I'm closing this for now. Feel free to open another issue if you have more questions, or if the provided methods are not sufficient for your use-case.

@flobernd flobernd closed this as completed May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.x Relates to 8.x client version Category: Question
Projects
None yet
Development

No branches or pull requests

2 participants