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 pathAutoPopulateMetadataTests.java
More file actions
206 lines (177 loc) · 8.3 KB
/
Copy pathAutoPopulateMetadataTests.java
File metadata and controls
206 lines (177 loc) · 8.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright 2021 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
*
* https://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;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.newCapture;
import static org.easymock.EasyMock.replay;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import com.google.api.core.ApiFutures;
import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.HttpRequest.RequestMethod;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.cloud.logging.spi.LoggingRpcFactory;
import com.google.cloud.logging.spi.v2.LoggingRpc;
import com.google.common.collect.ImmutableList;
import com.google.logging.v2.WriteLogEntriesRequest;
import com.google.logging.v2.WriteLogEntriesResponse;
import org.easymock.Capture;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class AutoPopulateMetadataTests {
private static final String LOG_NAME = "test-log";
private static final String RESOURCE_PROJECT_ID = "env-project-id";
private static final String LOGGING_PROJECT_ID = "log-project-id";
private static final MonitoredResource RESOURCE =
MonitoredResource.newBuilder("global")
.addLabel(MonitoredResourceUtil.PORJECTID_LABEL, RESOURCE_PROJECT_ID)
.build();
private static final LogEntry SIMPLE_LOG_ENTRY =
LogEntry.newBuilder(Payload.StringPayload.of("hello"))
.setLogName(LOG_NAME)
.setDestination(LogDestinationName.project(LOGGING_PROJECT_ID))
.build();
private static final LogEntry SIMPLE_LOG_ENTRY_WITH_DEBUG =
LogEntry.newBuilder(Payload.StringPayload.of("hello"))
.setLogName(LOG_NAME)
.setSeverity(Severity.DEBUG)
.setDestination(LogDestinationName.project(LOGGING_PROJECT_ID))
.build();
private static final WriteLogEntriesResponse EMPTY_WRITE_RESPONSE =
WriteLogEntriesResponse.getDefaultInstance();
private static final HttpRequest HTTP_REQUEST =
HttpRequest.newBuilder()
.setRequestMethod(RequestMethod.GET)
.setRequestUrl("https://example.com")
.setUserAgent("Test User Agent")
.build();
private static final String TRACE_ID = "01010101010101010101010101010101";
private static final String FORMATTED_TRACE_ID =
String.format(LoggingImpl.RESOURCE_NAME_FORMAT, RESOURCE_PROJECT_ID, TRACE_ID);
private static final String SPAN_ID = "1";
private static final boolean TRACE_SAMPLED = true;
private LoggingRpcFactory mockedRpcFactory;
private LoggingRpc mockedRpc;
private Logging logging;
private final Capture<WriteLogEntriesRequest> rpcWriteArgument = newCapture();
private ResourceTypeEnvironmentGetter mockedEnvGetter;
@BeforeEach
void setup() {
mockedEnvGetter = createMock(ResourceTypeEnvironmentGetter.class);
mockedRpcFactory = createMock(LoggingRpcFactory.class);
mockedRpc = createMock(LoggingRpc.class);
expect(mockedRpcFactory.create(anyObject(LoggingOptions.class)))
.andReturn(mockedRpc)
.anyTimes();
expect(mockedRpc.write(capture(rpcWriteArgument)))
.andReturn(ApiFutures.immediateFuture(EMPTY_WRITE_RESPONSE));
MonitoredResourceUtil.setEnvironmentGetter(mockedEnvGetter);
// the following mocks generate MonitoredResource instance same as RESOURCE
// constant
expect(mockedEnvGetter.getAttribute("project/project-id")).andStubReturn(RESOURCE_PROJECT_ID);
expect(mockedEnvGetter.getAttribute("")).andStubReturn(null);
replay(mockedRpcFactory, mockedRpc, mockedEnvGetter);
LoggingOptions options =
LoggingOptions.newBuilder()
.setProjectId(RESOURCE_PROJECT_ID)
.setServiceRpcFactory(mockedRpcFactory)
.build();
logging = options.getService();
}
@AfterEach
void teardown() {
new ContextHandler().removeCurrentContext();
}
private void mockCurrentContext(
HttpRequest request, String traceId, String spanId, boolean traceSampled) {
Context mockedContext =
Context.newBuilder()
.setRequest(request)
.setTraceId(traceId)
.setSpanId(spanId)
.setTraceSampled(traceSampled)
.build();
new ContextHandler().setCurrentContext(mockedContext);
}
@Test
void testAutoPopulationEnabledInLoggingOptions() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY));
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(HTTP_REQUEST, actual.getHttpRequest());
assertEquals(FORMATTED_TRACE_ID, actual.getTrace());
assertEquals(SPAN_ID, actual.getSpanId());
assertEquals(TRACE_SAMPLED, actual.getTraceSampled());
assertEquals(RESOURCE, actual.getResource());
}
@Test
void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions() {
// redefine logging option to opt out auto-populating
LoggingOptions options =
logging.getOptions().toBuilder().setAutoPopulateMetadata(false).build();
logging = options.getService();
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(true));
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(HTTP_REQUEST, actual.getHttpRequest());
assertEquals(FORMATTED_TRACE_ID, actual.getTrace());
assertEquals(SPAN_ID, actual.getSpanId());
assertEquals(TRACE_SAMPLED, actual.getTraceSampled());
assertEquals(RESOURCE, actual.getResource());
}
@Test
void testAutoPopulationDisabledInWriteOptions() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(false));
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertNull(actual.getHttpRequest());
assertNull(actual.getTrace());
assertNull(actual.getSpanId());
assertFalse(actual.getTraceSampled());
assertNull(actual.getResource());
}
@Test
void testSourceLocationPopulation() {
SourceLocation expected = SourceLocation.fromCurrentContext();
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY_WITH_DEBUG));
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(expected.getFile(), actual.getSourceLocation().getFile());
assertEquals(expected.getClass(), actual.getSourceLocation().getClass());
assertEquals(expected.getFunction(), actual.getSourceLocation().getFunction());
assertEquals(Long.valueOf(expected.getLine() + 1), actual.getSourceLocation().getLine());
}
@Test
void testNotFormattedTraceId() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build();
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.resource(expectedResource));
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(TRACE_ID, actual.getTrace());
}
@Test
void testMonitoredResourcePopulationInWriteOptions() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build();
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.resource(expectedResource));
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(expectedResource, actual.getResource());
}
}