use of io.opentracing.SpanContext in project hono by eclipse.
the class MongoDbBasedDeviceDao method create.
/**
* {@inheritDoc}
*/
@Override
public Future<String> create(final DeviceDto deviceConfig, final SpanContext tracingContext) {
Objects.requireNonNull(deviceConfig);
final Span span = tracer.buildSpan("create Device").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, deviceConfig.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, deviceConfig.getDeviceId()).start();
return mongoClient.insert(collectionName, JsonObject.mapFrom(deviceConfig)).map(success -> {
span.log("successfully created device");
LOG.debug("successfully created device [tenant: {}, device-id: {}, resource-version: {}]", deviceConfig.getTenantId(), deviceConfig.getDeviceId(), deviceConfig.getVersion());
return deviceConfig.getVersion();
}).recover(error -> {
if (MongoDbBasedDao.isDuplicateKeyError(error)) {
LOG.debug("device [{}] already exists for tenant [{}]", deviceConfig.getDeviceId(), deviceConfig.getTenantId(), error);
TracingHelper.logError(span, "device already exists");
throw new ClientErrorException(deviceConfig.getTenantId(), HttpURLConnection.HTTP_CONFLICT, "device already exists");
} else {
TracingHelper.logError(span, "error creating device", error);
return mapError(error);
}
}).onComplete(r -> span.finish());
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class MongoDbBasedDeviceDao method find.
/**
* {@inheritDoc}
*/
@Override
public Future<SearchResult<DeviceWithId>> find(final String tenantId, final int pageSize, final int pageOffset, final List<Filter> filters, final List<Sort> sortOptions, final SpanContext tracingContext) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(filters);
Objects.requireNonNull(sortOptions);
if (pageSize <= 0) {
throw new IllegalArgumentException("page size must be a positive integer");
}
if (pageOffset < 0) {
throw new IllegalArgumentException("page offset must not be negative");
}
final Span span = tracer.buildSpan("find Devices").addReference(References.CHILD_OF, tracingContext).start();
final JsonObject filterDocument = MongoDbDocumentBuilder.builder().withTenantId(tenantId).withDeviceFilters(filters).document();
final JsonObject sortDocument = MongoDbDocumentBuilder.builder().withDeviceSortOptions(sortOptions).document();
return processSearchResource(pageSize, pageOffset, filterDocument, sortDocument, MongoDbBasedDeviceDao::getDevicesWithId).onFailure(t -> TracingHelper.logError(span, "error finding devices", t)).onComplete(r -> span.finish());
}
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 String deviceId, final Optional<String> resourceVersion, final SpanContext tracingContext) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(deviceId);
Objects.requireNonNull(resourceVersion);
final Span span = tracer.buildSpan("delete Device").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantId).withTag(TracingHelper.TAG_DEVICE_ID, deviceId).start();
resourceVersion.ifPresent(v -> TracingHelper.TAG_RESOURCE_VERSION.set(span, v));
LOG.trace("deleting device [tenant-id: {}, device-id: {}, version: {}]", tenantId, deviceId, resourceVersion.orElse(null));
final JsonObject deleteDeviceQuery = MongoDbDocumentBuilder.builder().withVersion(resourceVersion).withTenantId(tenantId).withDeviceId(deviceId).document();
return mongoClient.findOneAndDelete(collectionName, deleteDeviceQuery).compose(result -> {
if (result == null) {
return MongoDbBasedDao.checkForVersionMismatchAndFail(deviceId, resourceVersion, getById(tenantId, deviceId, span));
} else {
span.log("successfully deleted device");
return Future.succeededFuture((Void) null);
}
}).onFailure(t -> TracingHelper.logError(span, "error deleting device", t)).recover(this::mapError).onComplete(r -> span.finish());
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class MongoDbBasedTenantDao method find.
/**
* {@inheritDoc}
*/
@Override
public Future<SearchResult<TenantWithId>> find(final int pageSize, final int pageOffset, final List<Filter> filters, final List<Sort> sortOptions, final SpanContext tracingContext) {
Objects.requireNonNull(filters);
Objects.requireNonNull(sortOptions);
if (pageSize <= 0) {
throw new IllegalArgumentException("page size must be a positive integer");
}
if (pageOffset < 0) {
throw new IllegalArgumentException("page offset must not be negative");
}
final Span span = tracer.buildSpan("find Tenants").addReference(References.CHILD_OF, tracingContext).start();
final JsonObject filterDocument = MongoDbDocumentBuilder.builder().withTenantFilters(filters).document();
final JsonObject sortDocument = MongoDbDocumentBuilder.builder().withTenantSortOptions(sortOptions).document();
return processSearchResource(pageSize, pageOffset, filterDocument, sortDocument, MongoDbBasedTenantDao::getTenantsWithId).onFailure(t -> TracingHelper.logError(span, "error finding tenants", t)).onComplete(r -> span.finish());
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class MongoDbBasedCredentialsDao method getByAuthIdAndType.
/**
* {@inheritDoc}
*/
@Override
public Future<CredentialsDto> getByAuthIdAndType(final String tenantId, final String authId, final String type, final SpanContext tracingContext) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(authId);
Objects.requireNonNull(type);
final Span span = tracer.buildSpan("get Credentials by auth ID and type").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantId).withTag(TracingHelper.TAG_AUTH_ID, authId).withTag(TracingHelper.TAG_CREDENTIALS_TYPE, type).start();
final JsonObject filter = MongoDbDocumentBuilder.builder().withTenantId(tenantId).withAuthId(authId).withType(type).document();
if (LOG.isTraceEnabled()) {
LOG.trace("retrieving credentials using filter:{}{}", System.lineSeparator(), filter.encodePrettily());
}
return mongoClient.findOne(collectionName, filter, PROJECTION_CREDS_BY_TYPE_AND_AUTH_ID).map(result -> {
if (result == null) {
throw new ClientErrorException(tenantId, HttpURLConnection.HTTP_NOT_FOUND, "no matching credentials on record");
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("credentials data from collection:{}{}", System.lineSeparator(), result.encodePrettily());
}
final var dto = result.mapTo(CredentialsDto.class);
dto.getCredentials().stream().forEach(cred -> cred.decryptFields(fieldLevelEncryption));
return dto;
}
}).onFailure(t -> {
LOG.debug("error retrieving credentials by auth-id and type", t);
TracingHelper.logError(span, "error retrieving credentials by auth-id and type", t);
}).recover(this::mapError).onComplete(r -> span.finish());
}
Aggregations