This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathRemoteLoggingHelper.java
More file actions
132 lines (118 loc) · 4.92 KB
/
Copy pathRemoteLoggingHelper.java
File metadata and controls
132 lines (118 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2016 Google LLC
*
* Licensed 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 com.google.cloud.logging.testing;
import com.google.api.gax.retrying.RetrySettings;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.logging.LoggingOptions;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Utility to create a remote logging configuration for testing. Logging options can be obtained via
* the {@link #getOptions()} method. Returned options have custom {@link
* LoggingOptions#getRetrySettings()}: {@link RetrySettings#getMaxRetryDelay()} is {@code 30000},
* {@link RetrySettings#getTotalTimeout()} is {@code 120000} and {@link
* RetrySettings#getInitialRetryDelay()} is {@code 250}.
*/
public class RemoteLoggingHelper {
private static final Logger log = Logger.getLogger(RemoteLoggingHelper.class.getName());
private final LoggingOptions options;
private RemoteLoggingHelper(LoggingOptions options) {
this.options = options;
}
/** Returns a {@link LoggingOptions} object to be used for testing. */
public LoggingOptions getOptions() {
return options;
}
/**
* Creates a {@code RemoteLoggingHelper} object for the given project id and JSON key input
* stream.
*
* @param projectId id of the project to be used for running the tests
* @param keyStream input stream for a JSON key
* @return A {@code RemoteLoggingHelper} object for the provided options
* @throws com.google.cloud.logging.testing.RemoteLoggingHelper.LoggingHelperException if {@code
* keyStream} is not a valid JSON key stream
*/
public static RemoteLoggingHelper create(String projectId, InputStream keyStream) {
try {
GrpcTransportOptions transportOptions = LoggingOptions.getDefaultGrpcTransportOptions();
LoggingOptions storageOptions =
LoggingOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(keyStream))
.setProjectId(projectId)
.setRetrySettings(retrySettings())
.setTransportOptions(transportOptions)
.build();
return new RemoteLoggingHelper(storageOptions);
} catch (IOException ex) {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, ex.getMessage());
}
throw LoggingHelperException.translate(ex);
}
}
/**
* Creates a {@code RemoteLoggingHelper} object using default project id and authentication
* credentials.
*/
public static RemoteLoggingHelper create() {
GrpcTransportOptions transportOptions = LoggingOptions.getDefaultGrpcTransportOptions();
LoggingOptions loggingOptions =
LoggingOptions.newBuilder()
.setRetrySettings(retrySettings())
.setTransportOptions(transportOptions)
.build();
return new RemoteLoggingHelper(loggingOptions);
}
/**
* Formats a resource name for testing purpose. This method appends a random UUID to the provided
* name. Prepends the Log name with the `java-logging` and the current instant time to allow for
* stale logs to be easily found and removed.
*
* <p>Format: java-logging_{INSTANT_IN_MILLIS}_{name}_{UUID}
*/
public static String formatForTest(String name) {
long nowInMillis = Instant.now().toEpochMilli();
return String.format(
"java-logging_%d_%s_%s", nowInMillis, name, UUID.randomUUID().toString().substring(0, 8));
}
private static RetrySettings retrySettings() {
return RetrySettings.newBuilder()
.setMaxRetryDelayDuration(Duration.ofMillis(30000L))
.setTotalTimeoutDuration(Duration.ofMillis(120000L))
.setInitialRetryDelayDuration(Duration.ofMillis(250L))
.setRetryDelayMultiplier(1.0)
.setInitialRpcTimeoutDuration(Duration.ofMillis(120000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeoutDuration(Duration.ofMillis(120000L))
.build();
}
public static class LoggingHelperException extends RuntimeException {
private static final long serialVersionUID = 2617749404172557196L;
public LoggingHelperException(String message, Throwable cause) {
super(message, cause);
}
public static LoggingHelperException translate(Exception ex) {
return new LoggingHelperException(ex.getMessage(), ex);
}
}
}