-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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 |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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