Skip to content

Commit

Permalink
Merge pull request #857 from amvanbaren/configure-tracing
Browse files Browse the repository at this point in the history
Configure tracing
  • Loading branch information
amvanbaren authored Mar 18, 2024
2 parents 0ce7b7b + 4f534d4 commit 71941ca
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 8 deletions.
7 changes: 6 additions & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def versions = [
httpclient5: '5.2.1',
jaxb_api: '2.3.1',
jaxb_impl: '2.3.8',
gatling: '3.9.5'
gatling: '3.9.5',
loki4j: '1.4.2'
]
ext['junit-jupiter.version'] = versions.junit
sourceCompatibility = versions.java
Expand Down Expand Up @@ -105,6 +106,10 @@ dependencies {
exclude group: 'org.antlr', module: 'antlr'
exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
}
implementation "com.github.loki4j:loki-logback-appender:${versions.loki4j}"
implementation "io.micrometer:micrometer-tracing"
implementation "io.micrometer:micrometer-tracing-bridge-otel"
implementation "io.opentelemetry:opentelemetry-exporter-zipkin"
runtimeOnly "io.micrometer:micrometer-registry-prometheus"
runtimeOnly "org.postgresql:postgresql"
jooqGenerator "org.postgresql:postgresql"
Expand Down
11 changes: 10 additions & 1 deletion server/src/dev/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
logging:
pattern:
level: '%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]'

server:
port: 8080

spring:
application:
name: openvsx-server
autoconfigure:
# don't send traces to Zipkin in development
exclude: org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinAutoConfiguration
profiles:
include: ovsx
cache:
Expand Down Expand Up @@ -132,4 +141,4 @@ ovsx:
publisher-agreement:
timezone: US/Eastern
integrity:
key-pair: create # create, renew, delete, 'undefined'
key-pair: create # create, renew, delete, 'undefined'
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.eclipse.openvsx;

import com.google.common.collect.Maps;
import io.micrometer.observation.annotation.Observed;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.cache.CacheService;
import org.eclipse.openvsx.eclipse.EclipseService;
Expand Down Expand Up @@ -259,6 +260,7 @@ public ReviewListJson getReviews(String namespace, String extensionName) {
}

