-
-
Notifications
You must be signed in to change notification settings - Fork 312
Log request order #1237
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
base: 1.10.x
Are you sure you want to change the base?
Log request order #1237
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright The Microcks Authors. | ||
| * | ||
| * 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 io.github.microcks.domain; | ||
|
|
||
| import org.springframework.data.annotation.Id; | ||
| import org.springframework.data.mongodb.core.mapping.Document; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| @Document("invocations") | ||
| public class InvocationLogEntry { | ||
| @Id | ||
| private Long timestampEpoch; | ||
| private String serviceName; | ||
| /** | ||
| * | ||
| */ | ||
|
Comment on lines
+28
to
+30
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove unnecessary comments. |
||
| private String serviceVersion; | ||
| /** | ||
| * | ||
| */ | ||
| private String mockResponse; | ||
| /** | ||
| * | ||
| */ | ||
| private Date invocationTimestamp; | ||
| /** | ||
| * | ||
| */ | ||
| private long duration; | ||
| private String source; | ||
| private String requestId; | ||
|
|
||
| public InvocationLogEntry(Long timestampEpoch, String serviceName, String serviceVersion, String mockResponse, | ||
| Date invocationTimestamp, long duration, String source, String requestId) { | ||
| this.timestampEpoch = timestampEpoch; | ||
| this.serviceName = serviceName; | ||
| this.serviceVersion = serviceVersion; | ||
| this.mockResponse = mockResponse; | ||
| this.invocationTimestamp = invocationTimestamp; | ||
| this.duration = duration; | ||
| this.source = source; | ||
| this.requestId = requestId; | ||
| } | ||
|
|
||
| public InvocationLogEntry() { | ||
| } | ||
|
|
||
| public Long getTimestampEpoch() { | ||
| return timestampEpoch; | ||
| } | ||
|
|
||
| public void setTimestampEpoch(Long timestampEpoch) { | ||
| this.timestampEpoch = timestampEpoch; | ||
| } | ||
|
|
||
| public String getServiceName() { | ||
| return serviceName; | ||
| } | ||
|
|
||
| public void setServiceName(String serviceName) { | ||
| this.serviceName = serviceName; | ||
| } | ||
|
|
||
| public String getServiceVersion() { | ||
| return serviceVersion; | ||
| } | ||
|
|
||
| public void setServiceVersion(String serviceVersion) { | ||
| this.serviceVersion = serviceVersion; | ||
| } | ||
|
|
||
| public String getMockResponse() { | ||
| return mockResponse; | ||
| } | ||
|
|
||
| public void setMockResponse(String mockResponse) { | ||
| this.mockResponse = mockResponse; | ||
| } | ||
|
|
||
| public Date getInvocationTimestamp() { | ||
| return invocationTimestamp; | ||
| } | ||
|
|
||
| public void setInvocationTimestamp(Date invocationTimestamp) { | ||
| this.invocationTimestamp = invocationTimestamp; | ||
| } | ||
|
|
||
| public long getDuration() { | ||
| return duration; | ||
| } | ||
|
|
||
| public void setDuration(long duration) { | ||
| this.duration = duration; | ||
| } | ||
|
|
||
| public String getSource() { | ||
| return source; | ||
| } | ||
|
|
||
| public void setSource(String source) { | ||
| this.source = source; | ||
| } | ||
|
|
||
| public String getRequestId() { | ||
| return requestId; | ||
| } | ||
|
|
||
| public void setRequestId(String requestId) { | ||
| this.requestId = requestId; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ public class Operation { | |
|
|
||
| private Set<String> resourcePaths; | ||
| private List<ParameterConstraint> parameterConstraints; | ||
| private String idPath; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is actually the |
||
|
|
||
| public String getName() { | ||
| return name; | ||
|
|
@@ -162,4 +163,12 @@ public void addParameterConstraint(ParameterConstraint constraint) { | |
| } | ||
| parameterConstraints.add(constraint); | ||
| } | ||
|
|
||
| public void setIdPath(String idPath) { | ||
| this.idPath = idPath; | ||
| } | ||
|
|
||
| public String getIdPath() { | ||
| return idPath; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright The Microcks Authors. | ||
| * | ||
| * 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 io.github.microcks.listener; | ||
|
|
||
| import io.github.microcks.domain.InvocationLogEntry; | ||
| import io.github.microcks.event.MockInvocationEvent; | ||
| import io.github.microcks.repository.InvocationLogRepository; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.ApplicationListener; | ||
| import org.springframework.lang.NonNull; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Log invocations to database, listens to MockInvocationEvent, properties mocks.enable-invocation-logs and | ||
| * mocks.enable-invocation-stats need to be enabled to use this. | ||
| */ | ||
| @Component | ||
| @ConditionalOnProperty(name = "mocks.enable-invocation-logs", havingValue = "true") | ||
| public class InvocationAdvancedMetrics implements ApplicationListener<MockInvocationEvent> { | ||
|
|
||
| final InvocationLogRepository repo; | ||
|
|
||
| public InvocationAdvancedMetrics(InvocationLogRepository repo) { | ||
| this.repo = repo; | ||
| } | ||
|
|
||
| @Override | ||
| public void onApplicationEvent(@NonNull MockInvocationEvent event) { | ||
| repo.insert(new InvocationLogEntry(event.getTimestamp(), event.getServiceName(), event.getServiceVersion(), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace event.getTimestamp() with event.getInvocationTimestamp().getTime() to ensure a valid epoch value is passed to the log entry. |
||
| event.getMockResponse(), event.getInvocationTimestamp(), event.getDuration(), | ||
| event.getSource().getClass().getSimpleName(), event.getRequestId())); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportsAsyncExecution() { | ||
| return ApplicationListener.super.supportsAsyncExecution(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright The Microcks Authors. | ||
| * | ||
| * 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 io.github.microcks.repository; | ||
|
|
||
| import io.github.microcks.domain.InvocationLogEntry; | ||
| import org.springframework.data.mongodb.repository.MongoRepository; | ||
| import org.springframework.data.repository.NoRepositoryBean; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Repository for InvocationLogEntries | ||
| */ | ||
| @NoRepositoryBean | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This construct and way to define repository is different than the one we used for I think it could be nice to refactor reusing the same pattern here. What do you think? |
||
| public interface InvocationLogRepository extends MongoRepository<InvocationLogEntry, Long> { | ||
|
|
||
| /** | ||
| * find the latest invocation log entries from database | ||
| * @param service Service to query | ||
| * @param version Version of the service to query | ||
| * @param limit maximum number of entries | ||
| * @return List of log entries ordered by newest entry first | ||
| */ | ||
| List<InvocationLogEntry> findLastEntriesByServiceName(String service, String version, int limit); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright The Microcks Authors. | ||
| * | ||
| * 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 io.github.microcks.repository; | ||
|
|
||
| import io.github.microcks.domain.InvocationLogEntry; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.data.mongodb.core.MongoOperations; | ||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||
| import org.springframework.data.mongodb.core.query.Collation; | ||
| import org.springframework.data.mongodb.core.query.Criteria; | ||
| import org.springframework.data.mongodb.core.query.Query; | ||
| import org.springframework.data.mongodb.repository.query.MongoEntityInformation; | ||
| import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; | ||
| import org.springframework.lang.NonNull; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Implementation for InvocationLogRepository | ||
| */ | ||
| @Repository | ||
| public class InvocationLogRepositoryImpl extends SimpleMongoRepository<InvocationLogEntry, Long> | ||
| implements InvocationLogRepository { | ||
|
|
||
| @Autowired | ||
| private MongoTemplate mongoTemplate; | ||
|
|
||
| public InvocationLogRepositoryImpl(MongoOperations mongoOperations) { | ||
| super(new MongoEntityInformation<>() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment on interface definition. |
||
| @Override | ||
| @NonNull | ||
| public Class<InvocationLogEntry> getJavaType() { | ||
| return InvocationLogEntry.class; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isNew(@NonNull InvocationLogEntry entity) { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Long getId(@NonNull InvocationLogEntry entity) { | ||
| return entity.getInvocationTimestamp().toInstant().toEpochMilli(); | ||
| } | ||
|
|
||
| @Override | ||
| @NonNull | ||
| public Class<Long> getIdType() { | ||
| return Long.class; | ||
| } | ||
|
|
||
| @Override | ||
| @NonNull | ||
| public String getCollectionName() { | ||
| return "invocations"; | ||
| } | ||
|
|
||
| @Override | ||
| @NonNull | ||
| public String getIdAttribute() { | ||
| return "invocationTimestamp"; | ||
| } | ||
|
|
||
| @Override | ||
| public Collation getCollation() { | ||
| return null; | ||
| } | ||
| }, mongoOperations); | ||
| } | ||
|
|
||
| /** | ||
| * find the latest invocation log entries from database | ||
| * @param service Service to query | ||
| * @param version Version of the service to query | ||
| * @param limit maximum number of entries | ||
| * @return List of log entries ordered by newest entry first | ||
| */ | ||
| @Override | ||
| public List<InvocationLogEntry> findLastEntriesByServiceName(String service, String version, int limit) { | ||
| Query query = new Query().addCriteria(Criteria.where("serviceName").is(service)) | ||
| .addCriteria(Criteria.where("serviceVersion").is(version)) | ||
| .with(Sort.by(Sort.Direction.DESC, "invocationTimestamp")).limit(limit); | ||
| return mongoTemplate.find(query, InvocationLogEntry.class); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this one is necessary. I prefer to limit the number of dependencies to spring-data stuff to the minimum.