Skip to content
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

add metrics api view implementation #102

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Add docker_enable_hostname_port_and_add_timeout_and_optimize_size

1.3.0

Metrics API comon [issue#101]

1.2.1

Bug logs: subservice log component as not apply
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ $ java -jar target/keypass-<VERSION>.jar server conf/config.yml
curl -i -H "Accept: application/xml" -H "Content-type: application/xml" \
-H "Fiware-Service: myTenant" \
-X POST -d @src/test/resources/es/tid/fiware/iot/ac/xacml/policy01.xml \
http://localhost:8080/pap/v1/subject/role12345
http://localhost:7070/pap/v1/subject/role12345
```

Response should be something like this:

```
HTTP/1.1 201 Created
Date: Mon, 15 Sep 2014 20:02:35 GMT
Location: http://localhost:8080/pap/v1/subject/role12345/policy/policy01
Location: http://localhost:7070/pap/v1/subject/role12345/policy/policy01
Content-Type: application/xml
Content-Length: 0
```
Expand All @@ -73,7 +73,7 @@ Content-Length: 0

```
curl -i -H "Fiware-Service: myTenant" \
http://localhost:8080/pap/v1/subject/role12345/policy/policy01
http://localhost:7070/pap/v1/subject/role12345/policy/policy01
```

Response will be the previously uploaded policy.
Expand All @@ -84,7 +84,7 @@ Response will be the previously uploaded policy.
curl -i -H "Accept: application/xml" -H "Content-type: application/xml" \
-H "Fiware-Service: myTenant" \
-X POST -d @src/test/resources/es/tid/fiware/iot/ac/xacml/policy01_request01.xml \
http://localhost:8080/pdp/v3
http://localhost:7070/pdp/v3
```
Response:

Expand Down
6 changes: 3 additions & 3 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ time=15:12:55.106 | lvl=INFO | corr= | trans=n/a | op=DropwizardResourceConfig |
PUT /pap/v1/{tenant}/subject/{subject}/policy/{policyId} (es.tid.fiware.iot.ac.pap.PoliciesEndpoint)
POST /pdp/v3/{tenant} (es.tid.fiware.iot.ac.pdp.PdpEndpoint)

time=15:12:55.464 | lvl=INFO | corr= | trans=n/a | op=ServerConnector | msg=Started application@7e00a490{HTTP/1.1}{0.0.0.0:8080}
time=15:12:55.464 | lvl=INFO | corr= | trans=n/a | op=ServerConnector | msg=Started admin@5c342a90{HTTP/1.1}{127.0.0.1:8081}
time=15:12:55.464 | lvl=INFO | corr= | trans=n/a | op=ServerConnector | msg=Started application@7e00a490{HTTP/1.1}{0.0.0.0:7070}
time=15:12:55.464 | lvl=INFO | corr= | trans=n/a | op=ServerConnector | msg=Started admin@5c342a90{HTTP/1.1}{127.0.0.1:7071}
```

## Database connection errors
Expand Down Expand Up @@ -72,6 +72,6 @@ time=15:15:02.453 | lvl=ERROR | corr=45a55bc3-3456-47ec-bf97-436f7ed0e71a | tran
## Monitoring API

You can inspect the application status through its admin interface, by default
configured to listen in <http://localhost:8081>.
configured to listen in <http://localhost:7071>.

The admin interface expose several metrics and health check status.
46 changes: 46 additions & 0 deletions apiary.apib
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,49 @@ Policy Decision Point
</Result>
</Response>
```




## Metrics [/v1.0/admin/metrics]

The following metrics are collected by the component:
+ incomingTransactions: number of requests consumed by the component.
+ incomingTransactionRequestSize: total size (bytes) in requests associated to incoming transactions
+ incomingTransactionResponseSize: total size (bytes) in responses associated to incoming transactions
+ incomingTransacionError: number of incoming transactions resulting in error.
+ serviceTime: average time to serve a transaction.

### Get current metrics [GET]
+ Parameters
+ reset (optional, `string`) ... ["true", "false"]
+ Response 200 (application/json)
+ Body
{
"service": {
},
"sum": {
"incomingTransactions": 25,
"incomingTransactionRequestSize": 3456,
"incomingTransactionResponseSize": 435,
"incomingTransacionError": 2,
"serviceTime": 0.48
}
}


### Reset current metrics [DELETE]
+ Parameters
+ Response 204 (application/json)
+ Body
{
"service": {
},
"sum": {
"incomingTransactions": 25,
"incomingTransactionRequestSize": 3456,
"incomingTransactionResponseSize": 435,
"incomingTransacionError": 2,
"serviceTime": 0.48
}
}
6 changes: 3 additions & 3 deletions conf/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
server:
applicationConnectors:
- type: http
port: 8080
port: 7070
bindHost: 0.0.0.0
adminConnectors:
- type: http
port: 8081
bindHost: 127.0.0.1
port: 7071
bindHost: 0.0.0.0

tenantHeader: Fiware-Service

Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>es.tid.fiware</groupId>
<artifactId>keypass</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>
<description>
Keypass is multi-tenant XACML server with PAP (Policy Administration
Point) andPDP (Policy Detention Point) capabilities.
Expand Down Expand Up @@ -43,6 +43,11 @@
<artifactId>dropwizard-views</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Expand Down Expand Up @@ -75,6 +80,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>

<properties>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/es/tid/fiware/iot/ac/AcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.dropwizard.client.JerseyClientConfiguration;

public class AcConfig extends Configuration {

@NotEmpty
Expand All @@ -51,6 +53,12 @@ public class AcConfig extends Configuration {
@JsonProperty("pdpCache")
private CacheConfig pdpCacheConfig = new CacheConfig();

@Valid
@NotNull
@JsonProperty
private JerseyClientConfiguration httpClient = new JerseyClientConfiguration();


public String getTenantHeader() {
return tenantHeader;
}
Expand All @@ -71,6 +79,10 @@ public CacheConfig getPdpCacheConfig() {
return pdpCacheConfig;
}

public JerseyClientConfiguration getJerseyClientConfiguration() {
return httpClient;
}

public void setTenantHeader(String tenantHeader) {
this.tenantHeader = tenantHeader;
}
Expand All @@ -90,4 +102,5 @@ public void setDatabase(DataSourceFactory database) {
public void setPdpCacheConfig(CacheConfig pdpCacheConfig) {
this.pdpCacheConfig = pdpCacheConfig;
}

}
15 changes: 11 additions & 4 deletions src/main/java/es/tid/fiware/iot/ac/AcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@
import es.tid.fiware.iot.ac.util.BlockingCacheFactory;
import es.tid.fiware.iot.ac.util.LogsEndpoint;
import es.tid.fiware.iot.ac.util.VersionEndpoint;
import es.tid.fiware.iot.ac.util.MetricsEndpoint;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.migrations.MigrationsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import com.codahale.metrics.MetricRegistry;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import com.sun.jersey.api.client.Client;


public class AcService extends io.dropwizard.Application<AcConfig> {

Expand Down Expand Up @@ -74,6 +78,8 @@ public void run(AcConfig configuration,
Environment environment)
throws Exception {

MetricRegistry metrics = environment.metrics();

PolicyDao dao = new PolicyDAOHibernate(hibernate.getSessionFactory());
PdpFactory pdpFactory = new PdpFactoryCached(dao,
new BlockingCacheFactory(
Expand All @@ -90,12 +96,13 @@ public void run(AcConfig configuration,
environment.jersey().getResourceConfig().getContainerResponseFilters().add(
new CorrelatorHeaderFilter(configuration.getCorrelatorHeader()));

environment.jersey().register(new TenantEndpoint(dao));
environment.jersey().register(new SubjectEndpoint(dao));
environment.jersey().register(new PoliciesEndpoint(dao));
environment.jersey().register(new PdpEndpoint(pdpFactory));
environment.jersey().register(new TenantEndpoint(dao, metrics));
environment.jersey().register(new SubjectEndpoint(dao, metrics));
environment.jersey().register(new PoliciesEndpoint(dao, metrics));
environment.jersey().register(new PdpEndpoint(pdpFactory, metrics));
environment.jersey().register(new LogsEndpoint());
environment.jersey().register(new VersionEndpoint());
environment.jersey().register(new MetricsEndpoint(metrics));

}
}
37 changes: 31 additions & 6 deletions src/main/java/es/tid/fiware/iot/ac/pap/PoliciesEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* 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
Expand All @@ -26,13 +26,16 @@
import es.tid.fiware.iot.ac.rs.Tenant;
import es.tid.fiware.iot.ac.rs.Correlator;
import io.dropwizard.hibernate.UnitOfWork;
import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.MetricRegistry;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Path("/pap/v1/subject/{subject}/policy/{policyId}")
@Produces(MediaType.APPLICATION_XML)
public class PoliciesEndpoint {
Expand All @@ -41,23 +44,33 @@ public class PoliciesEndpoint {

private PolicyDao dao;

public PoliciesEndpoint(PolicyDao dao) {
private MetricRegistry metrics;

public PoliciesEndpoint(PolicyDao dao, MetricRegistry metrics) {
this.dao = dao;
this.metrics = metrics;
}

@GET
@UnitOfWork
@Correlator
@Timed
public Response getPolicy(@Tenant String tenant,
@Correlator String correlator,
@PathParam("subject") String subject,
@PathParam("policyId") String policyId) {

LOGGER.debug("Getting policy with id [{}] for [{}] and subject [{}]", policyId, tenant, subject);

String id = URLEncoding.decode(policyId);

Policy p = dao.loadPolicy(tenant, subject, id);
metrics.counter("incomingTransactionRequestSize").inc(tenant.length() +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sum does not really give us the Request Size, don't you think? It gives us the size of the interesting contents (I mean, the values inside the target XML elements) but it leaves away all the XML boilerplate used to codify it. Maybe I'm wrong and that's not the case, but if I'm not, this should be noted in the documentation, as the main reason to have this metrics is bandwidth analysis, and it won't be much useful considering those small bits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keypass already uses a internal library for metrics, based on a jetty package. This library is good measuring time, but not total size of requests. This way, using a new coutner, we are rearusing that we could (input data, including headers), that is really near to real request size. The same for responses.

correlator.length() +
subject.length() +
policyId.length());
if (p != null) {
metrics.counter("incomingTransactionResponseSize").inc(p.getPolicy().length());
return Response.ok(p.getPolicy()).build();
} else {
return Response.status(404).build();
Expand All @@ -67,6 +80,7 @@ public Response getPolicy(@Tenant String tenant,
@DELETE
@UnitOfWork
@Correlator
@Timed
public Response deletePolicy(@Tenant String tenant,
@Correlator String correlator,
@PathParam("subject") String subject,
Expand All @@ -76,6 +90,10 @@ public Response deletePolicy(@Tenant String tenant,
LOGGER.debug("Removing policy with id [{}] for [{}] and subject [{}]", policyId, tenant, subject);

Policy p = dao.loadPolicy(tenant, subject, id);
metrics.counter("incomingTransactionRequestSize").inc(tenant.length() +
correlator.length() +
subject.length() +
policyId.length());
if (p != null) {
dao.deletePolicy(p);
return Response.status(204).build();
Expand All @@ -87,19 +105,26 @@ public Response deletePolicy(@Tenant String tenant,
@PUT
@UnitOfWork
@Correlator
@Timed
public Response updatePolicy(@Tenant String tenant,
@Correlator String correlator,
@PathParam("subject") String subject,
@PathParam("policyId") String policyId,
String policy) {

LOGGER.debug("Updating policy with id [{}] for [{}] and subject [{}]", policyId, tenant, subject);

String id = URLEncoding.decode(policyId);

Policy p = dao.loadPolicy(tenant, subject, id);
metrics.counter("incomingTransactionRequestSize").inc(tenant.length() +
correlator.length() +
subject.length() +
policyId.length() +
policy.length());
if (p != null) {
Policy newP = dao.updatePolicy(new Policy(id, tenant, subject, policy));
metrics.counter("incomingTransactionResponseSize").inc(newP.getPolicy().length());
return Response.ok(newP.getPolicy()).build();
} else {
return Response.status(404).build();
Expand Down
Loading