|
| 1 | +package io.github.reionchan.controller; |
| 2 | + |
| 3 | +import io.github.reionchan.AppBootstrap; |
| 4 | +import io.github.reionchan.dto.R; |
| 5 | +import io.opentelemetry.api.OpenTelemetry; |
| 6 | +import io.opentelemetry.api.common.AttributeKey; |
| 7 | +import io.opentelemetry.api.common.Attributes; |
| 8 | +import io.opentelemetry.api.metrics.LongHistogram; |
| 9 | +import io.opentelemetry.api.metrics.Meter; |
| 10 | +import io.opentelemetry.api.trace.Span; |
| 11 | +import io.opentelemetry.api.trace.Tracer; |
| 12 | +import io.opentelemetry.context.Scope; |
| 13 | +import io.swagger.v3.oas.annotations.Operation; |
| 14 | +import io.swagger.v3.oas.annotations.media.Content; |
| 15 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.http.MediaType; |
| 19 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 20 | +import org.springframework.web.bind.annotation.GetMapping; |
| 21 | +import org.springframework.web.bind.annotation.RestController; |
| 22 | + |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import static io.github.reionchan.consts.Role.ADMIN; |
| 26 | +import static io.github.reionchan.consts.Role.USER; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Reion |
| 30 | + * @date 2024-06-01 |
| 31 | + **/ |
| 32 | +@Slf4j |
| 33 | +@RestController |
| 34 | +@Tag(name = "PingController", description = "测试服务请求端点") |
| 35 | +public class PingController { |
| 36 | + |
| 37 | + private final AttributeKey<String> ATTR_METHOD = AttributeKey.stringKey("method"); |
| 38 | + |
| 39 | + private final Random random = new Random(); |
| 40 | + private final Tracer tracer; |
| 41 | + private final LongHistogram doWorkHistogram; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + public PingController(OpenTelemetry openTelemetry) { |
| 45 | + tracer = openTelemetry.getTracer(AppBootstrap.class.getName()); |
| 46 | + Meter meter = openTelemetry.getMeter(AppBootstrap.class.getName()); |
| 47 | + doWorkHistogram = meter.histogramBuilder("do-work").ofLongs().build(); |
| 48 | + } |
| 49 | + |
| 50 | + @GetMapping("/ping") |
| 51 | + @PreAuthorize("hasAnyRole('" + USER + "','" + ADMIN + "')") |
| 52 | + @Operation(summary = "ping 方法", description = "测试接口", |
| 53 | + requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody( |
| 54 | + description = "R 传输对象", |
| 55 | + content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)) |
| 56 | + ) |
| 57 | + public R<String> ping() throws InterruptedException { |
| 58 | + int sleepTime = random.nextInt(200); |
| 59 | + doWork(sleepTime); |
| 60 | + doWorkHistogram.record(sleepTime, Attributes.of(ATTR_METHOD, "ping")); |
| 61 | + return R.success("pong"); |
| 62 | + } |
| 63 | + |
| 64 | + private void doWork(int sleepTime) throws InterruptedException { |
| 65 | + Span span = tracer.spanBuilder("doWork").startSpan(); |
| 66 | + try (Scope ignored = span.makeCurrent()) { |
| 67 | + Thread.sleep(sleepTime); |
| 68 | + log.info("A sample log message!"); |
| 69 | + } finally { |
| 70 | + span.end(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments