Search in sources :

Example 31 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class MongoDbBasedTenantDao method delete.

/**
 * {@inheritDoc}
 */
@Override
public Future<Void> delete(final String tenantId, final Optional<String> resourceVersion, final SpanContext tracingContext) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(resourceVersion);
    final Span span = tracer.buildSpan("delete Tenant").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    resourceVersion.ifPresent(v -> TracingHelper.TAG_RESOURCE_VERSION.set(span, v));
    final JsonObject deleteTenantQuery = MongoDbDocumentBuilder.builder().withVersion(resourceVersion).withTenantId(tenantId).document();
    return mongoClient.findOneAndDelete(collectionName, deleteTenantQuery).compose(deleteResult -> {
        if (deleteResult == null) {
            return MongoDbBasedDao.checkForVersionMismatchAndFail(tenantId, resourceVersion, getById(tenantId, false, span));
        } else {
            LOG.debug("successfully deleted tenant [tenant-id: {}]", tenantId);
            span.log("successfully deleted tenant");
            return Future.succeededFuture((Void) null);
        }
    }).onFailure(t -> TracingHelper.logError(span, "error deleting tenant", t)).recover(this::mapError).onComplete(r -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X500Principal(javax.security.auth.x500.X500Principal) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) ArrayList(java.util.ArrayList) Status(io.vertx.ext.healthchecks.Status) Sort(org.eclipse.hono.service.management.Sort) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) HealthCheckProvider(org.eclipse.hono.service.HealthCheckProvider) SearchResult(org.eclipse.hono.service.management.SearchResult) References(io.opentracing.References) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantDto(org.eclipse.hono.service.management.tenant.TenantDto) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) MongoClient(io.vertx.ext.mongo.MongoClient) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) Span(io.opentracing.Span) FindOptions(io.vertx.ext.mongo.FindOptions) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span)

Example 32 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class MongoDbBasedDeviceDao method update.

/**
 * {@inheritDoc}
 */
@Override
public Future<String> update(final DeviceDto deviceConfig, final Optional<String> resourceVersion, final SpanContext tracingContext) {
    Objects.requireNonNull(deviceConfig);
    Objects.requireNonNull(resourceVersion);
    final Span span = tracer.buildSpan("update Device").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, deviceConfig.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, deviceConfig.getDeviceId()).start();
    resourceVersion.ifPresent(v -> TracingHelper.TAG_RESOURCE_VERSION.set(span, v));
    final JsonObject updateDeviceQuery = MongoDbDocumentBuilder.builder().withVersion(resourceVersion).withTenantId(deviceConfig.getTenantId()).withDeviceId(deviceConfig.getDeviceId()).document();
    final var document = JsonObject.mapFrom(deviceConfig);
    if (LOG.isTraceEnabled()) {
        LOG.trace("replacing existing device document with:{}{}", System.lineSeparator(), document.encodePrettily());
    }
    return mongoClient.findOneAndReplaceWithOptions(collectionName, updateDeviceQuery, document, new FindOptions(), new UpdateOptions().setReturningNewDocument(true)).compose(result -> {
        if (result == null) {
            return MongoDbBasedDao.checkForVersionMismatchAndFail(deviceConfig.getDeviceId(), resourceVersion, getById(deviceConfig.getTenantId(), deviceConfig.getDeviceId(), span));
        } else {
            span.log("successfully updated device");
            return Future.succeededFuture(result.getString(DeviceDto.FIELD_VERSION));
        }
    }).onFailure(t -> TracingHelper.logError(span, "error updating device", t)).recover(this::mapError).onComplete(r -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Status(io.vertx.ext.healthchecks.Status) Sort(org.eclipse.hono.service.management.Sort) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) HealthCheckProvider(org.eclipse.hono.service.HealthCheckProvider) SearchResult(org.eclipse.hono.service.management.SearchResult) References(io.opentracing.References) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) Set(java.util.Set) MongoClient(io.vertx.ext.mongo.MongoClient) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) Span(io.opentracing.Span) FindOptions(io.vertx.ext.mongo.FindOptions) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) FindOptions(io.vertx.ext.mongo.FindOptions) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span) UpdateOptions(io.vertx.ext.mongo.UpdateOptions)

Example 33 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class MongoDbBasedDeviceDao method delete.

/**
 * {@inheritDoc}
 */
@Override
public Future<Void> delete(final String tenantId, final SpanContext tracingContext) {
    Objects.requireNonNull(tenantId);
    final Span span = tracer.buildSpan("delete all of Tenant's Devices").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    final JsonObject removeDevicesQuery = MongoDbDocumentBuilder.builder().withTenantId(tenantId).document();
    return mongoClient.removeDocuments(collectionName, removeDevicesQuery).compose(result -> {
        span.log("successfully deleted devices");
        LOG.debug("successfully deleted devices of tenant [tenant-id: {}]", tenantId);
        return Future.succeededFuture((Void) null);
    }).recover(error -> {
        LOG.debug("error deleting devices", error);
        TracingHelper.logError(span, "error deleting devices", error);
        return mapError(error);
    }).onComplete(r -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Status(io.vertx.ext.healthchecks.Status) Sort(org.eclipse.hono.service.management.Sort) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) HealthCheckProvider(org.eclipse.hono.service.HealthCheckProvider) SearchResult(org.eclipse.hono.service.management.SearchResult) References(io.opentracing.References) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) Set(java.util.Set) MongoClient(io.vertx.ext.mongo.MongoClient) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) Span(io.opentracing.Span) FindOptions(io.vertx.ext.mongo.FindOptions) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span)

