Recording sales amount in datadog metrics - java

I'm trying to record my website sales $ amount in datadog. However I'm getting way more than the actual value.
I'm using java-dogstatsd client and spring. My application is running on 3 hosts. I recorded all metrics (using sendWebOrder method) but no luck.
#EnableConfigurationProperties({DataDogProperties.class})
#Component
public class DDMetrics {
#Autowired
DataDogProperties dataDogProperties;
#Autowired
private NonBlockingStatsDClient statsd;
private Map<TopicPartition,Long> lags = new HashMap<>();
#Bean
private NonBlockingStatsDClient initClient() {
NonBlockingStatsDClient metricsClient = new NonBlockingStatsDClient(
dataDogProperties.getServiceName(),
dataDogProperties.getHostname(),
dataDogProperties.getPort();
return metricsClient;
}
public void sendWebOrder(WebOrder webOrder) {
List<String> tags = new ArrayList<>();
tags.add("transactionType:" + webOrder.getTransactionType());
tags.add("dataSourceType:" + webOrder.getDataSourceType()));
statsd.count("amount_count", webOrder.getAmount(), String.join(",", tags));
statsd.recordDistributionValue("amount_dist", webOrder.getAmount(), String.join(",", tags));
statsd.recordHistogramValue("amount_hist", webOrder.getAmount(), String.join(",", tags));
statsd.recordGaugeValue("amount_gauge", webOrder.getAmount(), String.join(",", tags));
statsd.incrementCounter("weborder", String.join(",", tags));
}
I'm trying to generate a datadog toplist by transactiontype. I'm not getting the correct amount in any of the metrics (tried mainly count, gauge and histogram.sum). Here is my datadog config:
{
"viz": "toplist",
"requests": [
{
"q": "top(sum:projecta.webtransactions.amount_histogram.sum{$TransactionType} by {transactiontype}, 10, 'sum', 'desc')",
"type": "area",
"style": {
"palette": "dog_classic",
"type": "solid",
"width": "normal"
},
"aggregator": "sum",
"conditional_formats": []
}
],
"autoscale": true
}
What am I missing? Is this the correct way to record money value? Do I've to do any rollup in config?
Any help is appreciated.

Related

Using Jaca JsonPath to exclude items from JSON response

I am using the JsonSmartJsonProvider and my JSON looks like this
{
"info": {
"clientCount": 1,
"compactorVersion": 2,
"processMonitor": {
"processList": [
{
"name": "java.exe",
"commandLine": "",
"pid": 6224
}
]
}
}
}
I'm trying to exclude "processList", but keep everything else. I've tried variations on $.info[?(# noneof ['processMonitor'])], but I always end up with "info" being empty in the response. Is it possible to use JsonPath to do this? The code that is used to do this looks like this:
DocumentContext document = JsonPath.using(CONFIGURATION).parse(json);
Map<String, Object> result = new HashMap<>();
paths.forEach((key, value) -> result.put(key, document.read(value)));
return result;
As mentioned, you are actually looking for a JSON transformation. JOLT is a common library to do just that. A solution can look like this:
import java.util.List;
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;
public class MyJsonTransformer {
public static void main(String[] args) throws Exception {
List<Object> specs = JsonUtils.classpathToList("/spec.json");
Chainr chainr = Chainr.fromSpec(specs);
Object inputJSON = JsonUtils.classpathToObject("/input.json");
Object transformedOutput = chainr.transform(inputJSON);
System.out.println(JsonUtils.toPrettyJsonString(transformedOutput));
}
}
And a jolt remover spec file like this:
[
{
"operation": "remove",
"spec": {
"info": {
"processMonitor": {
"processList": ""
}
}
}
}
]
You can try JOLT online with your input and the spec above here. Pretty neat.
The JSON and spec can be defined inline as well. I have not tested this end to end.

how to implement $size in spring Data Aggregation in java

how to implement below code in java using spring Data mongodb
db.profil.aggregate([
{ "$match": { "typ": "Organisation" } },
{ "$group": {
"_id": null,
"count": {
"$sum": { "$size": "$foos" }
}
} }
])
MongoDB has an Aggregation framework for Java to support all aggregation APIs.
Try following:
import static com.mongodb.client.model.Accumulators.*;
import static com.mongodb.client.model.Aggregates.*;
import static com.mongodb.client.model.Projections.*;
Bson match = match(eq("typ", "Organisation")));
Bson projection = new Document("$size", "$foos" );
Bson group = group("$_id", Accumulators.sum("count",projection));
MongoCursor<Document> cursor = collection.aggregate(asList(match, group)).iterator();

Connecting to MongoDB Atlas: com.mongodb.MongoCommandException: Command failed with error 8000

so I'm trying to connect to MongoDB Atlas Service, which is hosting my database (which i have done in the past with no problems), but I keep getting this error and I can't understand why. I cannot find anyone with the same issue, so I'm quite stuck.
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 8000: 'not authorized on admin to execute command { insert: "adminCol", ordered: true, documents: [[{_id ObjectIdHex("5a4e3b6dd04f1c047975fdd5")} {id 512} {name peter jones} {hello hi}]] }' on server *******-shard-00-02-xygnn.mongodb.net:27017. The full response is { "ok" : 0, "errmsg" : "not authorized on admin to execute command { insert: \"adminCol\", ordered: true, documents: [[{_id ObjectIdHex(\"5a4e3b6dd04f1c047975fdd5\")} {id 512} {name peter jones} {hello hi}]] }", "code" : 8000, "codeName" : "AtlasError" }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164)
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:98)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:441)
at com.mongodb.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:76)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:189)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:263)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:126)
at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:373)
at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:255)
at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:66)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:199)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:190)
at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:432)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:190)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:66)
at com.mongodb.Mongo$3.execute(Mongo.java:833)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1025)
at com.mongodb.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:513)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:493)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:487)
at AdminDB.addAdmin(AdminDB.java:51)
at AdminDB.main(AdminDB.java:76)
This is the code I'm using.
public class AdminDB {
private MongoDatabase adminDB;
private MongoCollection<Document> adminCollection;
private String adminCol = "adminCol";
public AdminDB() {
MongoClientURI uri = new MongoClientURI(
"mongodb://***********-shard-00-00-xygnn.mongodb.net:27017,*******-shard-00-01-xygnn.mongodb.net:27017,******-shard-00-02-xygnn.mongodb.net:27017/test?ssl=true&replicaSet=******-shard-0&authSource=admin");
MongoClient mongoClient = new MongoClient(uri);
adminDB = mongoClient.getDatabase("admin");
adminCollection = adminDB.getCollection(adminCol);
}
public void addAdmin(JsonObject json) {
adminCollection.insertOne(Document.parse(json.toString()));
}
public static void main(String[] args) {
AdminDB adminDB = new AdminDB();
JsonObject json = new JsonObject();
json.addProperty("id", 512);
json.addProperty("name", "peter jones");
json.addProperty("hello", "hi");
adminDB.addAdmin(json);
}
}
To clarify, MongoDB Atlas M0 (Free Tier), M2, or M5 shared starter cluster do not support read/write operations on any collections within the admin database. See also Command Limitations in Free Tier Clusters.
Change the database name to insert document into:
database = mongoClient.getDatabase("anotherDatabase");

json file I/O with pretty print format using gson in java?

I already know how gson works and also know how to enable pretty
print.
I want to use gson and not simplejson.
The problem I had is that I wasn't able to create a file consisting of a List of Employee objects.
I've seen every other java threads in stackoverflow, mkyong, google's github and many others sites and I still wasn't able to accomplish this simple thing.
I already know how to read a file with this specific format but I wasn't able to write it.
The problem is I was not able to combine all these things together in a program.
A List of objects in gson with pretty print enabled must have the proper indentation, and every object must be separated with a comma and those objects must be wrapped between [ ] .
The problem explained with code
:
public class Employee implements Serializable {
private String lastName;
private String address;
private int id;
private String name;
}
I want to create a json file with the exact following content
[
{
"id":1,
"name": "John",
"lastName": "Doe",
"address": "NY City"
},
{
"id":2,
"name": "John",
"lastName": "Doe",
"address": "Canada"
},
{
"id":3,
"name": "John",
"lastName": "Doe",
"address": "Las Vegas"
},
]
I managed to create and write a json file of Person objects (as gson json Person objects), and read it, but again only as Person objects, where every line is an independent object, not a part of a List or Array of Person objects, like this
{"id":1,"name": "John","last": "Doe","address": "NY City"}
{"id":2,"name": "John","last": "Doe","address": "Canada"}
{"id":3,"name": "John","last": "Doe","address": "Las Vegas"}
but that's not what I want my final program to do.
I was also able to hard code a file with the following info and format and successfully obtain the Person objects
[
{
"id":1,
"name": "John",
"lastName": "Doe",
"address": "NY City"
},
{
"id":2,
"name": "John",
"lastName": "Doe",
"address": "Canada"
},
{
"id":3,
"name": "John",
"lastName": "Doe",
"address": "Las Vegas"
},
]
but I don't know how to create and write this json file with a java program
as an array of Person objects, where every Person object is a part of this
list or array with pretty print format, as the one I hard coded and
am able to read.
How can I do that in an elegant way?
EDIT---
Thanks a lot to #Shyam!
This is my final code, hope it helps someone.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class TestFileOfGsonWriter {
Gson gson ;
String filePath ;
BufferedReader bufferToReader ;
public TestFileOfGsonWriter()
{
this.filePath =
"C:\\FileOfGsonSingleListOfEmployees\\employees.json" ;
}
public List<Employee> createEmployees()
{
Employee arya = new Employee("Stark", "#81, 2nd main, Winterfell", 2, "Arya");
Employee jon = new Employee("Snow", "#81, 2nd main, Winterfell", 1, "Jon");
Employee sansa = new Employee("Stark", "#81, 2nd main, Winterfell", 3, "Sansa");
List<Employee> employees = new ArrayList<>();
employees.add(jon);
employees.add(arya);
employees.add(sansa);
return employees ;
}
public void jsonWriter(List<Employee> employees, String filePath)
{
this.gson = new GsonBuilder().setPrettyPrinting().create();
try(FileWriter writer = new FileWriter(filePath))
{
gson.toJson(employees,writer);
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void showEmployeeObjects()
{
try {
List<Employee> employees = this.getAllEmployees();
employees.forEach(e -> Employee.showEmployeeDetails(e));
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<Employee> getAllEmployees() throws IOException
{
FileReader reader = new FileReader(this.filePath);
this.bufferToReader = new BufferedReader(reader) ;
ArrayList <Employee> employees = this.gson.fromJson(getJson(),
new TypeToken<ArrayList<Employee>>(){}.getType());
return employees ;
}
private String getJson() throws IOException
{
StringBuilder serializedEmployee = new StringBuilder();
String line ;
while ( (line = this.bufferToReader.readLine()) != null )
{
serializedEmployee.append(line);
}
this.bufferToReader.close();
return serializedEmployee.toString();
}
public static void main(String[] args) {
TestFileOfGsonWriter testWriting = new TestFileOfGsonWriter() ;
List<Employee> employees = testWriting.createEmployees();
testWriting.jsonWriter(employees, testWriting.filePath);
testWriting.showEmployeeObjects();
}
}
I modified my Employee class so it would match with his dummy objects which were better I think, this is how it looks now.
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
String name ;
String address;
String lastName ;
int id ;
public static void showEmployeeDetails(Employee e)
{
System.out.println();
System.out.println("Employee's name: "+e.name);
System.out.println("Employee's last name"+e.lastName);
System.out.println("Employee's address: "+e.address);
System.out.println("Employee's ID: "+e.id);
}
public Employee(String myName, String myAddress, int myId, String myLastName)
{
this.name = myName ;
this.address = myAddress;
this.lastName = myLastName;
this.id = myId ;
}
}
So, the json file the program creates looks exactly how I wanted:
[
{
"name": "Snow",
"address": "#81, 2nd main, Winterfell",
"lastName": "Jon",
"id": 1
},
{
"name": "Stark",
"address": "#81, 2nd main, Winterfell",
"lastName": "Arya",
"id": 2
},
{
"name": "Stark",
"address": "#81, 2nd main, Winterfell",
"lastName": "Sansa",
"id": 3
}
]
and finally, this is the output:
Employee's name: Snow
Employee's last nameJon
Employee's address: #81, 2nd main, Winterfell
Employee's ID: 1
Employee's name: Stark
Employee's last nameArya
Employee's address: #81, 2nd main, Winterfell
Employee's ID: 2
Employee's name: Stark
Employee's last nameSansa
Employee's address: #81, 2nd main, Winterfell
Employee's ID: 3
As you are new, I'll quickly walk you through the process of writing a List of Employee objects to a JSON file with pretty printing:
Step 1: Create a method that takes in a List and a String filePath:
public void jsonWriter(List<Employee> employees, String filePath)
Step 2: Build a Gson Object with pretty printing enabled:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Step 3: Write your List<Employee> to a JSON file in the given filePath using a FileWriter:
try(FileWriter writer = new FileWriter(filePath)) {
gson.toJson(employees, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
Finally the entire method should look something like this:
public void jsonWriter(List<Employee> employees, String filePath) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try(FileWriter writer = new FileWriter(filePath)) {
gson.toJson(employees, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Step 4: Now, build your Employee objects, add them to a List and call this method with the appropriate filePath
Employee arya = new Employee("Stark", "#81, 2nd main, Winterfell", 2, "Arya");
Employee jon = new Employee("Snow", "#81, 2nd main, Winterfell", 1, "Jon");
Employee sansa = new Employee("Stark", "#81, 2nd main, Winterfell", 3, "Sansa");
List<Employee> employees = new ArrayList<>();
employees.add(jon);
employees.add(arya);
employees.add(sansa);
jsonWriter(employees, "C:/downloads/employees.json");
After running this code, the contents of JSON file will look something like this:
[
{
"lastName": "Snow",
"address": "#81, 2nd main, Winterfell",
"id": 1,
"name": "Jon"
},
{
"lastName": "Stark",
"address": "#81, 2nd main, Winterfell",
"id": 2,
"name": "Arya"
},
{
"lastName": "Stark",
"address": "#81, 2nd main, Winterfell",
"id": 3,
"name": "Sansa"
}
]
I hope this will help you in your learning process.
Note: I've used some random Employee names and details. You can replace it with your required details.
Firstly store those objects into a list of objects. Then add this line of code for pretty print.
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson (list));

Mongo dropAllUsersFromDatabase command not work by using MongoClient

I am new to MongoDB and have below code for dropping database. Before dropping the database, I have run a command dropAllUsersFromDatabase for removing all users from this database. However, this command didn't remove those users.
private final MongoClient mongo;
public void drop(final String databaseName) {
final MongoDatabase database = mongo.getDatabase(databaseName);
final BasicDBObject commandArguments = new BasicDBObject();
commandArguments.put("dropAllUsersFromDatabase", 1);
final BasicDBObject command = new BasicDBObject(commandArguments);
database.runCommand(command);
database.drop();
}
Those users still remaining in system.users collection in the admin database, which mongo stores user authentication and authorization information. For example:
{
"_id" : "57b3cc41851cb3aab00a3c84.57b3cc41851cb3aab00a3c84",
"user" : "57b3cc41851cb3aab00a3c84",
"db" : "57b3cc41851cb3aab00a3c84",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "8LjAxK/7z9yJLBfAchkLGg==",
"storedKey" : "HmktZ1GF+uQ8MJqIzY3/lU7VJEg=",
"serverKey" : "9CyQxvtK9BWT80la8p4o+kHyNwc="
}
},
"roles" : [
{
"role" : "dbOwner",
"db" : "57b3cc41851cb3aab00a3c84"
}
]
}
But, if I execute the command by mongo as below, it will delete those users correctly.
db.runCommand({ dropAllUsersFromDatabase: 1});
The login authoriztion are same and there has no exception when running database.runCommand(command);

Categories

Resources