Skip to content

Commit

Permalink
Added jersey-micrometer module
Browse files Browse the repository at this point in the history
code got ported from Micrometer 1.10.9 and contains metrics and observation listeners

related issue: eclipse-ee4j#5371

Signed-off-by: Marcin Grzejszczak <[email protected]>
  • Loading branch information
marcingrzejszczak committed Aug 17, 2023
1 parent c5ad99a commit 551390c
Show file tree
Hide file tree
Showing 28 changed files with 2,473 additions and 0 deletions.
104 changes: 104 additions & 0 deletions ext/micrometer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
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.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>org.glassfish.jersey.ext</groupId>
<version>2.41-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jersey-micrometer</artifactId>

<dependencies>

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>${micrometer.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-integration-test</artifactId>
<version>${micrometer-tracing.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
org.glassfish.jersey.micrometer.server.*;version=${project.version}
</Export-Package>
<Import-Package>
org.eclipse.microprofile.micrometer.server.*;version="!",
*
</Import-Package>
</instructions>
<unpackBundle>true</unpackBundle>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.micrometer.server;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

public interface AnnotationFinder {

AnnotationFinder DEFAULT = new AnnotationFinder() {
};

/**
* The default implementation performs a simple search for a declared annotation
* matching the search type. Spring provides a more sophisticated annotation search
* utility that matches on meta-annotations as well.
* @param annotatedElement The element to search.
* @param annotationType The annotation type class.
* @param <A> Annotation type to search for.
* @return A matching annotation.
*/
@SuppressWarnings("unchecked")
default <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
Annotation[] anns = annotatedElement.getDeclaredAnnotations();
for (Annotation ann : anns) {
if (ann.annotationType() == annotationType) {
return (A) ann;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.micrometer.server;

import io.micrometer.common.KeyValues;
import io.micrometer.common.lang.Nullable;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ContainerResponse;
import org.glassfish.jersey.server.monitoring.RequestEvent;

/**
* Default implementation for {@link JerseyObservationConvention}.
*
* @author Marcin Grzejszczak
* @since 2.41
*/
public class DefaultJerseyObservationConvention implements JerseyObservationConvention {

private final String metricsName;

public DefaultJerseyObservationConvention(String metricsName) {
this.metricsName = metricsName;
}

@Override
public KeyValues getLowCardinalityKeyValues(JerseyContext context) {
RequestEvent event = context.getRequestEvent();
ContainerRequest request = context.getCarrier();
ContainerResponse response = context.getResponse();
return KeyValues.of(JerseyKeyValues.method(request), JerseyKeyValues.uri(event),
JerseyKeyValues.exception(event), JerseyKeyValues.status(response), JerseyKeyValues.outcome(response));
}

@Override
public String getName() {
return this.metricsName;
}

@Nullable
@Override
public String getContextualName(JerseyContext context) {
if (context.getCarrier() == null) {
return null;
}
return "HTTP " + context.getCarrier().getMethod();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.micrometer.server;

import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import org.glassfish.jersey.server.ContainerResponse;
import org.glassfish.jersey.server.monitoring.RequestEvent;

/**
* Default implementation for {@link JerseyTagsProvider}.
*
* @author Michael Weirauch
* @author Johnny Lim
* @since 2.41
*/
public final class DefaultJerseyTagsProvider implements JerseyTagsProvider {

@Override
public Iterable<Tag> httpRequestTags(RequestEvent event) {
ContainerResponse response = event.getContainerResponse();
return Tags.of(JerseyTags.method(event.getContainerRequest()), JerseyTags.uri(event),
JerseyTags.exception(event), JerseyTags.status(response), JerseyTags.outcome(response));
}

@Override
public Iterable<Tag> httpLongRequestTags(RequestEvent event) {
return Tags.of(JerseyTags.method(event.getContainerRequest()), JerseyTags.uri(event));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.micrometer.server;

import java.util.List;

import io.micrometer.observation.transport.ReceiverContext;
import io.micrometer.observation.transport.RequestReplyReceiverContext;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ContainerResponse;
import org.glassfish.jersey.server.monitoring.RequestEvent;

/**
* A {@link ReceiverContext} for Jersey.
*
* @author Marcin Grzejszczak
* @since 2.41
*/
public class JerseyContext extends RequestReplyReceiverContext<ContainerRequest, ContainerResponse> {

private RequestEvent requestEvent;

public JerseyContext(RequestEvent requestEvent) {
super((carrier, key) -> {
List<String> requestHeader = carrier.getRequestHeader(key);
if (requestHeader == null || requestHeader.isEmpty()) {
return null;
}
return requestHeader.get(0);
});
this.requestEvent = requestEvent;
setCarrier(requestEvent.getContainerRequest());
}

public void setRequestEvent(RequestEvent requestEvent) {
this.requestEvent = requestEvent;
}

public RequestEvent getRequestEvent() {
return requestEvent;
}

}
Loading

0 comments on commit 551390c

Please sign in to comment.