Skip to content

Commit

Permalink
runtime-v2: logYaml step
Browse files Browse the repository at this point in the history
  • Loading branch information
brig committed Oct 4, 2023
1 parent 48e852d commit 8d4a7f0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 10 deletions.
21 changes: 21 additions & 0 deletions plugins/tasks/log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,48 @@
* =====
*/

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.walmartlabs.concord.runtime.v2.sdk.Task;
import com.walmartlabs.concord.runtime.v2.sdk.TaskResult;
import com.walmartlabs.concord.runtime.v2.sdk.Variables;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import javax.inject.Named;
import java.io.Serializable;

@Named("log")
public class LoggingTaskV2 implements Task {

private static final ObjectMapper yamlObjectMapper = createYamlObjectMapper();

@Override
public TaskResult execute(Variables variables) {
Object msg = variables.get("msg");
String format = variables.getString("format");

String logLevel = variables.getString("level", "INFO");
switch (logLevel.toUpperCase()) {
case "DEBUG": {
LogUtils.debug(msg);
LogUtils.debug(formatMessage(format, msg));
break;
}
case "INFO": {
LogUtils.info(msg);
LogUtils.info(formatMessage(format, msg));
break;
}
case "WARN": {
LogUtils.warn(msg);
LogUtils.warn(formatMessage(format, msg));
break;
}
case "ERROR": {
LogUtils.error(msg);
LogUtils.error(formatMessage(format, msg));
break;
}
default:
LogUtils.info(msg);
LogUtils.info(formatMessage(format, msg));
}

return TaskResult.success();
Expand Down Expand Up @@ -94,4 +102,28 @@ public static void error(Object o) {
public void call(String s) {
LogUtils.info(s);
}

private static Object formatMessage(String format, Object msg) {
if ("yaml".equalsIgnoreCase(format)) {
try {
return "\n" + yamlObjectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(msg);
} catch (Exception e) {
throw new RuntimeException("Invalid yaml:" + e.getMessage());
}
}

return msg;
}

private static ObjectMapper createYamlObjectMapper() {
return defaultObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
}

private static ObjectMapper defaultObjectMapper(JsonFactory jf) {
ObjectMapper om = new ObjectMapper(jf);
om.registerModule(new Jdk8Module());
om.registerModule(new JavaTimeModule());
return om;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static com.walmartlabs.concord.runtime.v2.parser.GrammarMisc.*;
import static com.walmartlabs.concord.runtime.v2.parser.GroupOfStepsGrammar.group;
import static com.walmartlabs.concord.runtime.v2.parser.LogGrammar.logStep;
import static com.walmartlabs.concord.runtime.v2.parser.LogGrammar.logYamlStep;
import static com.walmartlabs.concord.runtime.v2.parser.ParallelGrammar.parallelBlock;
import static com.walmartlabs.concord.runtime.v2.parser.ReturnGrammar.returnStep;
import static com.walmartlabs.concord.runtime.v2.parser.ScriptGrammar.script;
Expand Down Expand Up @@ -181,7 +182,7 @@ private static Parser<Atom, YamlValue> _val(JsonToken t) {
private static final Parser<Atom, Step> stepObject =
betweenTokens(JsonToken.START_OBJECT, JsonToken.END_OBJECT,
choice(choice(parallelBlock, group, exprFull), choice(taskFull, script, callFull, callForm),
choice(checkpoint, ifExpr, switchExpr), setVars, logStep, throwStep, suspendStep));
choice(checkpoint, ifExpr, switchExpr, setVars), logStep, logYamlStep, throwStep, suspendStep));

// step := exit | exprShort | parallelBlock | stepObject
private static final Parser<Atom, Step> step = orError(choice(exit, returnStep, exprShort, stepObject), YamlValueType.STEP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,26 @@
import static com.walmartlabs.concord.runtime.v2.parser.GrammarMisc.namedStep;
import static com.walmartlabs.concord.runtime.v2.parser.GrammarOptions.namedOptions;
import static com.walmartlabs.concord.runtime.v2.parser.GrammarV2.anyVal;
import static com.walmartlabs.concord.runtime.v2.parser.TaskGrammar.optionsWithStepName;

public final class LogGrammar {

public static final Parser<Atom, TaskCall> logStep =
namedStep("log", YamlValueType.TASK, (stepName, a) ->
anyVal.bind(msg ->
namedOptions.map(options -> new TaskCall(a.location, "log", optionsWithStepName(stepName)
namedOptions.map(options -> new TaskCall(a.location, "log", TaskGrammar.optionsWithStepName(stepName)
.putInput("msg", msg)
.putAllMeta(options.meta())
.build()))));

public static final Parser<Atom, TaskCall> logYamlStep =
namedStep("logYaml", YamlValueType.TASK, (stepName, a) ->
anyVal.bind(msg ->
namedOptions.map(options -> new TaskCall(a.location, "log", TaskGrammar.optionsWithStepName(stepName)
.putInput("msg", msg)
.putInput("format", "yaml")
.putAllMeta(options.meta())
.build()))));

private LogGrammar() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.walmartlabs.concord.runtime.v2.schema;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2020 Walmart Inc.
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaTitle;

import java.util.Map;

@JsonTypeName("LogYamlStep")
public interface LogYamlStepMixIn extends NamedStep {

@JsonProperty(value = "logYaml", required = true)
@JsonSchemaTitle("Log yaml step")
String logYaml();

@JsonProperty("meta")
Map<String, Object> meta();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@JsonSubTypes.Type(value = ExpressionShortMixIn.class, name = "Expression (short form)"),
@JsonSubTypes.Type(value = ExpressionFullMixIn.class, name = "Expression (full form)"),
@JsonSubTypes.Type(value = LogStepMixIn.class, name = "Log"),
@JsonSubTypes.Type(value = LogYamlStepMixIn.class, name = "LogYaml"),
@JsonSubTypes.Type(value = FlowCallStepMixIn.class, name = "Flow call"),
@JsonSubTypes.Type(value = FormCallStepMixIn.class, name = "Form call"),
@JsonSubTypes.Type(value = IfStepMixIn.class, name = "IF step"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public void test() throws Exception {
byte[] log = run();
assertLog(log, ".*Hello, Concord!.*");
assertLog(log, ".*" + Pattern.quote("defaultsMap:{a=a-value}") + ".*");
assertLog(log, ".*k: \"value\".*");

verify(processStatusCallback, times(1)).onRunning(instanceId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
flows:
default:
- log: "Hello, ${name}!"
- task: testDefaults
- task: testDefaults
- logYaml:
k: "value"

0 comments on commit 8d4a7f0

Please sign in to comment.