|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.logging; |
| 18 | + |
| 19 | +import static org.easymock.EasyMock.anyObject; |
| 20 | +import static org.easymock.EasyMock.capture; |
| 21 | +import static org.easymock.EasyMock.createMock; |
| 22 | +import static org.easymock.EasyMock.expect; |
| 23 | +import static org.easymock.EasyMock.newCapture; |
| 24 | +import static org.easymock.EasyMock.replay; |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNull; |
| 27 | + |
| 28 | +import com.google.api.core.ApiFutures; |
| 29 | +import com.google.cloud.MonitoredResource; |
| 30 | +import com.google.cloud.logging.HttpRequest.RequestMethod; |
| 31 | +import com.google.cloud.logging.Logging.WriteOption; |
| 32 | +import com.google.cloud.logging.spi.LoggingRpcFactory; |
| 33 | +import com.google.cloud.logging.spi.v2.LoggingRpc; |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | +import com.google.logging.v2.WriteLogEntriesRequest; |
| 36 | +import com.google.logging.v2.WriteLogEntriesResponse; |
| 37 | +import org.easymock.Capture; |
| 38 | +import org.junit.After; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +public class AutoPopulateMetadataTests { |
| 43 | + |
| 44 | + private static final String LOG_NAME = "test-log"; |
| 45 | + private static final String RESOURCE_PROJECT_ID = "env-project-id"; |
| 46 | + private static final String LOGGING_PROJECT_ID = "log-project-id"; |
| 47 | + private static final MonitoredResource RESOURCE = |
| 48 | + MonitoredResource.newBuilder("global") |
| 49 | + .addLabel(MonitoredResourceUtil.PORJECTID_LABEL, RESOURCE_PROJECT_ID) |
| 50 | + .build(); |
| 51 | + private static final LogEntry SIMPLE_LOG_ENTRY = |
| 52 | + LogEntry.newBuilder(Payload.StringPayload.of("hello")) |
| 53 | + .setLogName(LOG_NAME) |
| 54 | + .setDestination(LogDestinationName.project(LOGGING_PROJECT_ID)) |
| 55 | + .build(); |
| 56 | + private static final LogEntry SIMPLE_LOG_ENTRY_WITH_DEBUG = |
| 57 | + LogEntry.newBuilder(Payload.StringPayload.of("hello")) |
| 58 | + .setLogName(LOG_NAME) |
| 59 | + .setSeverity(Severity.DEBUG) |
| 60 | + .setDestination(LogDestinationName.project(LOGGING_PROJECT_ID)) |
| 61 | + .build(); |
| 62 | + private static final WriteLogEntriesResponse EMPTY_WRITE_RESPONSE = |
| 63 | + WriteLogEntriesResponse.newBuilder().build(); |
| 64 | + private static final HttpRequest HTTP_REQUEST = |
| 65 | + HttpRequest.newBuilder() |
| 66 | + .setRequestMethod(RequestMethod.GET) |
| 67 | + .setRequestUrl("https://example.com") |
| 68 | + .setUserAgent("Test User Agent") |
| 69 | + .build(); |
| 70 | + private static final String TRACE_ID = "01010101010101010101010101010101"; |
| 71 | + private static final String FORMATTED_TRACE_ID = |
| 72 | + String.format(LoggingImpl.RESOURCE_NAME_FORMAT, RESOURCE_PROJECT_ID, TRACE_ID); |
| 73 | + private static final String SPAN_ID = "1"; |
| 74 | + |
| 75 | + private LoggingRpcFactory mockedRpcFactory; |
| 76 | + private LoggingRpc mockedRpc; |
| 77 | + private Logging logging; |
| 78 | + private Capture<WriteLogEntriesRequest> rpcWriteArgument = newCapture(); |
| 79 | + private ResourceTypeEnvironmentGetter mockedEnvGetter; |
| 80 | + |
| 81 | + @Before |
| 82 | + public void setup() { |
| 83 | + mockedEnvGetter = createMock(ResourceTypeEnvironmentGetter.class); |
| 84 | + mockedRpcFactory = createMock(LoggingRpcFactory.class); |
| 85 | + mockedRpc = createMock(LoggingRpc.class); |
| 86 | + expect(mockedRpcFactory.create(anyObject(LoggingOptions.class))) |
| 87 | + .andReturn(mockedRpc) |
| 88 | + .anyTimes(); |
| 89 | + expect(mockedRpc.write(capture(rpcWriteArgument))) |
| 90 | + .andReturn(ApiFutures.immediateFuture(EMPTY_WRITE_RESPONSE)); |
| 91 | + MonitoredResourceUtil.setEnvironmentGetter(mockedEnvGetter); |
| 92 | + // the following mocks generate MonitoredResource instance same as RESOURCE constant |
| 93 | + expect(mockedEnvGetter.getAttribute("project/project-id")).andStubReturn(RESOURCE_PROJECT_ID); |
| 94 | + expect(mockedEnvGetter.getAttribute("")).andStubReturn(null); |
| 95 | + replay(mockedRpcFactory, mockedRpc, mockedEnvGetter); |
| 96 | + |
| 97 | + LoggingOptions options = |
| 98 | + LoggingOptions.newBuilder() |
| 99 | + .setProjectId(RESOURCE_PROJECT_ID) |
| 100 | + .setServiceRpcFactory(mockedRpcFactory) |
| 101 | + .build(); |
| 102 | + logging = options.getService(); |
| 103 | + } |
| 104 | + |
| 105 | + @After |
| 106 | + public void teardown() { |
| 107 | + (new ContextHandler()).removeCurrentContext(); |
| 108 | + } |
| 109 | + |
| 110 | + private void mockCurrentContext(HttpRequest request, String traceId, String spanId) { |
| 111 | + Context mockedContext = |
| 112 | + Context.newBuilder().setRequest(request).setTraceId(traceId).setSpanId(spanId).build(); |
| 113 | + (new ContextHandler()).setCurrentContext(mockedContext); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAutoPopulationEnabledInLoggingOptions() { |
| 118 | + mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID); |
| 119 | + |
| 120 | + logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY)); |
| 121 | + |
| 122 | + LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0)); |
| 123 | + assertEquals(HTTP_REQUEST, actual.getHttpRequest()); |
| 124 | + assertEquals(FORMATTED_TRACE_ID, actual.getTrace()); |
| 125 | + assertEquals(SPAN_ID, actual.getSpanId()); |
| 126 | + assertEquals(RESOURCE, actual.getResource()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions() { |
| 131 | + // redefine logging option to opt out auto-populating |
| 132 | + LoggingOptions options = |
| 133 | + logging.getOptions().toBuilder().setAutoPopulateMetadata(false).build(); |
| 134 | + logging = options.getService(); |
| 135 | + mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID); |
| 136 | + |
| 137 | + logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(true)); |
| 138 | + |
| 139 | + LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0)); |
| 140 | + assertEquals(HTTP_REQUEST, actual.getHttpRequest()); |
| 141 | + assertEquals(FORMATTED_TRACE_ID, actual.getTrace()); |
| 142 | + assertEquals(SPAN_ID, actual.getSpanId()); |
| 143 | + assertEquals(RESOURCE, actual.getResource()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testAutoPopulationDisabledInWriteOptions() { |
| 148 | + mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID); |
| 149 | + |
| 150 | + logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(false)); |
| 151 | + |
| 152 | + LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0)); |
| 153 | + assertNull(actual.getHttpRequest()); |
| 154 | + assertNull(actual.getTrace()); |
| 155 | + assertNull(actual.getSpanId()); |
| 156 | + assertNull(actual.getResource()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testSourceLocationPopulation() { |
| 161 | + SourceLocation expected = SourceLocation.fromCurrentContext(0); |
| 162 | + logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY_WITH_DEBUG)); |
| 163 | + |
| 164 | + LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0)); |
| 165 | + assertEquals(expected.getFile(), actual.getSourceLocation().getFile()); |
| 166 | + assertEquals(expected.getClass(), actual.getSourceLocation().getClass()); |
| 167 | + assertEquals(expected.getFunction(), actual.getSourceLocation().getFunction()); |
| 168 | + assertEquals(new Long(expected.getLine() + 1), actual.getSourceLocation().getLine()); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testNotFormattedTraceId() { |
| 173 | + mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID); |
| 174 | + |
| 175 | + final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build(); |
| 176 | + |
| 177 | + logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.resource(expectedResource)); |
| 178 | + |
| 179 | + LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0)); |
| 180 | + assertEquals(TRACE_ID, actual.getTrace()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testMonitoredResourcePopulationInWriteOptions() { |
| 185 | + mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID); |
| 186 | + |
| 187 | + final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build(); |
| 188 | + |
| 189 | + logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.resource(expectedResource)); |
| 190 | + |
| 191 | + LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0)); |
| 192 | + assertEquals(expectedResource, actual.getResource()); |
| 193 | + } |
| 194 | +} |
0 commit comments