Skip to content
Permalink
Browse files
metrics/osgi: initial contribution
The modules still refer to the Sling parent pom, but that should not
be a blocker for the contribution (maybe for a first release).
  • Loading branch information
rombert committed Sep 3, 2020
1 parent da5dd62 commit 8863470e1b194d6c5593b6fa955e7600e2b4c018
Show file tree
Hide file tree
Showing 28 changed files with 2,186 additions and 0 deletions.
@@ -0,0 +1,70 @@
# Apache Felix OSGi Metrics

The OSGi metrics module defines a simple mechanism to gather OSGi-related metrics for application startup.

The module is split into two bundles:

- _collector_ - a zero-dependencies bundle that uses the OSGi APIs to gather various metrics
- _consumers_ - a single bundle that contains various consumers

## Metric collection

The metrics are collected by the `org.apache.felix.metrics.osgi.collector` bundle. This bundle requires no configuration and imports a minimal set of packages, to allow starting as early as possible.

As soon as startup is completed the metrics are made available to consumers that implement the `StartupMetricsListener` interface. The metrics are published after an optional delay, to prevent on-off bounces in startup completion.

Startup completion is delegated to either the `org.apache.felix.systemready` or the `org.apache.felix.healtchecks.api` bundles, which publish marker services once the system is considered ready.

## Metric publication

The `org.apache.felix.metrics.osgi.consumers` bundle contains three out-of-the-box implementation for publishing the metrics

- DropWizard metrics using a `MetricRegistry`
- JSON file written in the bundle data directory
- Log entries using the SLF4j API

### JSON metrics file sample

The following (truncated) JSON file exemplifies how the metrics are written

```json
{
"application": {
"startTimeMillis": 1587469534671,
"startDurationMillis": 14635
},
"bundles": [
{
"symbolicName": "org.osgi.util.pushstream",
"startTimeMillis": 1587469535933,
"startDurationMillis": 0
},
{
"symbolicName": "org.apache.aries.util",
"startTimeMillis": 1587469535935,
"startDurationMillis": 0
},
{
"symbolicName": "org.apache.felix.configadmin",
"startTimeMillis": 1587469536313,
"startDurationMillis": 58
}
],
"services": [
{
"identifier": "jmx.objectname=org.apache.sling.classloader:name=FSClassLoader,type=ClassLoader",
"restarts": 2
}
]
}
```

Similar metrics are reported through the other collectors.

## Usage

1. Add the `org.apache.felix/org.apache.felix.metrics.osgi.collector` bundle and ensure that
it starts as early as possible
1. Add the `org.apache.felix/org.apache.felix.metrics.osgi.consumers` bundle.
1. Add the required bundles, either Apache Felix SystemReady or Apache Felix Health Checks
1. Start up the application
@@ -0,0 +1,2 @@
Import-Package: org.slf4j;resolution:=optional, *
DynamicImport-Package: org.slf4j
@@ -0,0 +1,157 @@
<?xml version="1.0"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
you 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. -->
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling-bundle-parent</artifactId>
<version>38</version>
<relativePath />
</parent>

<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.metrics.osgi.collector</artifactId>
<version>0.1.0-SNAPSHOT</version>

<name>Apache Felix OSGi Metrics Collector</name>
<description>
Collects metrics related to the OSGi framework and makes them available to consumers.
</description>

<build>
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-baseline-maven-plugin</artifactId>
<configuration>
<failOnMissing>false</failOnMissing>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
</dependency>

<!-- note this is set to optional in bnd.bnd, to help the bundle start as soon as possible -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- testing dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>

<!-- IT dependencies -->
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>${pax-exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>${pax-exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>${pax-exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-aether</artifactId>
<version>${pax-url.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>6.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.systemready</artifactId>
<version>0.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.healthcheck.api</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.healthcheck.core</artifactId>
<version>2.0.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.healthcheck.generalchecks</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>

<properties>
<pax-exam.version>4.13.2</pax-exam.version>
<pax-url.version>2.6.2</pax-url.version>
</properties>
</project>
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.felix.metrics.osgi;

import java.time.Duration;
import java.time.Instant;

public final class BundleStartDuration {

private final String symbolicName;
private final Instant startingAt;
private final Duration startedAfter;

public BundleStartDuration(String symbolicName, Instant startingAt, Duration startedAfter) {
this.symbolicName = symbolicName;
this.startingAt = startingAt;
this.startedAfter = startedAfter;
}

public String getSymbolicName() {
return symbolicName;
}

public Instant getStartingAt() {
return startingAt;
}

public Duration getStartedAfter() {
return startedAfter;
}
}
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.felix.metrics.osgi;

public final class ServiceRestartCounter {

private final String serviceIdentifier;
private final int serviceRestarts;

public ServiceRestartCounter(String serviceIdentifier, int serviceRestarts) {
this.serviceIdentifier = serviceIdentifier;
this.serviceRestarts = serviceRestarts;
}

/**
* @return a opaque service identifier, used for describing the service that has restarted
*/
public String getServiceIdentifier() {
return serviceIdentifier;
}

public int getServiceRestarts() {
return serviceRestarts;
}
}

0 comments on commit 8863470

Please sign in to comment.