Example 34 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class MongoDbBasedDeviceDao method resolveGroupMembers.

/**
 * {@inheritDoc}
 */
@Override
public Future<Set<String>> resolveGroupMembers(final String tenantId, final Set<String> viaGroups, final SpanContext tracingContext) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(viaGroups);
    if (viaGroups.isEmpty()) {
        return Future.succeededFuture(Set.of());
    }
    final Span span = tracer.buildSpan("resolve group members").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    span.log("resolving " + viaGroups.size() + " device groups");
    final var deviceMemberOfSpec = new JsonObject().put("$exists", true).put("$in", new JsonArray(List.copyOf(viaGroups)));
    final JsonObject resolveGroupMembersQuery = MongoDbDocumentBuilder.builder().withTenantId(tenantId).document().put(PROPERTY_DEVICE_MEMBER_OF, deviceMemberOfSpec);
    // retrieve only the deviceId instead of the whole document.
    final FindOptions findOptions = new FindOptions().setFields(new JsonObject().put(RegistrationConstants.FIELD_PAYLOAD_DEVICE_ID, true).put("_id", false));
    return mongoClient.findWithOptions(collectionName, resolveGroupMembersQuery, findOptions).map(documents -> {
        if (documents == null) {
            final Set<String> result = Set.of();
            return result;
        } else {
            span.log("successfully resolved " + documents.size() + " group members");
            return documents.stream().map(json -> json.getString(RegistrationConstants.FIELD_PAYLOAD_DEVICE_ID)).collect(Collectors.toSet());
        }
    }).onComplete(r -> span.finish());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Status(io.vertx.ext.healthchecks.Status) Sort(org.eclipse.hono.service.management.Sort) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) HealthCheckProvider(org.eclipse.hono.service.HealthCheckProvider) SearchResult(org.eclipse.hono.service.management.SearchResult) References(io.opentracing.References) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) Set(java.util.Set) MongoClient(io.vertx.ext.mongo.MongoClient) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) Span(io.opentracing.Span) FindOptions(io.vertx.ext.mongo.FindOptions) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) FindOptions(io.vertx.ext.mongo.FindOptions) Set(java.util.Set) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span)

Example 35 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class ClasspathSchemaCreator method runScript.

private Future<Void> runScript(final JdbcProperties jdbcProperties, final String script, final SpanContext ctx) {
    final JDBCClient jdbcClient = JdbcProperties.dataSource(vertx, jdbcProperties);
    final Promise<Void> clientCloseTracker = Promise.promise();
    SQL.runTransactionally(jdbcClient, tracer, ctx, (connection, context) -> {
        final var expanded = Statement.statement(script).expand();
        log.debug("Creating database schema in [{}] with script: {}", jdbcProperties.getUrl(), expanded);
        return expanded.query(jdbcClient).recover(SQL::translateException);
    }).onComplete(ar -> jdbcClient.close(clientCloseTracker));
    return clientCloseTracker.future();
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) JdbcProperties(org.eclipse.hono.service.base.jdbc.config.JdbcProperties) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) Future(io.vertx.core.Future) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JDBCClient(io.vertx.ext.jdbc.JDBCClient) Buffer(io.vertx.core.buffer.Buffer) SchemaCreator(org.eclipse.hono.deviceregistry.jdbc.SchemaCreator) Span(io.opentracing.Span) TracingHelper(org.eclipse.hono.tracing.TracingHelper) JDBCClient(io.vertx.ext.jdbc.JDBCClient)

Aggregations

SpanContext (io.opentracing.SpanContext)118 Span (io.opentracing.Span)81 Future (io.vertx.core.Future)70 Tracer (io.opentracing.Tracer)60 Objects (java.util.Objects)57 HttpURLConnection (java.net.HttpURLConnection)56 JsonObject (io.vertx.core.json.JsonObject)55 TracingHelper (org.eclipse.hono.tracing.TracingHelper)55 Logger (org.slf4j.Logger)54 LoggerFactory (org.slf4j.LoggerFactory)54 List (java.util.List)51 Promise (io.vertx.core.Promise)45 Optional (java.util.Optional)45 Map (java.util.Map)40 ClientErrorException (org.eclipse.hono.client.ClientErrorException)39 MessageHelper (org.eclipse.hono.util.MessageHelper)33 UUID (java.util.UUID)31 Collectors (java.util.stream.Collectors)31 HashMap (java.util.HashMap)25 Vertx (io.vertx.core.Vertx)24