Giriş
Açıklaması şöyle
One of the things that are unique about Micrometer is that it aims to be a facade with support for multiple observability standards, including OTEL, Brave, and others.As such, its focus is more on defining the vocabulary, conventions, and interfaces to use in the application which are arguably the more important aspect of making logging effective from a code perspective.
OTEL
Örnek
Şöyle yaparız
@Bean public OpenTelemetry getOpenTelemetry() { Resource serviceNameResource = Resource.create( Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName)); //[OTel component] SpanExporter is a component that gets called when a span is finished. OtlpGrpcSpanExporter otlpExporter = OtlpGrpcSpanExporter.builder() .setEndpoint(otlpEexporterEndpoint) .setTimeout(30, TimeUnit.SECONDS) .build(); // [OTel component] SdkTracerProvider is an SDK implementation for TracerProvider SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder() .setResource(Resource.getDefault().merge(serviceNameResource)) .addSpanProcessor(SimpleSpanProcessor.create(otlpExporter)) .build(); // [OTel component] The SDK implementation of OpenTelemetry OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder() .setTracerProvider(sdkTracerProvider) .setPropagators(ContextPropagators.create(B3Propagator.injectingSingleHeader())) .build(); return openTelemetrySdk; }
Örnek
Şöyle yaparız
@PostMapping("/pets/new")public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) { Observation observation = Observation.createNotStarted("create_pet", registry).start(); try (Observation.Scope scope = observation.openScope()) { // only 2 names are valid (low cardinality) observation.lowCardinalityKeyValue("pet.type", pet.getType().getName()); observation.highCardinalityKeyValue("pet.name", pet.getName()); observation.event(Observation.Event.of("PetCreated")); //more logic this.owners.save(owner); return "redirect:/owners/{ownerId}"; } catch (Exception exception) { observation.error(exception); throw exception; } finally { observation.stop(); } }
createNotStarted + observe
Açıklaması şöyle
The method observe is actually a timer that will measure the execution time of whatever is passed inside.
Örnek
Şöyle yaparız
@PostMapping("/pets/{petId}/edit")public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) { return Observation.createNotStarted("update ", registry).observe(()->{ if (result.hasErrors()) { model.put("pet", pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } owner.addPet(pet); this.owners.save(owner); return "redirect:/owners/{ownerId}"; }); }
Örnek
Şöyle yaparız
public class UserController {private final ObservationRegistry observationRegistry;@GetMappingpublic User findUserByFirstName(@RequestParam("firstName") String firstName) {return Observation.createNotStarted("users.find", this.observationRegistry).observe(() -> {Optional<User> findUserResult = this.userRepository.findByFirstName(firstName);if (findUserResult.isEmpty()) {throw new IllegalArgumentException( String.format("User with firstName=%s not found.", firstName));}return findUserResult.get();});}}
Hiç yorum yok:
Yorum Gönder