Skip to content

Add possibility to use google credentials file from file referenced in ENV variable #74

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
Expand Down Expand Up @@ -57,9 +57,11 @@ public GoogleCloudLoggingSink(GoogleCloudLoggingSinkOptions sinkOptions, ITextFo
}

// logging client for google cloud apis
_client = _sinkOptions.GoogleCredentialJson.IsNullOrWhiteSpace()
var clientBuilder = CreateBuilderBasedOnCredentials(_sinkOptions);

_client = clientBuilder == null
? LoggingServiceV2Client.Create()
: new LoggingServiceV2ClientBuilder { JsonCredentials = _sinkOptions.GoogleCredentialJson }.Build();
: clientBuilder.Build();
}

public Task EmitBatchAsync(IEnumerable<LogEvent> events)
Expand Down Expand Up @@ -152,5 +154,26 @@ private static string GetLogEntryMessage(LogEntry logEntry)
_ => LogSeverity.Default
};

private static LoggingServiceV2ClientBuilder? CreateBuilderBasedOnCredentials(GoogleCloudLoggingSinkOptions sinkOptions)
{
string? credentialsFilePath = null;
if (!sinkOptions.GoogleCredentialsEnvironmentVariableName.IsNullOrWhiteSpace())
{
credentialsFilePath = Environment.GetEnvironmentVariable(sinkOptions.GoogleCredentialsEnvironmentVariableName);
}

LoggingServiceV2ClientBuilder? clientBuilder = null;
if (!sinkOptions.GoogleCredentialJson.IsNullOrWhiteSpace())
{
clientBuilder = new LoggingServiceV2ClientBuilder { JsonCredentials = sinkOptions.GoogleCredentialJson };
}
else if (!credentialsFilePath.IsNullOrWhiteSpace())
{
clientBuilder = new LoggingServiceV2ClientBuilder { CredentialsPath = credentialsFilePath };
}

return clientBuilder;
}

public Task OnEmptyBatchAsync() => Task.CompletedTask;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using Serilog.Configuration;
using Serilog.Core;
Expand Down Expand Up @@ -67,6 +67,7 @@ public static LoggerConfiguration GoogleCloudLogging(
bool useSourceContextAsLogName = true,
bool useLogCorrelation = true,
string? googleCredentialJson = null,
string? googleCredentialsEnvironmentVariableName = null,
string? serviceName = null,
string? serviceVersion = null,
int? batchSizeLimit = null,
Expand All @@ -87,7 +88,8 @@ public static LoggerConfiguration GoogleCloudLogging(
useLogCorrelation,
googleCredentialJson,
serviceName,
serviceVersion
serviceVersion,
googleCredentialsEnvironmentVariableName
);

return loggerConfiguration.GoogleCloudLogging(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace Serilog.Sinks.GoogleCloudLogging;

Expand Down Expand Up @@ -50,6 +50,11 @@ public class GoogleCloudLoggingSinkOptions
/// </summary>
public string? GoogleCredentialJson { get; set; }

/// <summary>
/// File path with Content of Google Cloud JSON credentials to override using Application Default credentials.
/// </summary>
public string? GoogleCredentialsEnvironmentVariableName { get; set; }

/// <summary>
/// Attach service name to log entries (added as `serviceContext.service` metadata in `jsonPayload`).
/// Required for logged exceptions to be forwarded to StackDriver Error Reporting.
Expand Down Expand Up @@ -115,7 +120,8 @@ public GoogleCloudLoggingSinkOptions(
bool useLogCorrelation = true,
string? googleCredentialJson = null,
string? serviceName = null,
string? serviceVersion = null)
string? serviceVersion = null,
string? googleCredentialsEnvironmentVariableName = null)
{
ProjectId = projectId;
ResourceType = resourceType;
Expand All @@ -134,5 +140,6 @@ public GoogleCloudLoggingSinkOptions(
GoogleCredentialJson = googleCredentialJson;
ServiceName = serviceName;
ServiceVersion = serviceVersion;
GoogleCredentialsEnvironmentVariableName = googleCredentialsEnvironmentVariableName;
}
}