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

DM 1876 opentelemetry custom exporter for platform used in eg ms #79

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions hello-world-microservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>c8y.agents.lpwan.backend</groupId>
<artifactId>lpwan-backend</artifactId>
<version>${c8y.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -39,11 +44,27 @@
<artifactId>microservice-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>java-client-model</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>c8y.opentelemetry.exporters</groupId>
<artifactId>opentelemetry-metrics-exporters</artifactId>
<version>${c8y.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
22 changes: 22 additions & 0 deletions hello-world-microservice/src/main/configuration/cumulocity.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
],
"roles":[
],
"resources": {
"cpu": "1000m",
"memory": "1Gi"
},
"billing" : {
"metrics" : [
{
"name": "hwGaugeMetricStSt",
"type": "NUMBER",
"aggregation": {
"function": "LATEST"
}
},
{
"name": "hwSumMetricStSt",
"type": "NUMBER",
"aggregation": {
"function": "SUM"
}
}
]
},
"livenessProbe":{
"httpGet":{
"path": "/health",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package c8y.example.helloworld;

import io.opentelemetry.api.metrics.ObservableDoubleGauge;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class GaugeStorage {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This component stores the list of gauge callbacks to be closed post export. See the comment mentioned in PR for the explanation

private List<ObservableDoubleGauge> observableDoubleGaugeList = new ArrayList<>();

public List<ObservableDoubleGauge> getObservableDoubleGaugeList() {
return observableDoubleGaugeList;
}

public void addObservableGauge(ObservableDoubleGauge doubleGauge) {
observableDoubleGaugeList.add(doubleGauge);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@MicroserviceApplication
@RestController
@ComponentScan(basePackages = {
"c8y.example.helloworld",
"com.cumulocity.exporters.platform",
"com.cumulocity.exporters.common"
})
public class HelloWorldMain {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package c8y.example.helloworld;

import com.cumulocity.exporters.common.OpenTelemetryExporterStrategy;
import com.cumulocity.exporters.common.OpenTelemetryExporterStrategyEnum;
import com.cumulocity.exporters.common.OpenTelemetryExporterStrategyFactory;
import com.cumulocity.exporters.platform.ExportCompletedEvent;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.ObservableDoubleGauge;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;

@RestController
@Slf4j
public class InventoryController implements ApplicationListener<ExportCompletedEvent> {
private static String INSTRUMENTATION_SCOPE_NAME = HelloWorldMain.class.getName();

@Autowired
private OpenTelemetryExporterStrategyFactory exporterStrategyFactory;

// @Autowired
// private OpenTelemetry openTelemetry;

@Autowired
private GaugeStorage gaugeStorage;

private Meter meter;

@GetMapping("/gaugeMetric")
public String gaugeMetric() {
OpenTelemetryExporterStrategy openTelemetryExporterStrategy = exporterStrategyFactory.findStrategy(OpenTelemetryExporterStrategyEnum.PLATFORM);
OpenTelemetry openTelemetry = openTelemetryExporterStrategy.getOpenTelemetry();
meter = openTelemetry.getMeter(INSTRUMENTATION_SCOPE_NAME);
Random random = new Random();
int value = random.nextInt(20);
DateTime recordedTime = DateTime.now();
log.info("Sending gaugeMetric with value {}", value);
ObservableDoubleGauge gauge = meter
.gaugeBuilder("hwGaugeMetricStSt")
.buildWithCallback(
getObservableDoubleMeasurementConsumer(value, recordedTime));
gaugeStorage.addObservableGauge(gauge);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Store the gauge in the list post instrumentation of metric

return "test";
}

private Consumer<ObservableDoubleMeasurement> getObservableDoubleMeasurementConsumer(int value, DateTime recordedTime) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Attributes used to record time at which the metric point was actually instrumented. Refer https://github.com/SoftwareAG/cumulocity-clients-java/pull/273/files#r1132329707 for explanation

return result -> result.record(value, Attributes.of(AttributeKey.stringKey(recordedTime.toString()), recordedTime.toString()));
}

@GetMapping("/sumMetric")
public String sumMetric() {
OpenTelemetryExporterStrategy openTelemetryExporterStrategy = exporterStrategyFactory.findStrategy(OpenTelemetryExporterStrategyEnum.PLATFORM);
OpenTelemetry openTelemetry = openTelemetryExporterStrategy.getOpenTelemetry();
meter = openTelemetry.getMeter(INSTRUMENTATION_SCOPE_NAME);
log.info("Sending sumMetric at {}", LocalDateTime.now());
Random random = new Random();
LongCounter sumCounter = meter.counterBuilder("hwSumMetricStSt")
.setUnit("units")
.build();
sumCounter.add(10);
return "test";
}

@Override
public void onApplicationEvent(ExportCompletedEvent event) {
int metricCollectionSize = event.getMetricCollectionSize();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Post export, close the list of gauges recorded.

List<ObservableDoubleGauge> gaugeList = gaugeStorage.getObservableDoubleGaugeList();
for(int metricIndex = 0; metricIndex < metricCollectionSize; metricIndex++) {
gaugeList.get(metricIndex).close();
}
gaugeList.subList(0, metricCollectionSize).clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
application.name=hello-world
server.port=80
server.port=12320

# This parameter is provided by platform for microservice deployed in cumulocity.
# You can set it up to your dedicated cumulocity address. You can get one by trying free trial on www.cumulocity.com
C8Y.baseURL=
C8Y.baseURL=http://localhost:30080
spring.main.allow-bean-definition-overriding=true