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

Fixed hofund connection tags #58

Merged
merged 4 commits into from
Nov 27, 2024
Merged
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
10 changes: 10 additions & 0 deletions changelog/unreleased/000057-connection-tags-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Fixed an issue with registering hofund.node, caused by inconsistent sets of tag keys across different connection types
authors:
- name: Mateusz Witkowski
nick: witx98
url: https://github.com/witx98
type: fixed #[added/changed/deprecated/removed/fixed/security/other]
issues:
- 57
merge_requests:
- 58
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.logchange.hofund.connection;

import dev.logchange.hofund.StringUtils;
import lombok.Builder;
import lombok.Data;

Expand All @@ -8,12 +9,21 @@
@Data
@Builder
public class HofundConnection {

/**
* Name of the resource that application connects to f.e. cart-db, fcm, products-app
*/
private final String target;
private final String url;
private final Type type;
private final AtomicReference<StatusFunction> fun;
private final String description;

public String toTargetTag() {
return type == Type.DATABASE ? target + "_" + type : target;
}

public String getDescription() {
return StringUtils.emptyIfNull(description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,21 @@ public HofundConnectionMeter(HofundInfoProvider infoProvider, List<HofundConnect
.collect(Collectors.toList());
}

public static List<Tag> tags(HofundInfoProvider infoProvider, HofundConnection connection) {
private List<Tag> tags(HofundConnection connection) {
List<Tag> tags = new LinkedList<>();
tags.add(Tag.of("id", infoProvider.getApplicationName() + "-" + connection.getTarget() + "_" + connection.getType()));
tags.add(Tag.of("source", infoProvider.getApplicationName()));

String target = connection.getTarget();
if (connection.getType() == Type.DATABASE) {
target += "_" + connection.getType();
tags.add(Tag.of("db_vendor", ((HofundDatabaseConnection) connection).getDbVendor()));
}

tags.add(Tag.of("target", target));
tags.add(Tag.of("target", connection.toTargetTag()));
tags.add(Tag.of("type", connection.getType().toString()));

tags.add(Tag.of("description", connection.getDescription()));
return tags;
}

@Override
public void bindTo(MeterRegistry meterRegistry) {
connections.forEach(connection -> {
Gauge.builder(NAME, connection, con -> con.getFun().get().getStatus().getValue())
.description(DESCRIPTION)
.tags(tags(infoProvider, connection))
.register(meterRegistry);
});
connections.forEach(connection -> Gauge.builder(NAME, connection, con -> con.getFun().get().getStatus().getValue())
.description(DESCRIPTION)
.tags(tags(connection))
.register(meterRegistry));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package dev.logchange.hofund.graph.edge;

import dev.logchange.hofund.connection.HofundConnection;
import dev.logchange.hofund.connection.HofundConnectionMeter;
import dev.logchange.hofund.connection.HofundConnectionsProvider;
import dev.logchange.hofund.info.HofundInfoProvider;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -37,12 +38,19 @@ public HofundEdgeMeter(HofundInfoProvider infoProvider, List<HofundConnectionsPr
@Override
public void bindTo(MeterRegistry meterRegistry) {

connections.forEach(connection -> {
Gauge.builder(NAME, atomicInteger, AtomicInteger::doubleValue)
.description(DESCRIPTION)
.tags(HofundConnectionMeter.tags(infoProvider, connection))
.register(meterRegistry);
});
connections.forEach(connection -> Gauge.builder(NAME, atomicInteger, AtomicInteger::doubleValue)
.description(DESCRIPTION)
.tags(tags(connection))
.register(meterRegistry));

}

private List<Tag> tags(HofundConnection connection) {
List<Tag> tags = new LinkedList<>();
tags.add(Tag.of("id", infoProvider.getApplicationName() + "-" + connection.getTarget() + "_" + connection.getType()));
tags.add(Tag.of("source", infoProvider.getApplicationName()));
tags.add(Tag.of("target", connection.toTargetTag()));
tags.add(Tag.of("type", connection.getType().toString()));
return tags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import dev.logchange.hofund.connection.HofundConnection;
import dev.logchange.hofund.connection.HofundConnectionsProvider;
import dev.logchange.hofund.connection.HofundDatabaseConnection;
import dev.logchange.hofund.connection.Type;
import dev.logchange.hofund.info.HofundInfoProvider;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -44,12 +42,10 @@ public void bindTo(MeterRegistry meterRegistry) {
.tags(tagsForInfo())
.register(meterRegistry);

connections.forEach(connection -> {
Gauge.builder(NAME, atomicInteger, AtomicInteger::doubleValue)
.description(DESCRIPTION)
.tags(tagsForConnection(connection))
.register(meterRegistry);
});
connections.forEach(connection -> Gauge.builder(NAME, atomicInteger, AtomicInteger::doubleValue)
.description(DESCRIPTION)
.tags(tagsForConnection(connection))
.register(meterRegistry));

}

Expand All @@ -66,18 +62,10 @@ private List<Tag> tagsForInfo() {

private List<Tag> tagsForConnection(HofundConnection connection) {
List<Tag> tags = new LinkedList<>();

String target = connection.getTarget();
if (connection.getType() == Type.DATABASE) {
target += "_" + connection.getType();
tags.add(Tag.of("db_vendor", ((HofundDatabaseConnection) connection).getDbVendor()));
}

tags.add(Tag.of("id", target));
tags.add(Tag.of("id", connection.toTargetTag()));
tags.add(Tag.of("title", connection.getTarget() + "_" + connection.getType()));
tags.add(Tag.of("subtitle", connection.getType().toString()));
tags.add(Tag.of("subtitle", connection.getType().toString() + " (" + connection.getDescription() + ")"));
tags.add(Tag.of("type", connection.getType().toString()));

return tags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dev.logchange.hofund.connection;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class HofundConnectionTest {


@ParameterizedTest
@EnumSource(
value = Type.class,
names = "DATABASE",
mode = EnumSource.Mode.EXCLUDE)
void shouldTargetPropertyWhenTypeIsDifferentThanDB() {
// given:
HofundConnection connection = HofundConnection.builder()
.target("Target")
.type(Type.HTTP)
.build();

// when:
String targetTag = connection.toTargetTag();

// then:
assertEquals("Target", targetTag);
}

@Test
void shouldTargetPropertyWhenTypeIsDB() {
// given:
HofundConnection connection = HofundConnection.builder()
.target("Target")
.type(Type.DATABASE)
.build();

String expected = "Target_database";

// when:
String targetTag = connection.toTargetTag();

// then:
assertEquals(expected, targetTag);
}

@Test
void shouldReturnEmptyDescriptionWhenPropertyIsNull() {
// given:
HofundConnection connection = HofundConnection.builder().build();

// when:
String description = connection.getDescription();

// then:
assertEquals("", description);
}

@Test
void shouldReturnValueOfDescriptionWhenPropertyIsNotNull() {
// given:

String expected = "Description";
HofundConnection connection = HofundConnection.builder().description(expected).build();

// when:
String description = connection.getDescription();

// then:
assertEquals(expected, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private static HofundConnection getHofundConnection(String target, Type type) {
target,
"fake",
type,
new AtomicReference<>(() -> Status.UP)
new AtomicReference<>(() -> Status.UP),
null
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.logchange.hofund.connection.spring.datasource;

import dev.logchange.hofund.connection.HofundDatabaseConnection;
import dev.logchange.hofund.connection.HofundConnection;
import dev.logchange.hofund.connection.spring.datasource.h2.H2Connection;
import dev.logchange.hofund.connection.spring.datasource.oracle.OracleConnection;
import dev.logchange.hofund.connection.spring.datasource.postgresql.PostgreSQLConnection;
Expand All @@ -14,7 +14,7 @@
@Slf4j
public class DataSourceConnectionFactory {

public static HofundDatabaseConnection of(DataSource dataSource) {
public static HofundConnection of(DataSource dataSource) {
try (Connection connection = dataSource.getConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
String productName = metaData.getDatabaseProductName();
Expand All @@ -30,7 +30,7 @@ public static HofundDatabaseConnection of(DataSource dataSource) {
}
};
} catch (SQLException e) {
log.warn("Error creating HofundDatabaseConnection", e);
log.warn("Error creating datasource HofundConnection", e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package dev.logchange.hofund.connection.spring.datasource;

import dev.logchange.hofund.connection.*;
import dev.logchange.hofund.connection.HofundConnection;
import dev.logchange.hofund.connection.Status;
import dev.logchange.hofund.connection.StatusFunction;
import dev.logchange.hofund.connection.Type;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -17,11 +23,11 @@ public abstract class DatasourceConnection {
protected final DataSource dataSource;
protected final String testQuery;

public HofundDatabaseConnection toHofundConnection() {
return HofundDatabaseConnection.hofundDatabaseConnectionBuilder()
public HofundConnection toHofundConnection() {
return HofundConnection.builder()
.target(getTarget())
.type(Type.DATABASE)
.dbVendor(getDbVendor())
.description(getDbVendor())
.url(getUrl())
.fun(new AtomicReference<>(testConnection()))
.build();
Expand Down