@Override
@Observed
public SearchResultJson search(ISearchService.Options options) {
var json = new SearchResultJson();
var size = options.requestedSize;
Expand All @@ -280,6 +282,7 @@ public SearchResultJson search(ISearchService.Options options) {
}

@Override
@Observed
public QueryResultJson query(QueryRequest request) {
if (!StringUtils.isEmpty(request.extensionId)) {
var split = request.extensionId.split("\\.");
Expand Down Expand Up @@ -331,6 +334,7 @@ public QueryResultJson query(QueryRequest request) {
}

@Override
@Observed
public QueryResultJson queryV2(QueryRequestV2 request) {
if (!StringUtils.isEmpty(request.extensionId)) {
var split = request.extensionId.split("\\.");
Expand Down
4 changes: 4 additions & 0 deletions server/src/main/java/org/eclipse/openvsx/RegistryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.eclipse.openvsx;

import com.google.common.collect.Iterables;
import io.micrometer.observation.annotation.Observed;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.headers.Header;
Expand Down Expand Up @@ -904,6 +905,7 @@ public ResponseEntity<ReviewListJson> getReviews(
return new ResponseEntity<>(json, HttpStatus.NOT_FOUND);
}

@Observed
@GetMapping(
path = "/api/-/search",
produces = MediaType.APPLICATION_JSON_VALUE
Expand Down Expand Up @@ -1030,6 +1032,7 @@ private int mergeSearchResults(SearchResultJson result, List<SearchEntryJson> en
return mergedEntries;
}

@Observed
@GetMapping(
path = "/api/v2/-/query",
produces = MediaType.APPLICATION_JSON_VALUE
Expand Down Expand Up @@ -1142,6 +1145,7 @@ public ResponseEntity<QueryResultJson> getQueryV2(
.body(result);
}

@Observed
@GetMapping(
path = "/api/-/query",
produces = MediaType.APPLICATION_JSON_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.eclipse.openvsx.web.ShallowEtagHeaderFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
Expand Down Expand Up @@ -65,6 +64,8 @@ public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter()
public RequestRejectedHandler requestRejectedHandler() {
return new HttpStatusRequestRejectedHandler();
}

@Bean
@ConditionalOnProperty(value = "ovsx.data.mirror.enabled", havingValue = "true")
public FilterRegistrationBean<ReadOnlyRequestFilter> readOnlyRequestFilter(
@Value("${ovsx.data.mirror.read-only.allowed-endpoints}") String[] allowedEndpoints,
Expand All @@ -76,9 +77,4 @@ public FilterRegistrationBean<ReadOnlyRequestFilter> readOnlyRequestFilter(

return registrationBean;
}

@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("application", "openvsx-server");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
********************************************************************************/
package org.eclipse.openvsx;

import io.micrometer.observation.annotation.Observed;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.json.*;
import org.eclipse.openvsx.search.ISearchService;
Expand Down Expand Up @@ -259,6 +260,7 @@ public ReviewListJson getReviews(String namespace, String extension) {
}

@Override
@Observed
public SearchResultJson search(ISearchService.Options options) {
var urlTemplate = urlConfigService.getUpstreamUrl() + "/api/-/search";
var uriVariables = new HashMap<String,String>();
Expand Down Expand Up @@ -293,6 +295,7 @@ public SearchResultJson search(ISearchService.Options options) {
}

@Override
@Observed
public QueryResultJson query(QueryRequest request) {
var urlTemplate = urlConfigService.getUpstreamUrl() + "/api/-/query";
var queryParams = new HashMap<String,String>();
Expand Down Expand Up @@ -330,6 +333,7 @@ public QueryResultJson query(QueryRequest request) {
}

@Override
@Observed
public QueryResultJson queryV2(QueryRequestV2 request) {
var urlTemplate = urlConfigService.getUpstreamUrl() + "/api/v2/-/query";
var queryParams = new HashMap<String,String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/** ******************************************************************************
* Copyright (c) 2024 Precies. Software Ltd and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* ****************************************************************************** */
package org.eclipse.openvsx.metrics;

import io.micrometer.common.KeyValue;
import io.micrometer.observation.ObservationFilter;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.aop.ObservedAspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("!test")
public class MetricsConfiguration {
@Bean
public ObservedAspect observedAspect(ObservationRegistry observationRegistry) {
return new ObservedAspect(observationRegistry, new RegistryObservationConvention());
}

@Bean
public ObservationFilter observationFilter(
@Value("${management.metrics.tags.application:app}") String service,
@Value("${management.metrics.tags.environment:development}") String environment,
@Value("${management.metrics.tags.instance:local}") String instance
) {
return context -> context
.addLowCardinalityKeyValue(KeyValue.of("service.name", service))
.addLowCardinalityKeyValue(KeyValue.of("deployment.environment", environment))
.addLowCardinalityKeyValue(KeyValue.of("service.instance.id", instance));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/** ******************************************************************************
* Copyright (c) 2024 Precies. Software Ltd and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* ****************************************************************************** */
package org.eclipse.openvsx.metrics;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.aop.ObservedAspect;
import org.aspectj.lang.reflect.MethodSignature;

public class RegistryObservationConvention implements ObservationConvention<ObservedAspect.ObservedAspectContext> {

private ObjectMapper mapper;

public RegistryObservationConvention() {
this.mapper = new ObjectMapper();
}

@Override
public KeyValues getHighCardinalityKeyValues(ObservedAspect.ObservedAspectContext context) {
var joinPoint = context.getProceedingJoinPoint();
var args = joinPoint.getArgs();
var methodSignature = (MethodSignature) joinPoint.getSignature();
var parameterNames = methodSignature.getParameterNames();
var argKeyValues = new KeyValue[args.length];
for(var i = 0; i < args.length; i++) {
var key = "args." + parameterNames[i];
var value = convertObjectToString(args[i]);
argKeyValues[i] = KeyValue.of(key, value);
}

return ObservationConvention.super.getHighCardinalityKeyValues(context).and(argKeyValues);
}

private String convertObjectToString(Object arg) {
if(arg instanceof String) {
return (String) arg;
} else if(arg instanceof Number || arg instanceof Boolean) {
return String.valueOf(arg);
} else {
try {
return mapper.writeValueAsString(arg);
} catch (JsonProcessingException e) {
return "";
}
}
}

@Override
public boolean supportsContext(Observation.Context context) {
return context instanceof ObservedAspect.ObservedAspectContext;
}

@Override
public String getName() {
return "org.eclipse.openvsx.observed";
}

@Override
public String getContextualName(ObservedAspect.ObservedAspectContext context) {
var methodSignature = (MethodSignature) context.getProceedingJoinPoint().getSignature();
var method = methodSignature.getMethod();
return method.getDeclaringClass().getSimpleName() + "#" + method.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* ****************************************************************************** */
package org.eclipse.openvsx.repositories;

import io.micrometer.observation.annotation.Observed;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.json.QueryRequest;
Expand Down Expand Up @@ -267,6 +268,7 @@ private List<String> findVersionStringsSorted(List<Condition> conditions, Pageab
return versionsQuery.fetch(record -> record.get(EXTENSION_VERSION.VERSION));
}

@Observed
public Page<ExtensionVersion> findActiveVersions(QueryRequest request) {
var conditions = new ArrayList<Condition>();
if (!StringUtils.isEmpty(request.namespaceUuid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
********************************************************************************/
package org.eclipse.openvsx.repositories;

import io.micrometer.observation.annotation.Observed;
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.json.QueryRequest;
import org.eclipse.openvsx.util.NamingUtil;
Expand Down Expand Up @@ -317,6 +318,7 @@ public List<Extension> findActiveExtensionsById(Collection<Long> ids) {
return extensionJooqRepo.findAllActiveById(ids);
}

@Observed
public Page<ExtensionVersion> findActiveVersions(QueryRequest request) {
return extensionVersionJooqRepo.findActiveVersions(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.micrometer.observation.annotation.Observed;
import org.eclipse.openvsx.util.TargetPlatform;
import org.eclipse.openvsx.util.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -54,6 +55,7 @@ public boolean isEnabled() {
@Autowired
VersionService versions;

@Observed
@Transactional
@Cacheable(CACHE_DATABASE_SEARCH)
@CacheEvict(value = CACHE_AVERAGE_REVIEW_RATING, allEntries = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.util.ObjectBuilder;
import io.micrometer.observation.annotation.Observed;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.entities.Extension;
import org.eclipse.openvsx.migration.HandlerJobRequest;
Expand Down Expand Up @@ -250,6 +251,7 @@ public void removeSearchEntry(Extension extension) {
}
}

@Observed
public SearchHits<ExtensionSearch> search(Options options) {
var resultWindow = options.requestedOffset + options.requestedSize;
if(resultWindow > getMaxResultWindow()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import io.micrometer.observation.annotation.Observed;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.eclipse.openvsx.entities.Extension;

Expand All @@ -29,6 +31,7 @@ public interface ISearchService {
/**
* Search with given options
*/
@Observed
SearchHits<ExtensionSearch> search(Options options);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.eclipse.openvsx.search;

import io.micrometer.observation.annotation.Observed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.data.elasticsearch.core.SearchHits;
Expand Down Expand Up @@ -54,6 +55,7 @@ protected ISearchService getImplementation() {

}

@Observed
public SearchHits<ExtensionSearch> search(ElasticSearchService.Options options) {
return getImplementation().search(options);
}
Expand Down

0 comments on commit 71941ca

Please sign in to comment.